Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在不暂停GUI的情况下在计时器内运行不同的计时器?_C#_Timer - Fatal编程技术网

C# 如何在不暂停GUI的情况下在计时器内运行不同的计时器?

C# 如何在不暂停GUI的情况下在计时器内运行不同的计时器?,c#,timer,C#,Timer,我有一个SendCountingInfo()类,它将每5分钟向服务器发送一条消息。此类中的代码是: public void StartSendCountingInfo() { DoStartSendCountingInfo(300000); } private void DoStartSendCountingInfo(int iMiSecs) { _pingTimer = new System.Timers.Timer(iMiSecs); _pingTime

我有一个SendCountingInfo()类,它将每5分钟向服务器发送一条消息。此类中的代码是:

public void StartSendCountingInfo()
{
      DoStartSendCountingInfo(300000);
}

private void DoStartSendCountingInfo(int iMiSecs)
{
      _pingTimer = new System.Timers.Timer(iMiSecs);
      _pingTimer.Elapsed += new System.Timers.ElapsedEventHandler(pingTimer_Elapsed);
      _pingTimer.Start();
}

void pingTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
    PingRemoteHost();
}
当我试图在Windows窗体类中调用它时,它不起作用。 但是,当我删除计时器并直接调用PingRemoteHost()时,它就工作了。但是,表单没有正确加载。它显示空白屏幕,但PingRemoteHost()方法有效

以下是windows窗体中的代码:

private void Layout_Load(object sender, EventArgs e)
{
     tSystemChecker = new System.Timers.Timer(1000);
     tSystemChecker.Elapsed += new System.Timers.ElapsedEventHandler(tSystemChecker_Elapsed);
     tSystemChecker.Start();

     this.WindowState = FormWindowState.Maximized;
}

void tSystemChecker_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
    UIThreadWork(this, delegate
    {
        try
        {
             SuspendLayout();
             DoCheckHardwareStatus();
             DoCheckLanguage();

             SendCountingInfo sci = new SendCountingInfo();
             sci.StartSendCountingInfo();
        }
        catch (Exception exp)
        {
            System.Diagnostics.Debug.WriteLine(exp.Message);
            System.Diagnostics.Debug.WriteLine(exp.Source);
            System.Diagnostics.Debug.WriteLine(exp.StackTrace);
        }

        ResumeLayout(true);
   });
}

你知道出了什么问题吗?

使用一个线程,看看问题是否仍然存在

using System.Threading;
//Put this where you want to start the first timer
Thread thread = new Thread(dowork =>
{
    public void StartSendCountingInfo();
}
如果要更新GUI,请使用控件

guicontrol.BeginInvoke((MethodInvoker)delegate() 
{ 
    guicontrol.Text = "aa";
    //etc
});