簡體   English   中英

從Windows窗體應用程序從另一個沒有GUI的類中調用計時器滴答事件

[英]Calling a timer tick event from Windows Form Application from another class with no GUI

我正在嘗試運行計時器滴答事件,該事件最初在我的Windows窗體上運行,也使用線程加載到另一個類上時也是如此。 我試圖在沒有幫助的另一個線程上調用計時器事件。 我應該為該線程使用相同的計時器還是創建新的計時器。 這是我當前的實現:

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(); 
    }



   }
}

但是計時器滴答事件會在啟動應用程序后立即啟動,而不是在單擊按鈕時開始-私有無效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();
    }
   }
}

當嘗試上述方法時,不會在新線程上觸發計時器。 有什么建議嗎?

沒有任何阻塞代碼或循環,您的線程將無法長期生存。 以下內容每隔一秒鍾調用一次您的測試方法,並且不使用計時器

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM