Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to invoke a method on the UI thread from within a worker thread? [duplicate]

I'm working on a project which uses the following technologies:

  • C# (.NET 4.0)
  • WCF
  • PRISM 4

I'm currently making an asynchronous call to one of our Web Services using the Begin/End methods generated by a proxy. The call is successful and the client is able to receive the Web Service's response on a worker thread.

Once the response is received, I proceed to raise an event. The class subscribed to the event proceeds to request a UI navigation using PRISM:

Application.Current.Dispatcher.BeginInvoke(new Action(() =>
    this.RegionManager.RequestNavigate(RegionNames.LoginContentRegion, projectSelectionViewUri)));

Since the asynchronous WCF response is not captured on the UI thread, I'm forced to invoke the UI thread using Application.Current.Dispatcher.BeginInvoke(...).

The problem here is that the invoke seems to do nothing. The UI is not updated, and no exception is thrown.

How should I invoke the UI thread from within an event that is raised on a worker thread?

Edit: This question has been re-asked at the following link, since the supposed duplicate does not provide an answer:

Request UI navigation using PRISM 4 on an asynchronous WCF response thread

like image 968
Hussein Khalil Avatar asked Dec 20 '11 15:12

Hussein Khalil


Video Answer


1 Answers

You need to make sure you are invoking on the actual UI Dispatcher, not necessarily the Current. You could try passing in the UI Dispatcher, or have some form of callback that will be handled by the UI somewhere.

like image 198
Samuel Slade Avatar answered Oct 12 '22 10:10

Samuel Slade