修改主线程外的控件(需要专业人员的理论确认)
【腾讯云】亏本大甩卖,服务器4核16G 1年370元(带宽12M,系统盘120GB SSD盘,月流量2000GB)!!!!!!
云产品 配置 价格
服务器 1核2G,带宽5M,系统盘50GB SSD盘,月流量500GB 38元/年
MySQL 1核1G 19元/年
服务器 16核32G,带宽18M,系统盘250GB SSD盘,月流量5000GB 1197元/年
点我进入腾讯云,查看更多详情

I have a winform with a form called MainForm. I have a static class called ObjMgr. In ObjMg, I have several other static classes, values, but Its not important now.

In the ObjMgr class, I also have a static void Pulse() method, that I populate my other static classes in the ObjMgr. This pulse method have to run very often, like 0,033 secs.

On MainForm I placed a start button, that on click event, starts pulsing my ObjMgr on another thread.

Pulser = new System.Threading.Thread(new System.Threading.ThreadStart(ObjMgr.StartPulsing));
Pulser.IsBackground = true;
Pulser.Start();

My ObjMgr.StartPulsing method source:

        while(true)
        {
            ObjMgr.Pulse();
            System.Threading.Thread.Sleep(30);
        }

My Pulser method source :

        //here I update all my data in ObjMgr
        // Its need to be fast, I have some while, and switch statements here.
        // complicated code here :D The main thing is, its populating my classes with data.

Now I have my pulser running , and keeping my static class data fresh. Its very nice. Next step, I would like to show some data on my form (UI), from the updated ObjMgr classes. On my form I have about ~20 labels, 2 ProgressBar values, I want to update often, like the Pulsator. (0,033sec)

It would be obvious to update my labels, from the Pulsator method it self, but Iam afraid it would slow down my Pulsator, and I dont want it. So, I need some tips, how to do this thing. I was thinking, and I implented timer, that I set the interval to 30, then on tick event I read my data from the ObjMgr, and show modify the labels. It working okey, but little buggy, because some synchron problems. If you understand my problem could you give me some feedback, or tips how to make this better?

IF I would try to update my labels from the Pulse() method itself, would It slow my Pulse() method? If not, how could I update 20 labels on MainForm, from another thread?

#0

Considering the timing criteria of your situation I would avoid initiating the UI updates from the worker thread. The reason is two fold. First, it is likely to have some impact on the timing characteristics of thread. Second, you would have to use an expensive marshaling operation (via Control.Invoke) to transfer the UI updating operation onto the UI thread.

Your best bet in this situation is package all of the information needed to perform the UI update in a single class and then store it in a shared variable. Then have your UI thread poll this shared variable at an interval appropriate for you application.1

This approach has several advantages.

  • The UI and worker threads remain loosly coupled as opposed to the Control.Invoke or Control.BeginInvoke approach which tightly couples them.
  • The UI thread will not impede the progress of the worker thread.
  • The worker thread cannot dominate the time the UI thread spends updating.
  • The intervals at which the UI and worker threads perform operations can remain independent.
  • The worker thread cannot overrun the UI thread's message pump.

1Updating the UI as quickly as you propose does not sound like a good idea. The UI is meant for human interaction and as such no human will care or even notice that it updates that quickly.

#1

IF I would try to update my labels from the Pulse() method itself, would It slow my Pulse() method?

First: No user can read 30 updates/sec, so better built in some throttling here. But only changing the Text properties of some Labels is very fast as long as you avoid forced Updating.

But then you still have to Invoke() the updates, and that puts you at the mercy of the Message-loop. It will usually be fast enough but your worst-case will probably be > 33 ms.

So the best way is to use Control.BeginInvoke(), you dispatch an update but don't wait for it to finish. Best make copies of the data (strings) you want to use because now your update will be concurrent with your Pulse.

// pseudo code
void Pulse()
{
    // your DTO, a simple string[] or a special object
    var data = new string[] { x1.text, x2.Text } ;
    MainForm.BeginInvoke(Updater, new object[] { data }); 
}

推荐文章

在复制Delphi字符串之前需要分配内存?

在复制Delphi字符串之前需要分配内存?

推荐文章

SQL/C中的营业时间#

SQL/C中的营业时间#

推荐文章

我能有在NUnit运行测试之前和之后执行的代码吗?

我能有在NUnit运行测试之前和之后执行的代码吗?

推荐文章

IoC:在基于web的应用程序中使用子容器的优势

IoC:在基于web的应用程序中使用子容器的优势

推荐文章

将依赖项属性绑定到另一个依赖项属性

将依赖项属性绑定到另一个依赖项属性

推荐文章

SQL Server 2008时间列弄乱了GridView数据格式字符串

SQL Server 2008时间列弄乱了GridView数据格式字符串

推荐文章

操纵变量的地址以存储较小的类型?

操纵变量的地址以存储较小的类型?

推荐文章

如何在HQL/GORM中表示“值在动态列表中的位置”?

如何在HQL/GORM中表示“值在动态列表中的位置”?

推荐文章

如何在WPF ScrollViewer中检查子控件剪辑?

如何在WPF ScrollViewer中检查子控件剪辑?

推荐文章

Mac OS X上的Qt库和源代码安装在哪里?

Mac OS X上的Qt库和源代码安装在哪里?

推荐文章

来自WCF数据服务的POX返回数据

来自WCF数据服务的POX返回数据

推荐文章

为C中的char*[64]分配(和取消分配)内存

为C中的char*[64]分配(和取消分配)内存

推荐文章

从后台线程更新解除分配的UIWebView

从后台线程更新解除分配的UIWebView

推荐文章

在Flex中,如何使复选框的标签与底部对齐?

在Flex中,如何使复选框的标签与底部对齐?

推荐文章

NetBeans中的“无建议”

NetBeans中的“无建议”

推荐文章

GWT/Java中等待多个异步事件完成的干净方法

GWT/Java中等待多个异步事件完成的干净方法