Callback after a thread.Start() finished?
I've a small question
I've a WPF application, and one method takes some times(it send informations through WCF services). I would like to do this in another thread than the GUI thread.
The problem:
The GUI ask to services, which asks to a datastore, which give a new transaction manager, which communicate with the WCF s开发者_运维百科ervice.
The datastore objects have the GUI thread as owner.
I would like, in the transaction manger, do something like this:
public void MyMethod(abcdef){
//Income with the GUI thread
//Launch a thread with my action which tooks times
//update my objects with the result(Has to be done with the GUI thread)
But I've no GUI element here on which I can do a XYZ.Dispatch or Invoke or BeginInvoke. But when I start, I know the GUI thread reference. The problem is that I'm going to have thread Owner exceptions.
I've some constraints:
- I cannot have a GUI object to do a begin Invoke here
- I cannot set that the datastore has non-gui datastore
- I need that this call is non-blocking
- I need results of the non-GUI thread
Do you have an idea about how to do that?
Thank you!
Use Dispatcher.BeginInvoke()
.
With that method you can execute a function, with parameters, that is executed on the GUI thread. You pass the Dispatcher
property of your WPF window to the background worker and the BeginInvoke
method of that instance is used to communicate back.
精彩评论