如何在表单应用程序后面添加计时器或线程 [英] How to add a timer or a thread behind a form application

查看:68
本文介绍了如何在表单应用程序后面添加计时器或线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我有一个基于表单的应用程序,我想在事件发生时更新表单的内容,所以我尝试了两件事: br />
1)开始一个线程,它将持续监控是否检测到事件?如果已发生事件,则表单内容将更新。这个过程不断发生。

2)启动一个计时器,在每个计时器上监视事件。



问题是这两种方法都导致应用程序挂起。出现白色屏幕,表格被挂起。所以我需要做这样的事情,我可以在窗体中执行所有操作,并在后台进行并行处理。



非常感谢

Hello,

I have a form based application in which i want to update the contents of a form when an event occurs so i tried out two things:
1) Started a thread which will continuously monitor if an event has been detected or not? if event has occurred then the contents of the form will be updated. This process is continuously taking place.
2) Started a Timer which in which on every tick the event is monitored.

The problem is that both these methods are causing the application to hang. A white screen appears and the form gets hanged. So i need to do such a thing where i can do all the operations in a form and parallel processing in the background takes place.

Thanks a lot

推荐答案

我要处理的方法是使用一个线程,而不是一个计时器,并建立连接并维护它直到应用程序关闭:不要打开和关闭连接,保持打开。



可能,我会滥用BackgroundWorker和它的ReportProgress事件,用于传输指示状态的枚举值。

The way I would handle this is to use a thread, not a timer, and to establish a connection and maintain it until the application closes: don't open and close the connection, keep it open.

Probably, I would misuse a BackgroundWorker and it's ReportProgress event to transfer an enum value indicating the status.
public enum ResponseCode
   {
   None,
   TerraceDoorSensor,
   BeamDetector2,
   BeamDetector1,
   SmokeDetected,
   LPGGasLeakDetected,
   Unknown,
   }



创建一个名为exitNow的类级别bool,并创建后台worker:


Create a class level bool called exitNow, and create the background worker:

BackgroundWorker work = new BackgroundWorker();
work.WorkerReportsProgress = true;
work.ProgressChanged += new ProgressChangedEventHandler(work_ProgressChanged);
work.DoWork += new DoWorkEventHandler(work_DoWork);
exitNow = false;
work.RunWorkerAsync();





后台员工坐在一个循环中:



The background worker sits in a loop:

void work_DoWork(object sender, DoWorkEventArgs e)
    {
    BackgroundWorker work = sender as BackgroundWorker;
    if (work != null)
        {
        // Open the connection
        net = new Network("172.16.3.230");
        net.InitConnection();
        while (!exitNow)
            {
            net.send(st.create_read_packet());
            int deviceResponse = net.receive()[9];
            ResponseCode status = ResponseCode.None;
            switch (deviceResponse)
                {
                default: status = ResponseCode.Unknown; break;
                case 0x02: status = ResponseCode.TerraceDoorSensor; break;
                case 0x80: status = ResponseCode.BeamDetector1; break;
                ...
                }
            work.ReportProgress((int) status);
            System.Threading.Thread.Sleep(500);
            }
        //Close the connection.
        ...
        }
    }



报告进度事件显示:


And the report progress event does the display:

void work_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
    ResponseCode status = (ResponseCode)e.ProgressPercentage;
    switch (status)
        {
        case ResponseCode.None: break;
        case ResponseCode.BeamDetector1: MessageBox.Show("Beam detector1 event"); break;
        ...
        }
    }

(虽然我可能不会使用MEssageBox!)

当你关闭你的应用程序,在你的FormClosing事件中你将exitNow设置为true以允许工人自然死亡。

(Though I'd probably not use a MEssageBox!)
When you close your app, in your FormClosing event you set exitNow to true to allow the worker to die naturally.


这篇关于如何在表单应用程序后面添加计时器或线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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