C#应用程序中的计时器阻止主UI。 [英] Timer in C# application is blocking main UI.

查看:64
本文介绍了C#应用程序中的计时器阻止主UI。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在使用一个C#应用程序。此应用程序与设备建立连接。

在该应用程序中,存在一个监视器功能。用户可以从连接的设备和有害生物中选择几个参数来监视窗口(C#)并开始监视(更新监视器表单以获取新值)并记录(将这些参数写入一个.csv文件中)。

我在监视器表单上使用了一个form.timer并执行此功能

Hi,

I am working on one C# application. This application does connection with device.
In that application one monitor functionality is present . User can select few parameters from connected device and pest in to monitor window (C# from) and starts monitoring (update monitor form for new values) and logging (write these parameters in one .csv file).
I have used one form.timer on monitor form and doing this functionality

private void Sampling_Timer_Tick(object sender, EventArgs e)
        {
for (int iCount = 0; iCount < SamplingParameterList.Length; iCount++) //SamplingParameterList.Length mostly have 100+ value
               {
//read parameter value and display it in monitor list.
}
//write values in .csv file
        }





但是当我启动计时器时,我的应用程序变慢了。我认为UI线程被阻止了。 (如果我点击任何复选框,那么在该复选框应用程序上获得选中标记需要30秒)



我需要这个计时器才能在准确的时间间隔内工作。意味着如果我将间隔设置为1秒,那么日志文件中的每一行都应该只在一秒内被捕获,用户也应该可以在其他表格上工作。



please帮助我。



But when I start its timer, my application becomes slow. I think UI thread gets blocked. (if I click on any check box then for getting checked mark on that check box application takes 30 sec)

I need this timer to work on accurate interval. Means if I set interval at 1 second then each line in log file should get captured at one second only and user should be able to work on other forms also.

please help me on this.

推荐答案

你可以在你的tick事件中开始一个新的线程。

那样你将花很少的时间阻止GUI。



你可以在这里开始阅读线程:线程化(C#和Visual Basic) [ ^ ]



这是一个简短的例子:

You can start a new thread inside your tick event.
That way you will spend very little time blocking the GUI.

You can start reading about threads here: Threading (C# and Visual Basic)[^]

And here is a short example:
using System.Threading;

private void Sampling_Timer_Tick(object sender, EventArgs e)
{
    Thread worker = new Thread(new ThreadStart(DoSomething));
    worker.Name = "Doer";
    worker.IsBackground = true;
    worker.Start();
}

private void DoSomething()
{
    Stopwatch sw = new Stopwatch();
    sw.Start();
    
    // Do your stuff here, but keep the process time below your tick time
    // (1 second)
    
    // Also if you want to update, beware of cross thread isssues
    // Loo into how to use .BeginInvoke

    sw.Stop();
    string elapsedTime = sw.ElapsedMilliseconds.ToString();
    // Present the elapsed time in the GUI
}



[更新]

我添加了一个秒表,可用于衡量你在工人方法中花费的时间。

如果你从设备上花了超过一秒的读数值,你有一点问题,因为下一个计时器事件会在你从你的设备读完之前触发。

你基本上有两种方法可以解决这个困境:


[UPDATE]
I added a Stopwatch that can be used to measure the time you spend in the worker method.
If you spend more than one second reading values from the device you have a little problem, as the next timer event will trigger before you have finished reading from your device.
You basically have two ways to solve this dilemma:

1. Read fewer parameters, if that is possible.
2. Make the time between events long enough for all the parameters to be read.





添加锁定的解决方案从长远来看,这将无法正常工作,因为您很快会堆积一个请求从设备读取的队列,这个队列只会越来越长,直到资源耗尽为止。





还有其他几种方法可以达到相同的效果,但开始时这很简单。

例如, BackgroundWorker 也可以使用。


这篇关于C#应用程序中的计时器阻止主UI。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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