In the class library that I am writing, I have a method that allows the library to go and do some things in another thread that does something like:
public void DoStuffAsync(AP p) { this.Running = true; this.Cancel = false; ParameterizedThreadStart threadStart = new ParameterizedThreadStart(DoStuff); Thread procThread = new Thread(threadStart); procThread.Start(p); }
I also have a series of events declared on an interface that a developer can connect, such as StatusUpdate and ProgressUpdate. I am currently writing a small test application (in WPF, although I expect the same behavior in WinForms) that calls DoStuffAsync () and then updates the progress bar and label.
Unfortunately, the 1st pass I have an error, the usual thread is not the one that owns the controls. I would like the user to not invoke Invoke () within the user interface and just subscribe to events and make them work.
So the question is, is there a way I can do this is my code when working with event handlers? Currently running like this:
public void UpdateProgress(object sender, ProgressEventArgs e) { if (handler != null) { handler(sender, e); } }
source share