简体   繁体   English

事件是否在另一个线程中运行? (.Net Compact Framework)

[英]Is an Event running in another thread? (.Net Compact Framework)

I'm developing a Windows Mobile 5.0 or above with .Net Compact Framework 2.0 SP2 and C# . 我正在使用.Net Compact Framework 2.0 SP2C#开发Windows Mobile 5.0或更高 版本

when I try to access the control's width on a method that handles an event it throws me the following exception: 当我尝试在处理事件的方法上访问控件的宽度时,将引发以下异常:

Control.Invoke must be used to interact with controls created on a separate thread. 必须使用Control.Invoke与在单独线程上创建的控件进行交互。

Is this method running in another thread? 此方法是否在另一个线程中运行?

Thank you! 谢谢!

Yeah, controls cannot be accessed by threads that have not created them. 是的,尚未创建控件的线程无法访问控件。 Well, to be more accurate they can if you really want but you risk the application locking up "randomly" due to a deadlock. 好吧,更准确地说,如果您确实需要,它们可以这样做,但是您可能会因死锁而使应用程序“随机”锁定。

To get around this problem use the Invoke() or BeginInvoke() methods to set a callback for the "UI Thread" to pick up. 要解决此问题,请使用Invoke()或BeginInvoke()方法来设置“ UI线程”的回调。

eg 例如

private void HandleSomeEvent(object sender, EventArgs e)
{
    if(textBox1.InvokeRequired)
    {
        textBox1.BeginInvoke(new EventHandler(HandleSomeEvent), new object[]{sender, e});
    }
    else
    {
        textBox1.Text = "WIN!";
    }
}

An event runs on the thread that fired it. 事件在触发它的线程上运行。 So if the event was fired on a different thread than the one that created the control, it runs in a different thread, yeah. 因此,如果事件是在与创建控件的线程不同的线程上触发的,那么它将在不同的线程中运行,是的。

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

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