在C#中,伺候mainthread同时继续处理用户界面更新吗? (.NET CF 2.0) [英] In C#, wait on the mainthread while continuing to process UI updates? (.NET 2.0 CF)

查看:96
本文介绍了在C#中,伺候mainthread同时继续处理用户界面更新吗? (.NET CF 2.0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在主线程中以其他方式阻止代码的执行,同时仍然允许显示用户界面的变化。

I want to otherwise block code execution on the main thread while still allowing UI changes to be displayed.

我试着拿出一个简化的例子的版本是什么我试图做的;这是我能拿出最好的。显然,这并不能说明我想要的行为,否则我不会张贴的问题。我只希望它给一些代码方面来支持我,我希望能解决这个问题的解释差

I tried to come up with a simplified example version of what I'm trying to do; and this is the best I could come up with. Obviously it doesn't demonstrate the behavior I'm wanting or I wouldn't be posting the question. I just hope it gives some code context to back my poor explanation of the problem I'm hoping to solve.

中的窗体我有这样的一个按钮单击处理程序:

Within a button click handler on a form I have this:

    private void button2_Click(object sender, EventArgs e)
    {
        AutoResetEvent autoResetEvent = new AutoResetEvent(false);

        new Thread(delegate() 
        {
            // do something that takes a while.
            Thread.Sleep(1000);

            // Update UI w/BeginInvoke
            this.BeginInvoke(new ThreadStart(
                delegate() { 
                    this.Text = "Working... 1";
                    this.Refresh();
                    Thread.Sleep(1000); // gimme a chance to see the new text
                }));

            // do something else that takes a while.
            Thread.Sleep(1000);

            // Update UI w/Invoke
            this.Invoke(new ThreadStart(
                delegate() {
                    this.Text = "Working... 2";
                    this.Refresh();
                    Thread.Sleep(1000); // gimme a chance to see the new text
                }));

            // do something else that takes a while.
            Thread.Sleep(1000);

            autoResetEvent.Set();
        }).Start();


        // I want the UI to update during this 4 seconds, even though I'm 
        // blocking the mainthread
        if (autoResetEvent.WaitOne(4000, false))
        {
            this.Text = "Event Signalled";
        }
        else
        {
            this.Text = "Event Wait Timeout";
        }
        Thread.Sleep(1000); // gimme a chance to see the new text
        this.Refresh();
    }

如果我没有设置timout对WaitOne的()的应用程序将死锁的调用()调用。

If I didn't set a timout on the WaitOne() the app would deadlock on the Invoke() call.


至于为什么我愿意做到这一点,我一直在负责移动应用程序做的工作在后台线程中的一个子系统,但仍然有它阻止用户的工作流程(主线程),只是有时和某种类型的工作相关的只有子系统。

As to why I'd want to do this, I've been tasked with moving one subsystem of an app to do work in a background thread, but still have it block user's workflow (the main thread) only sometimes and for certain types of work related to that subsystem only.

推荐答案

我的东西去了我还没有看到尚未公布这是使用MessageQueues。

I went with something I haven't seen posted yet which is to use MessageQueues.


  • 的MainThread块在等待队列上的下一条消息。

  • 后台线程职位不同类型的消息到的MessageQueue的。

  • 有些消息类型信号的MainThread更新UI元素。

  • 当然,还有一个消息要告诉MainThread停止阻塞和等待消息。

  • The MainThread blocks while waiting for the next message on a queue.
  • The background thread posts different types of messages to the MessageQueue.
  • Some of the message types signal the MainThread to update UI elements.
  • Of course, there is a message to tell the MainThread to stop blocking and waiting for messages.

在似乎考虑到窗口消息循环已经存在某处顶部,但它的工作原理。

Seems over the top considering the windows message loop already exists somewhere, but it works.

这篇关于在C#中,伺候mainthread同时继续处理用户界面更新吗? (.NET CF 2.0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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