Thread vs Timer

  • Thread starter Thread starter MDB
  • Start date Start date
M

MDB

Hello All,

I have function that checks for messages every 30 seconds and was wondering
if it is better to use a thread or timer to do this?
 
All depends on what the check does and how you want to implement it. There
are also 2 different timers - Thread.Timer and Forms.Timer which are very
different in behavior.

-Chris
 
Here is what I am currently doing, I start the following thread that checks
for messages every 20 seconds. Although it checks for messages every 20
seconds, it only sleeps for 2 seconds that way if the thread was shut down,
it can shut down within 2 seconds. The timer is much cleaner however, I am
unsure if it is the better way.

I also experimented with the timer by using this code:

******TIMER******
private System.Windows.Forms.Timer timerHTTP;

// CHecks Messages Every 20 Seconds


timerHTTP.Interval = 20000;
timerHTTP.Enabled = true;


private void timerHTTP_Tick(object sender, EventArgs e)
{
PMMobile.Classes.Globals.gMessage.GetMessages(false);
}

******* THREAD*******

private void RunMessageThread()
{
int intMessageSleep = 20; //How long in secs before running again.
DateTime dtMessageSleep = DateTime.Now; //Set Inital DateTime

//Int TimeDifference
TimeSpan intTimeDifference;

//Check right away
myApp.Classes.Globals.gMessage.GetMessages(false);

while(myApp.Classes.Globals.gblMessageThread)
{
//Get time difference and check to see if >= intSleep Time
intTimeDifference = DateTime.Now - dtMessageSleep;

if(myApp.Classes.Globals.gblMessageThread && intTimeDifference.Seconds >=
intMessageSleep)
{
myApp.Classes.Globals.gMessage.GetMessages(false);
dtMessageSleep = DateTime.Now;
}

//Sleep for 2 seconds
Thread.Sleep(2000);

}

}
 
The answer depends on how long does your GetMessages call take. If it's
anything over 500 msec, I'd go for either a dedicated thread or
System.Threading.Timer. Pausing UI for over 1/2 sec is not a good idea -
especially on timer. When using threads or thread timers mind the need to
use Invoke to update UI
 
Thanks, I will stick with the thread then since it does take some time to
GetMessages.

Thanks Again
 
In addition to both other comments...

If I understood your scenario, I'd go for the Threading.Timer... No need to
wake the Thread up every two seconds (or have that timespan diff stuff)...
When the tmr calls you back check for your flag (gblMessageThread) before
GetMessages... only then restart the Timer (in other words on
creation/Change sets the timer's period to -1). At the place where you set
your flag to False also dispose of the timer.

If for any other reason you *must* use a Thread, combine it with a
ManualResetEvent instead of waking the thread every 2 seconds for no real
reason...

Cheers
Daniel


MDB said:
Thanks, I will stick with the thread then since it does take some time to
GetMessages.

Thanks Again


Alex Feinman said:
The answer depends on how long does your GetMessages call take. If it's
anything over 500 msec, I'd go for either a dedicated thread or
System.Threading.Timer. Pausing UI for over 1/2 sec is not a good idea -
especially on timer. When using threads or thread timers mind the need to
use Invoke to update UI

--
Alex Feinman
---
Visit http://www.opennetcf.org
MDB said:
Here is what I am currently doing, I start the following thread that
checks
for messages every 20 seconds. Although it checks for messages every 20
seconds, it only sleeps for 2 seconds that way if the thread was shut
down,
it can shut down within 2 seconds. The timer is much cleaner however, I
am
unsure if it is the better way.

I also experimented with the timer by using this code:

******TIMER******
private System.Windows.Forms.Timer timerHTTP;

// CHecks Messages Every 20 Seconds


timerHTTP.Interval = 20000;
timerHTTP.Enabled = true;


private void timerHTTP_Tick(object sender, EventArgs e)
{
PMMobile.Classes.Globals.gMessage.GetMessages(false);
}

******* THREAD*******

private void RunMessageThread()
{
int intMessageSleep = 20; //How long in secs before running again.
DateTime dtMessageSleep = DateTime.Now; //Set Inital DateTime

//Int TimeDifference
TimeSpan intTimeDifference;

//Check right away
myApp.Classes.Globals.gMessage.GetMessages(false);

while(myApp.Classes.Globals.gblMessageThread)
{
//Get time difference and check to see if >= intSleep Time
intTimeDifference = DateTime.Now - dtMessageSleep;

if(myApp.Classes.Globals.gblMessageThread &&
intTimeDifference.Seconds
=
intMessageSleep)
{
myApp.Classes.Globals.gMessage.GetMessages(false);
dtMessageSleep = DateTime.Now;
}

//Sleep for 2 seconds
Thread.Sleep(2000);

}

}


"Chris Tacke, eMVP" <ctacke[at]OpenNETCF_dot_org> wrote in message
All depends on what the check does and how you want to implement it.
There
are also 2 different timers - Thread.Timer and Forms.Timer which are very
different in behavior.

-Chris

"MDB" <nospam> wrote in message
Hello All,

I have function that checks for messages every 30 seconds and was
wondering
if it is better to use a thread or timer to do this?
 
Thanks


Daniel Moth said:
In addition to both other comments...

If I understood your scenario, I'd go for the Threading.Timer... No need to
wake the Thread up every two seconds (or have that timespan diff stuff)...
When the tmr calls you back check for your flag (gblMessageThread) before
GetMessages... only then restart the Timer (in other words on
creation/Change sets the timer's period to -1). At the place where you set
your flag to False also dispose of the timer.

If for any other reason you *must* use a Thread, combine it with a
ManualResetEvent instead of waking the thread every 2 seconds for no real
reason...

Cheers
Daniel


"MDB" <nospam> wrote in message news:[email protected]...
Thanks, I will stick with the thread then since it does take some time to
GetMessages.

Thanks Again
every
however,
I
am
unsure if it is the better way.

I also experimented with the timer by using this code:

******TIMER******
private System.Windows.Forms.Timer timerHTTP;

// CHecks Messages Every 20 Seconds


timerHTTP.Interval = 20000;
timerHTTP.Enabled = true;


private void timerHTTP_Tick(object sender, EventArgs e)
{
PMMobile.Classes.Globals.gMessage.GetMessages(false);
}

******* THREAD*******

private void RunMessageThread()
{
int intMessageSleep = 20; //How long in secs before running again.
DateTime dtMessageSleep = DateTime.Now; //Set Inital DateTime

//Int TimeDifference
TimeSpan intTimeDifference;

//Check right away
myApp.Classes.Globals.gMessage.GetMessages(false);

while(myApp.Classes.Globals.gblMessageThread)
{
//Get time difference and check to see if >= intSleep Time
intTimeDifference = DateTime.Now - dtMessageSleep;

if(myApp.Classes.Globals.gblMessageThread && intTimeDifference.Seconds
=
intMessageSleep)
{
myApp.Classes.Globals.gMessage.GetMessages(false);
dtMessageSleep = DateTime.Now;
}

//Sleep for 2 seconds
Thread.Sleep(2000);

}

}


"Chris Tacke, eMVP" <ctacke[at]OpenNETCF_dot_org> wrote in message
All depends on what the check does and how you want to implement it.
There
are also 2 different timers - Thread.Timer and Forms.Timer which
are
very
different in behavior.

-Chris

"MDB" <nospam> wrote in message
Hello All,

I have function that checks for messages every 30 seconds and was
wondering
if it is better to use a thread or timer to do this?
 
Back
Top