Skip to content

Simple Threading

j_sen21

Asked Mar 27, 2008, 6:34 AM EDT1 answer
Share
LinkedInX

Have forgotten my ways with threading!

I just want to make sure the thread will stop after the call. This code executes under the timer1. Im assuming that after the thread is created and the function finishes, the thread terminated itself. I wated process under ctrl,alt, delete and I am fairly confident it is working.

Am I correct, ( Im worried Im creating a problem down the road)???

Thanks

TypeScript
private void timer1_Tick(object sender, EventArgs e)
{
Thread t = new Thread(runningTimer);
t.Start();


}
private void runningTimer()

{
System.Net.WebRequest req = System.Net.WebRequest.Create(grate.ToString() + "?wsdl");
try
{
m_wr = req.GetResponse();
m_wr.Close();
}
catch{}
if (m_wr != null)
{

bconnected = true;

Code
}
else
{
bconnected = false;
}
m_wr = null;
}

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Personaly I don't suggest the use of timer. It may slow down your application depending on what it's doing. Instead you can use a delay and recall the Void.

    System.Threading.Thread t; //You must declare this to work properly

    t = new System.Threading.Thread(new System.Threading.ThreadStart(runningTimer));
    t.Start();

    And you will be able to stop with with abort command. It's obsolete but works fine with me. And I couldn't learn another way yet.

    t.Abort();

    You can use a Void which I got and instead of writing the big code for delay at thread just call Delay and say the delay. Call it as Delay(number);

    void Delay(int delayInMilliseconds)
    {
    System.Threading.Thread.Sleep(delayInMilliseconds);
    }

    private void runningTimer()
    {
       System.Net.WebRequest req = System.Net.WebRequest.Create(grate.ToString() + "?wsdl");
       try
       {
          m_wr = req.GetResponse();
          m_wr.Close();
       }
       catch{}
       if (m_wr != null)
       {
          bconnected = true; 
       }
       else
       {
          bconnected = false;
       }
       m_wr = null;
       Delay(timer1.Interval); //Change this to the time that you want. Used delay of timer1 for now.
       runningTimer();
    }

    0