简体   繁体   English

在后台工作线程中使用C#全局变量是否安全

[英]Is it safe to use C# global variables in a background worker thread

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. 我的问题是有一个名为UCSProject的繁重类,它包含许多字符串和List字段,我需要将此类的实例传递给后台worker,因为该类有点重,我想减少重复次数实例直接使用全局变量,而不是将其作为参数传递给后台工作者。

To make it short, I just want to know is it safe to access global variables from a background worker thread in C# 为了简短起见,我只想知道从C#中的后台工作线程访问全局变量是否安全

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 如果希望彼此修改对象,请使用Lock

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. 所有变量都将指向完全相同的对象,除非您Clone它或创建一个旧的变量作为原型。

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 本系列文章详细介绍了堆和堆栈的内容: http//www.c-sharpcorner.com/uploadfile/rmcochran/csharp_memory01122006130034pm/csharp_memory.aspx

It is safe, but you have to synchronize access to the variables, eg by using the lock statement. 它是安全的,但您必须同步对变量的访问,例如使用lock语句。

See " lock Statement " in the MSDN library. 请参阅MSDN库中的“ lock Statement ”。

不,它不是,除非您在使用任何数据字段时使用lock(object) { }锁定它。

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 . 如果您需要修改或更新数据 - 请使用Lock

You may also use [ThreadStatic] . 您也可以使用[ThreadStatic] The value of the variable will be unique for each thread. 每个线程的变量值都是唯一的。 See MSDN for how to use it. 有关如何使用它,请参阅MSDN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM