从没有GUI的另一个类调用Windows窗体应用程序中的计时器刻度事件

时间:2012-11-21 03:02:49

标签: c# windows timer

我正在尝试运行最初在我的Windows窗体上运行的计时器刻度事件,当使用线程加载到另一个类时也是如此。我尝试在另一个没有帮助的线程上调用timer事件。我应该使用相同的计时器或为该线程创建一个新的计时器。这是我目前的实施:

namespace XT_3_Sample_Application
{
      public partial class Form1 : Form
      {

       Queue<string> receivedDataList = new Queue<string>();



       System.Timers.Timer tmrTcpPolling = new System.Timers.Timer(); 

       public Form1()
       {
        InitializeComponent();
        TM702_G2_Connection_Initialization();

         }

         static void threadcal()
         {
        Class1 c = new Class1();
        c.timer_start();
        c.test("192.168.1.188",9999);

        }


       public string Connection_Connect(string TCP_IP_Address, int TCP_Port_Number)
          {

                if (tcpClient.Connected)
                {
                    Socket_Client = tcpClient.Client;
                    TcpStreamReader_Client = new StreamReader(tcpClient.GetStream(), Encoding.ASCII);
                    tmrTcpPolling.Start();
                    Status = "Connected\r\n";
                }
                else
                {
                    Status = "Cannot Connect\r\n";
                }



        }

       public string Connection_Disconnect()
       {
           tmrTcpPolling.Stop();

          // do something

          return "Disconnected\r\n";
      }

     void TM702_G2_Connection_Initialization()
    {
        tmrTcpPolling.Interval = 1;
        tmrTcpPolling.Elapsed += new ElapsedEventHandler(tmrTcpPolling_Elapsed);
    } 


    #region Timer Event
    void tmrTcpPolling_Elapsed(object sender, ElapsedEventArgs e)
    {
        try
        {
            if (tcpClient.Available > 0)
            {

                receivedDataList.Enqueue(TcpStreamReader_Client.ReadLine());
            }
        }
        catch (Exception)
        {

            //throw;
        }
    }

    private void tmrDisplay_Tick(object sender, EventArgs e)
    {
        Tick();
    }

    public void Tick()
    {
        Console.Write("tick" + Environment.NewLine);
        if (receivedDataList.Count > 0)
        {
            string RAW_Str = receivedDataList.Dequeue();
            //tbxConsoleOutput.AppendText(RAW_Str + Environment.NewLine);
            tbxConsoleOutput.AppendText(Parser_Selection(RAW_Str) + Environment.NewLine);
        }

    }

    #endregion

    private void btnConnect_Click(object sender, EventArgs e)
    {
        tbxConsoleOutput.AppendText(Connection_Connect(tbxTCPIP.Text, Convert.ToInt32(tbxPort.Text, 10)));
        Thread t = new Thread(threadcal);
        t.Start(); 
    }



   }
}

但是计时器tick事件在应用程序启动时启动,但在按钮点击时没有启动 - private void btnConnect_Click(object sender,EventArgs e)。 我正在尝试为类Class1的测试方法调用一个单独的线程。我正在尝试使用类似的计时器事件来接收来自服务器的输出,用于此线程。

 namespace XT_3_Sample_Application
 {
   class Class1
   {

    TcpClient tcpClient;
    Socket Socket_Client;
    StreamReader TcpStreamReader_Client;    // Read in ASCII
    Queue<string> receivedDataList = new Queue<string>();
    System.Timers.Timer tmrTcpPolling = new System.Timers.Timer();



    void TM702_G2_Connection_Initialization()
    {
        tmrTcpPolling.Interval = 1;
        tmrTcpPolling.Elapsed += new ElapsedEventHandler(tmrTcpPolling_Elapsed);
    } 
    public void test(string TCP_IP_Address, int TCP_Port_Number)
    {
        TM702_G2_Connection_Initialization();


        try
        {
            string Status = "";
            Ping pingSender = new Ping();
            PingOptions options = new PingOptions();




            PingReply reply = pingSender.Send(TCP_IP_Address, timeout, buffer, options);

            if (reply.Status == IPStatus.Success)
            {
                tcpClient = new TcpClient();
                tcpClient.Connect(TCP_IP_Address, TCP_Port_Number);

                if (tcpClient.Connected)
                {
                    Socket_Client = tcpClient.Client;
                    TcpStreamReader_Client = new StreamReader(tcpClient.GetStream(), Encoding.ASCII);
                    tmrTcpPolling.Start();
                    Status = "Connected\r\n";
                }
                else
                {
                    Status = "Cannot Connect\r\n";
                }
            }
            else
            {
                Status = "Ping Fail\r\n";
            }

            MessageBox.Show(TCP_IP_Address + " :" + Status);
        }
        catch (System.Exception ex)
        {
            MessageBox.Show(TCP_IP_Address + " :" +  ex.Message);
        }


        setFilterType();

        setButtonRadioLvl();
        heloCmd();



    }
    public void timer_Start()
    {
    Form1 f = new Form1();
    f.Tick();
    }
   }
}

尝试上述方法时,不会在新线程上触发计时器。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

没有任何阻止代码或循环,您的线程将不会长寿。以下每隔一秒调用一次测试方法,不使用计时器

    static void threadcal()
    {
        while (true)
        {
            Thread.Sleep(1000);
            Class1 c = new Class1();
            c.test("192.168.1.188", 9999);
        }
    }