The following is the pattern I used to solve the above...
Apparently I could have implemented
ISyncronizedInvoke, which is how Control.Invoke works.
However I'm unsure as to how to make that run on the correct thread.

I'd love some feedback on both approaches

private int mInterval=30;
private bool mStatus;
private Timer mTimer;
WaitHandle tmrSync = new AutoResetEvent(false);

public void StartComponent()
{
      mTimer = new Timer(new TimerCallback(TimerCallback), tmrSync,
1000, mInterval * 1000);
      ExecuteLoop();
}

public void ExecuteLoop()
{
     while (mStatus == true)
     {
           tmrSync.WaitOne();
           DoWork();
     }
}

public void TimerCallback(object state)
{

      AutoResetEvent are = (AutoResetEvent)state;
      are.Set();
}


On Sep 14, 6:41 pm, InfRes <[email protected]> wrote:
> Hi All,
>
> Currently converting a winforms app to a windows service.
> It polls a DB and calls some COM stuff.
>
> The COM stuff is fussy and needs an STA thread..
>
> The winforms app used a timer and all the com interop was in the form.
>
> Moving to a service, there is no form, and thread timer callbacks cant
> call back to my 'UI' thread,
> they are instead run in a threadpool thread which is MTA rather than
> STA, so my COM doesn't work...
> If I create a new thread the COM objects can't edit the variables
> passed in...
>
> Essentially I'm stuck!
>
> Any Ideas?

Reply via email to