Thomas
Thomas

Reputation: 37

C# BackgroundWorker question

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

Answers (4)

Islam Yahiatene
Islam Yahiatene

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

Andy
Andy

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

Jon Seigel
Jon Seigel

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

Adam Robinson
Adam Robinson

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

Related Questions