How do I let main thread suspend and wait for the System.Timer.Timer running

Posted by Kelvin on Stack Overflow See other posts from Stack Overflow or by Kelvin
Published on 2010-05-20T03:24:55Z Indexed on 2010/05/20 3:40 UTC
Read the original article Hit count: 236

Filed under:

Hi I am using System.Timer.Timer

I always get NULL after running my programme and it only works if I add this.sleep(6000). Suppose the reason is the main thread ends but the timer hasn't finished ... Here is the class and I call the class from my main form.

Class class1
{
  string finalResult = "";
  public string getNumber()
  {
    RunTimer();  
    return finalResult;
  }
  pubic void RunTimer ()
  {
    timer = new System.Timers.Timer(30000);
    timer.Interval = 1000;         
    timer.Elapsed += new System.Timers.ElapsedEventHandler(cal);
    timer.Start();
  }
  private void cal(object sender,System.Timers.ElapsedEventArgs e)
  {
    finalResult += READFROMCOMPORT;
  }
}

© Stack Overflow or respective owner

Related posts about c#