0

我正在开发一个 Windows 窗体应用程序。它在运行应用程序时触发 Tick 事件。private void tmrDisplay_Tick(object sender, EventArgs e) - 当一个线程启动时,方法应该被另一个类调用。有关如何从另一个类调用相同的滴答事件的任何帮助 - Class1 c?万分感谢

  namespace XT_3_Sample_Application
 {
   public partial class Form1 : Form
    {
    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(); 

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



    }

    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)
    {

        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(); 
    }
    static void threadcal()
    {
        Class1 c = new Class1();
        c.test("192.168.2.235",9999);

      }
  }
}
4

1 回答 1

2

只要您有对计时器类的引用,您就可以公开一个可以调用的公共刻度方法。然后在计时器的事件中使用它而不是复制代码

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

public void Tick()
{
    if (receivedDataList.Count > 0)
    {
        string RAW_Str = receivedDataList.Dequeue();
        //tbxConsoleOutput.AppendText(RAW_Str + Environment.NewLine);
        tbxConsoleOutput.AppendText(Parser_Selection(RAW_Str) + Environment.NewLine);
    }
}
于 2012-11-20T02:51:45.587 回答