C
Colin Peters
I need some tips on how to realize a particular functionality in c#.
I’ve left out any c# nomenclature because I don’t want to muddy the
water by misusing terms.
I have a windows service that implements its business logic in a worker
thread. This allows the main thread of the service to react to stop and
start messages quickly whilst the worker thread polls occasionally to
see if it ought to stop.
So, just to recap, that makes two threads; a message pump (MP) thread
that implements the bare bones of a windows service and a worker thread
which does the real work. Let’s say that the “real work” involves
checking hardware periodically and updating a db with results. It runs
in an endless loop until the MP thread sets its “exit” bool.
Now, I want to add a component to the mix. It is periodically polling
another DB for updates and needs to inform my service when interesting
changes have taken place. The polling rate is outside my concern. The
component will call a callback function when a change is detected. Since
the component is to function fairly autonomously I guess it runs in its’
own thread (call it Thread3), probably a MP thread because it will have
to handle timer messages.
The question is: How can I detect when the callback is called? Is it
possible to execute the callback in a thread other than Thread3?
I imagine the pseudo code in my logic thread to look something like this:
Logic::Start()
{
M_Control = new M_Control();
M_Control.SetCallBack(SomethingHappened);
M_Control.StartYourPollingLogic();
....
While(!exit)
{
//Do other stuff
If(bSomething)
{
// react to this then set Boolean back to false
}
}
....
}
Logic::SomethingHappened()
{
bSomething = true;
}
Setting aside the race condition on bSomething, how can I pass to the
M_Control the address of the function I want it to call?
I'm not after a complete solution, just some hints as to what namespaces
or topics I ought to look at.
Thanks in advance
Colin
I’ve left out any c# nomenclature because I don’t want to muddy the
water by misusing terms.
I have a windows service that implements its business logic in a worker
thread. This allows the main thread of the service to react to stop and
start messages quickly whilst the worker thread polls occasionally to
see if it ought to stop.
So, just to recap, that makes two threads; a message pump (MP) thread
that implements the bare bones of a windows service and a worker thread
which does the real work. Let’s say that the “real work” involves
checking hardware periodically and updating a db with results. It runs
in an endless loop until the MP thread sets its “exit” bool.
Now, I want to add a component to the mix. It is periodically polling
another DB for updates and needs to inform my service when interesting
changes have taken place. The polling rate is outside my concern. The
component will call a callback function when a change is detected. Since
the component is to function fairly autonomously I guess it runs in its’
own thread (call it Thread3), probably a MP thread because it will have
to handle timer messages.
The question is: How can I detect when the callback is called? Is it
possible to execute the callback in a thread other than Thread3?
I imagine the pseudo code in my logic thread to look something like this:
Logic::Start()
{
M_Control = new M_Control();
M_Control.SetCallBack(SomethingHappened);
M_Control.StartYourPollingLogic();
....
While(!exit)
{
//Do other stuff
If(bSomething)
{
// react to this then set Boolean back to false
}
}
....
}
Logic::SomethingHappened()
{
bSomething = true;
}
Setting aside the race condition on bSomething, how can I pass to the
M_Control the address of the function I want it to call?
I'm not after a complete solution, just some hints as to what namespaces
or topics I ought to look at.
Thanks in advance
Colin