从另一个线程访问单例对象
调用服务方法
我使用ThreadPool.QueueUserWorkItem(o => service.Method(arg1, arg2));
服务有对象“loggingService”,我使用 Spring.Net
私有只读 ILoggingServiceloggingService = ObjectBuilder.GetObjectByName("LoggingService");
'LoggingService' 类是单例的。它将日志信息写入log.txt。
当我尝试在此服务方法中调用loggingService.Info(“test”)时,出现异常:文件正被另一个进程占用。
如何访问loggingService?
I call service method using
ThreadPool.QueueUserWorkItem(o => service.Method(arg1, arg2));
Service has object 'loggingService' with I was get using Spring.Net
private readonly ILoggingService loggingService = ObjectBuilder.GetObjectByName("LoggingService");
'LoggingService' class is singleton. It writes log info to log.txt.
When I try to call loggingService.Info("test") in this service method, I get exception: file is busy by another process.
How can I access to the loggingService?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的单例显然是每个线程的。
您将需要某种跨线程传递
LoggingService
的方法。例如,您可以在原始线程中设置
service.loggingService
。或者,您可以配置 Spring.Net 以使其成为非线程本地单例。
请注意,您的 LoggingService 必须是线程安全的,否则您会在运行时遇到奇怪的错误。
Your singleton is apparently per-thread.
You will need some way of passing the
LoggingService
across threads.For example, you could set
service.loggingService
in the original thread.Alternatively, you might be able to configure Spring.Net to make it a non-thread-local singleton.
Note that your LoggingService must be thread-safe, or you'll get strange errors at runtime.
我在编写一些使用一堆线程的客户端应用程序时遇到了类似的问题。
基本上,您希望 LoggingService 保留一个内部队列(其访问应通过锁控制),并且每次调用 log 方法时,您只需将消息附加到该队列中。在 log 方法结束时检查队列当前是否正在写入文件,如果没有,则开始写入。
I had a similar issue while writing some client side application that used a bunch of threads.
Basically you want your LoggingService to keep an internal queue (whose access should be controlled via a lock) and every time you call the log method you only append the message to this queue. At the end of the log method check if the queue is currently being written to a file and if not, start writing.
我做到了!
我使用队列和线程:
我在应用程序末尾调用 Shutdown() :
I did it!
I use Queue and threading:
I call Shutdown() at the end of app: