Run thread every 1 minute - problems

G

Guest

Hi there,

Having an issue with RunAppAtTime

I'm successfully running a thead every 1 minute, however when the user
clicks a button the thread should not run again, however it just isn't
working. Here's my code that isn't doing what it should be.

if (updateReminder != null)
{
EventWaitHandle notifyEvent = new OpenNETCF.Threading.EventWaitHandle(
false,
OpenNETCF.Threading.EventResetMode.AutoReset,
"SAMS_REMINDER_EVENT" );

// Notify the thread to stop.
notifyEvent.Set();
notifyEvent.Close();
}

Anything blindingly obvious wrong there??

I have exactly the same sort of thing working, and the same routine runs
with the app exists, and that works fine.

Many Thanks
Rob
 
P

Paul G. Tobey [eMVP]

I don't think you've given us enough to answer your question. You're saying
that, when some button is hit in the application, you fire an event that is
supposed to notify the thread that it should exit? Or something else?

Paul T.
 
G

Guest

Hi Paul,

Yes that's exactly it.... the application at a point in time runs a thread
at a specific time with RunAppAtTime().. - This works fine, however i can't
seem to be able to stop the thread being called.

Thanks for the help...
Rob
 
P

Paul G. Tobey [eMVP]

Well, RunAppAtTime() doesn't "run" a thread. It can fire an event, which
you might have a thread waiting on with one of the Win32 WaitXXX()
functions. If that's what you are doing, I'd have the thread wait on
*multiple* events, one the actual RunAppAtTime() and one a general event
that signals that it's time for the thread to exit. The Win32 call
WaitForMultipleObjects() would be used to do this and the parameter
fWaitForAll should be set to FALSE, assuring that either event can allow the
Wait to return. On return, the return value of WaitForMultipleObjects
indicates which event caused the return. If it's the run-app event, do the
periodic process; if it's the exit event, return from the thread procedure.

Paul T.
 
G

Guest

Paul, thanks for the reply.... it sounds complicated!

I already have this working for another process which works on the same
principal, and to signify to the system that i'm done with that notification
i simply do something like this:

EventWaitHandle notifyEvent;

if ( timedSyncThread != null )
{
notifyEvent = new OpenNETCF.Threading.EventWaitHandle(
false, OpenNETCF.Threading.EventResetMode.AutoReset,
"SAMS_SYNC_EVENT" );

// Notify the thread to stop.
notifyEvent.Set();

notifyEvent.Close();
}

That runs when the application exists and seems to work as expected, however
when i run the same code for my other new process then it simply just doesn't
stop the event from firing.

Being honest, what you've just suggested is a bit alien to me at the moment.
I haven't touched on any of that before.
 
P

Paul G. Tobey [eMVP]

It's not complicated. If you can do threading at all, it's a minor
variation of what you have to do there in every case.

That's fine code for *signaling* the event, assuming that the event on which
the thread is waiting is waiting on that event *and assuming that when it
fires, it's time to leave*! If you have the thread waiting on only one
event and you set it, what's the thread going to think? That the event was
fired by run-at-time, right? Set a breakpoint on return from the wait and
see what's happening; I bet it's obvious, when you do that.

Paul T.
 
G

Guest

Thanks Paul, i have this working now.

I was getting myself a bit confused over i nothing really.

Appreciate the help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top