在后台工作线程中使用 C# 全局变量是否安全
你好 我正在开发一个简单的桌面应用程序,它需要处理一些操作,例如加载网页,这可能会阻塞主线程,因此我将代码移至后台工作人员。
我的问题是有一个名为 UCSProject 的重类,其中包含许多字符串和列表字段,我需要将此类的实例传递给后台工作者,因为该类有点重,我想减少重复的数量直接使用全局变量来实例化,而不是将其作为参数传递给后台工作者。
简而言之,我只想知道从 C# 中的后台工作线程访问全局变量是否安全
Hi
I am working on a simple desktop application, it needs to handle some operations like loading a webpage which may block the main thread, so i moved the code to a background worker.
My problem is there is a heavy class named UCSProject, which contains many string and List fields, i need to pass an instance of this class to the background worker, since the class is a bit heavy, i would like to reduce the number of duplicate instances by using the global variable directly, instead of passing it as an argument to the background worker.
To make it short, I just want to know is it safe to access global variables from a background worker thread in C#
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
除非您的两个线程(后台线程和普通线程)都没有修改对象,否则它是安全的。
如果您希望彼此修改对象,请使用 锁定
It is safe unless until both your threads(background & normal) not modifying the object.
If you want your object to be modified by each other, use Lock
通过你的问题,我怀疑你不明白类的变量是如何工作的。您不需要全局变量来只拥有对象的一份副本。所有变量都将指向完全相同的对象,除非您
克隆
它或使用旧变量作为原型创建一个新变量。换句话说,除非您按照上一段所述显式创建新副本,否则全局变量不会改变任何内容。
我也想知道如果您认为创建副本会损害性能,那么您的类会有多重?它的重量是多少MB?
更新
本系列文章详细介绍了堆和堆栈是什么:http://www.c-sharpcorner.com/uploadfile/rmcochran/csharp_memory01122006130034pm/csharp_memory.aspx
By your question I suspect that you do not understand how variables to classes work. You do not need a global variable to only have one copy of your object. All variables will point on exactly the same object unless you
Clone
it or create a new one with the old one as prototype.A global variable will in other words change nothing unless you explicitly create new copies as described in the previous paragraph.
I do also wonder how heavy your class can be if you think that the performance would be hurt by creating copies of it? How many mb do it weight?
Update
This article series describes in great detail what the heap and stack is: http://www.c-sharpcorner.com/uploadfile/rmcochran/csharp_memory01122006130034pm/csharp_memory.aspx
它是安全的,但您必须同步对变量的访问,例如使用
lock
语句。请参阅MSDN 库。
It is safe, but you have to synchronize access to the variables, e.g. by using the
lock
statement.See "lock Statement" in the MSDN library.
不,不是,除非您在使用其任何数据字段时都使用
lock(object) { }
锁定它。No, it is not, unless you're locking it with
lock(object) { }
whenever you use any of its data fields.如果您不修改任何字符串或变量,则不需要锁定。
我还会考虑将其设为静态类如果数据在整个应用程序中共享——那么您将不需要传递实例。
如果您需要修改或更新数据 - 使用 锁定。
If you're not modifying any of the strings or variables then you don't need to lock.
I'd also consider making this a static class if the data is shared across the whole application -- then you won't need to pass an instance.
If you need modify or update the data -- use Lock.
您还可以使用
[ThreadStatic]
。该变量的值对于每个线程都是唯一的。请参阅 MSDN 了解如何使用它。You may also use
[ThreadStatic]
. The value of the variable will be unique for each thread. See MSDN for how to use it.