在没有“交叉线程"的情况下,我可以从BackgroundWorker访问什么? [英] What can I access from a BackgroundWorker without "Cross Threading"?

查看:72
本文介绍了在没有“交叉线程"的情况下,我可以从BackgroundWorker访问什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我意识到我无法从BackgroundWorker的DoWork事件处理程序访问Form控件. (如果我尝试这样做,我将得到一个异常,如预期的那样.)

I realise that I can't access Form controls from the DoWork event handler of a BackgroundWorker. (And if I try to, I get an Exception, as expected).

但是,我可以访问表单上存在的其他(自定义)对象吗?

However, am I allowed to access other (custom) objects that exist on my Form?

例如,我创建了一个"Settings"类,并在我的Form中实例化了它,我似乎能够读取和写入其属性.

For instance, I've created a "Settings" class and instantiated it in my Form and I seem to be able to read and write to its properties.

这行得通吗?

如果我有一个静态课程怎么办?我可以安全地访问它吗?

What if I had a static class? Would I be able to access that safely?

推荐答案

@Engram:

您已经掌握了它的要点-CrossThreadCalls只是MS插入.NET Framework的一个不错的功能,可以防止骨头"式的并行编程错误.我猜您已经发现,可以通过在 class 上设置"AllowCrossThreadCalls"属性(而不是在该类的实例上,例如设置Label.AllowCrossThreadCalls和不是lblMyLabel.AllowCrossThreadCalls).

You've got the gist of it - CrossThreadCalls are just a nice feature MS put into the .NET Framework to prevent the "bonehead" type of parallel programming mistakes. It can be overridden, as I'm guessing you've already found out, by setting the "AllowCrossThreadCalls" property on the class (and not on an instance of the class, e.g. set Label.AllowCrossThreadCalls and not lblMyLabel.AllowCrossThreadCalls).

但更重要的是,您对使用某种锁定机制的需求是正确的.每当您有多个执行线程(无论是线程,进程还是其他线程)时,都需要确保当一个线程对变量进行读/写操作时,您可能不希望其他线程进行插入和更改.第一根线的脚.

But more importantly, you're right about the need to use some kind of locking mechanism. Whenever you have multiple threads of execution (be it threads, processes or whatever), you need to make sure that when you have one thread reading/writing to a variable, you probably don't want some other thread barging and changing that value under the feet of the first thread.

.NET Framework实际上提供了几种其他机制,根据情况的不同,这些机制可能比锁定代码更有用.第一种是使用 Monitor 类,该类具有锁定特定 object 的作用.使用此方法时,只要其他线程不尝试锁定同一对象,它们就可以继续执行. Mutex (或信号灯). Mutex基本上就像是在线程之间捕获标志的游戏.如果一个线程抓住了该标志,则直到第一个线程将其删除之前,其他任何线程都无法抓住它. (信号量就像互斥对象,只是游戏中可以有多个标志.)

The .NET Framework actually provides several other mechanisms which might be more useful, depending on circumstances, than locking in code. The first is to use a Monitor class, which has the effect of locking a particular object. When you use this, other threads can continue to execute, as long as they don't try to lock that same object. Another very useful and common parallel-programming idea is the Mutex (or Semaphore). The Mutex is basically like a game of Capture the Flag between your threads. If one thread grabs the flag, no other threads can grab it until the first thread drops it. (A Semaphore is just like a Mutex, except that there can be more than one flag in a game.)

显然,这些概念都不能解决每个特定的问题-但有一天,有更多工具可以帮助您解决问题:)

Obviously, none of these concepts will work in every particular problem - but having a few more tools to help you out might come in handy some day :)

这篇关于在没有“交叉线程"的情况下,我可以从BackgroundWorker访问什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆