Reputation: 37
I have a method which is worked asynchronous by a BackgroundWorker. Within this method, some code loops infinitly with an interval of 1000 ms.
Within this code, depending on some value, an event is dispatched. As far as I have understood this, this event runs in the same thread as the code from which it has been raised.
But what I want is, that this event runs in the same thread as the object which holds the BackgroundWorker. How do I achive this?
Upvotes: 1
Views: 1067
Reputation: 1469
You may need to take a look at the System.Threading.SynchronizationContext
which can be used to post events back to the BackgroundWorker Holder.
Upvotes: 0
Reputation: 327
Is your 'receiving' thread the GUI thread? If so then look at Control.Invoke() or Control.BeginInvoke(). Assuming it is your form, or some control on your form, that is handling this periodic event then you should be able to do this.
Upvotes: 0
Reputation: 12401
Depending on your domain and application, you can use the ProgressChanged
event (and ReportProgress
method), which runs in the thread you want.
Upvotes: 2
Reputation: 185643
You would be better off using a Timer
to schedule recurring events. If this is a Windows Forms application (which I'll assume it is since you want to run it in the context of the "owning" thread) you should use a System.Windows.Forms.Timer
instance.
Upvotes: 4