Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8861911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:35:59+00:00 2026-06-14T15:35:59+00:00

I’m trying to run the timer tick event which initially runs on my windows

  • 0

I’m trying to run the timer tick event which initially runs on my windows form,also when loaded on another class using a thread. I tried calling the timer event on another thread it didn’t help. Am I supposed to use the same timer or create a new timer for that thread. This is my current implementation:

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



   }
}

But the timer tick event starts the moment the application is launched but not on button click – private void btnConnect_Click(object sender, EventArgs e).
I’m trying to call a separate thread for the class Class1’s test method. I’m trying to use a similar timer event to receive output from the server, for this thread.

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

When tried the above method the timer is not fired on the new thread. Any suggestions on this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T15:36:01+00:00Added an answer on June 14, 2026 at 3:36 pm

    Without any blocking code or loop your thread will not live long. the following calls your test method every one second and doesn’t use timer

        static void threadcal()
        {
            while (true)
            {
                Thread.Sleep(1000);
                Class1 c = new Class1();
                c.test("192.168.1.188", 9999);
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I would like to run a str_replace or preg_replace which looks for certain words
I'm trying to select an H1 element which is the second-child in its group
I have a text area in my form which accepts all possible characters from
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am doing a simple coin flipping experiment for class that involves flipping a
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.