Timer Controll in Thread
-
Hello I m developing a window application. I m doing diferent tasks against different events using multi-threading. Against 1 thread , I m enabling the timer, and against its function I m performing 1 task, but it is not working i.e it is not executing the timer function. if I put all the code against some button rather than within thread , then it works well. plz tell me how could I use the Timer within the thread. here is my Code. private void Start_Click(object sender, EventArgs e) { pthrd = new Thread(new ThreadStart(Thrd1)); pthrd .Start(); } void Thrd1() { MessageBox.Show("Thread Start"); timer1.Enabled = true; } private void timer1_Tick(object sender, EventArgs e) { MessageBox.Show("Start Grabbing"); Calculate(); }
Shanzay
-
Hello I m developing a window application. I m doing diferent tasks against different events using multi-threading. Against 1 thread , I m enabling the timer, and against its function I m performing 1 task, but it is not working i.e it is not executing the timer function. if I put all the code against some button rather than within thread , then it works well. plz tell me how could I use the Timer within the thread. here is my Code. private void Start_Click(object sender, EventArgs e) { pthrd = new Thread(new ThreadStart(Thrd1)); pthrd .Start(); } void Thrd1() { MessageBox.Show("Thread Start"); timer1.Enabled = true; } private void timer1_Tick(object sender, EventArgs e) { MessageBox.Show("Start Grabbing"); Calculate(); }
Shanzay
DeepOceans, I am not sure if this is correct, but i think its because the thread does the work and then exits, thus the timer will be told to start but then instantly quick, thus it never ticks. Since i am not sure if my thinking is correct, i dont have a solution other than to keep the thread alive. Whats the reason you want to run a Timer in the background anyways?
Regards, Gareth. (FKA gareth111)
-
DeepOceans, I am not sure if this is correct, but i think its because the thread does the work and then exits, thus the timer will be told to start but then instantly quick, thus it never ticks. Since i am not sure if my thinking is correct, i dont have a solution other than to keep the thread alive. Whats the reason you want to run a Timer in the background anyways?
Regards, Gareth. (FKA gareth111)
I m using Timer to enable my function for grabbing the video frames on every tick. yes u might b right that thread does its work and exits but in my case it never actually goes in the function (i checked by printing a messegeBox). i already gave the code. plz try it and tell me more specific reason. Thanku so much !
Shanzay
-
Hello I m developing a window application. I m doing diferent tasks against different events using multi-threading. Against 1 thread , I m enabling the timer, and against its function I m performing 1 task, but it is not working i.e it is not executing the timer function. if I put all the code against some button rather than within thread , then it works well. plz tell me how could I use the Timer within the thread. here is my Code. private void Start_Click(object sender, EventArgs e) { pthrd = new Thread(new ThreadStart(Thrd1)); pthrd .Start(); } void Thrd1() { MessageBox.Show("Thread Start"); timer1.Enabled = true; } private void timer1_Tick(object sender, EventArgs e) { MessageBox.Show("Start Grabbing"); Calculate(); }
Shanzay
Your thread function should look something like this:
private void Thrd1()
{
// just sit and spin
while (true)
{
// sleep for the desired time - this example uses 1 second
Thread.Sleep(1000);Calculate(); }
}
private void Calculate()
{
// do some work
}BTW "Thrd1" is a crappy function name.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
Hello I m developing a window application. I m doing diferent tasks against different events using multi-threading. Against 1 thread , I m enabling the timer, and against its function I m performing 1 task, but it is not working i.e it is not executing the timer function. if I put all the code against some button rather than within thread , then it works well. plz tell me how could I use the Timer within the thread. here is my Code. private void Start_Click(object sender, EventArgs e) { pthrd = new Thread(new ThreadStart(Thrd1)); pthrd .Start(); } void Thrd1() { MessageBox.Show("Thread Start"); timer1.Enabled = true; } private void timer1_Tick(object sender, EventArgs e) { MessageBox.Show("Start Grabbing"); Calculate(); }
Shanzay
The Timer uses a thread, so you needn't use your own as well.
-
The Timer uses a thread, so you needn't use your own as well.
But using a thread is more programmerish. The Timer class is for lazy people. :)
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
But using a thread is more programmerish. The Timer class is for lazy people. :)
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001Certainly, but using both at once is nuts.
-
Certainly, but using both at once is nuts.
Agreed.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001