Tag: 线程安全

锁定一个实习的字符串?

更新:如果此方法不是线程安全的,这是可以接受的,但我有兴趣学习如何使其线程安全。 另外,如果可以避免,我不想为所有key锁定单个对象。 原始问题:假设我想编写一个更高阶的函数,它接受一个键和一个函数,并检查一个对象是否已使用给定的密钥进行缓存。 如果是,则返回缓存的值。 否则,运行给定的函数并缓存并返回结果。 这是我的代码的简化版本: public static T CheckCache(string key, Func fn, DateTime expires) { object cache = HttpContext.Current.Cache.Get(key); //clearly not thread safe, two threads could both evaluate the below condition as true //what can I lock on since the value of “key” may not be known at compile time? if (cache == null) […]

C#Winforms线程:调用Closed Form

以下代码说明了我的困境。 代码创建一个处理内容的后台线程,然后使用结果调用UI线程。 如果后台线程在窗体关闭后调用窗体上的Invoke,它可能会抛出exception。 它在调用Invoke之前检查IsHandleCreated,但是在检查之后表单可能会关闭。 void MyMethod() { // Define background thread Action action = new Action( () => { // Process something var data = BackgroundProcess(); // Try to ensure the form still exists and hope // that doesn’t change before Invoke is called if (!IsHandleCreated) return; // Send data to UI thread for processing […]

从后台线程更新UI

这只是一个奇怪的问题。 哪一个是从另一个线程更新UI的最佳方式。 首先,这一个: private delegate void MyDelegateMethod(); void MyMethod() { if (unknowncontrol.InvokeRequired) { this.BeginInvoke(new MyDelegateMethod(MyMethod)); return; } unknowncontrol.property = “updating!”; } 另一方面: Invoke((System.Threading.ThreadStart)delegate() { unknowncontrol.property = “updating!”; }); 或者,有更好的方法吗? 当然这是针对WinForms的,对于WPF来说是调度员。 WPF的代码如何? 我问,’因为,过去我在使用上述两个选项从引发的事件更新UI时遇到了错误。 类似的错误:“没有可用的源代码”。 我假设我们所有人都见过他们:D。 感谢,并有一个愉快的一天!

C#数组中的线程安全性

有2个不同的线程: 一个从C#数组读取(例如从第一个位置读取), 另一个写入相同的C#数组但写入不同的位置(例如到最后一个位置) 线程安全与否? (我的意思是这里没有锁定阅读或写作)

C#:线程安全事件

下面的实现是否是线程安全的? 如果不是我错过了什么? 我应该在某处使用volatile关键字吗? 或OnProcessingCompleted方法中的某个锁? 如果是这样,在哪里? public abstract class ProcessBase : IProcess { private readonly object completedEventLock = new object(); private event EventHandler ProcessCompleted; event EventHandler IProcess.ProcessCompleted { add { lock (completedEventLock) ProcessCompleted += value; } remove { lock (completedEventLock) ProcessCompleted -= value; } } protected void OnProcessingCompleted(ProcessCompletedEventArgs e) { EventHandler handler = ProcessCompleted; if (handler […]

这是在C#中引发事件的有效模式吗?

更新 :为了让读者阅读此内容的好处,自.NET 4以来,由于自动生成事件的同步更改,锁定是不必要的,所以我现在就使用它: public static void Raise(this EventHandler handler, object sender, T e) where T : EventArgs { if (handler != null) { handler(sender, e); } } 并提出它: SomeEvent.Raise(this, new FooEventArgs()); 在阅读了Jon Skeet 关于multithreading的文章之后 ,我试图将他提倡的方法封装在一个像这样的扩展方法中引发事件(使用类似的通用版本): public static void Raise(this EventHandler handler, object @lock, object sender, EventArgs e) { EventHandler handlerCopy; lock (@lock) { handlerCopy = […]

为什么锁在不同的对象上执行?

可能重复: 锁(锁柜)和锁(variable_which_I_am_using)之间的区别 在我见过的所有“线程安全”代码示例中,它们锁定了一个单独的虚拟对象。 为什么不能直接对有问题的数据执行锁定?

System.Timers.Timer与System.Threading.Timer的线程安全性

在本文中: http : //msdn.microsoft.com/en-us/magazine/cc164015.aspx作者声明System.Threading.Timer不是线程安全的 。 从那以后,在Rich的书“CLR via C#”上的博客上重复了这一点,但这绝不合理。 此外, MSDN文档确保“此类型是线程安全的”。 1)谁说实话? 2)如果这是原始文章是什么使System.Threading.Timer不是线程安全的,它的包装器System.Timers.Timer如何实现更multithreading安全? 谢谢

C#代理是否是线程安全的?

如果你有一个带有委托成员变量的类实例,并且多个线程调用该委托(假设它指向一个长期运行的方法),是否存在任何争用问题? 你是否需要锁定委托,或者每个线程调用委托所指向的方法是否安全,因为每个线程都有自己的调用堆栈?

这是否使用静态队列线程安全?

msdn文档声明静态通用队列是线程安全的。 这是否意味着以下代码是线程安全的? 换句话说,当一个线程将一个int排队并且另一个线程同时将一个int排队时,是否存在问题? 我是否必须为线程安全锁定Enqueue和Dequeue操作? class Test { public static Queue queue = new Queue(10000); Thread putIntThread; Thread takeIntThread; public Test() { for(int i = 0; i < 5000; ++i) { queue.Enqueue(0); } putIntThread = new Thread(this.PutInt); takeIntThread = new Thread(this.TakeInt); putIntThread.Start(); takeIntThread.Start(); } void PutInt() { while(true) { if(queue.Count 0) {//no need to lock here […]