18

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

Community
  • 1
  • 1
Hussein Khalil
  • 1,585
  • 2
  • 25
  • 47

1 Answers1

10

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.

Samuel Slade
  • 8,405
  • 6
  • 33
  • 55
  • How would I go about passing the UI Dispatcher ? – Hussein Khalil Dec 20 '11 at 15:38
  • 1
    Assuming you create the class that requires it from some UI element (i.e. a `UserControl`), you can do something like: `var someClass = new SomeClass(this.Dispatcher);`. – Samuel Slade Dec 20 '11 at 15:39
  • @HusseinKhalil - Can you not send a reference of the main Window? – Security Hound Dec 20 '11 at 15:41
  • 1
    @Ramhound Passing some class a reference to the `MainWindow` is never a good solution, unless that class is going to handle some part of the `MainWindow`. If it's simply to use the `Dispatcher`, you only need to pass through the `Dispatcher`. – Samuel Slade Dec 20 '11 at 15:42
  • The problem I'm encountering is that the class which requires the dispatcher is created using PRISM (i.e.: its a View's View-Model), and so I do not directly instantiate it. I'm using UNITY to provide it with all the class references it needs using inversion of control. I tried requesting a dispatcher in the class's constructor, hoping that I would get the UI dispatcher using inversion of control (since the class is created, I assume, on the UI thread), but it didn't work. – Hussein Khalil Dec 20 '11 at 15:47
  • 1
    @Slade - I will trust you when you say its not a good idea. The only reason I would do it, is in order to get the dispatcher, thats one way I use ISyncInvok – Security Hound Dec 20 '11 at 15:49
  • @HusseinKhalil - You should open a new question I guess. We need some more sample code. – Security Hound Dec 20 '11 at 15:50
  • 1
    Ok, two suggestions: 1. Create some class to contain a static reference to the UI `Dispatcher` (not particularly a nice approach, but should be manageable enough if done properly). 2. Post the IoC code sample your trying to use and I or someone may be able to help further. – Samuel Slade Dec 20 '11 at 15:50
  • Thanks Ramhound and Slade. I like your idea of storing the UI dispatcher in a static reference... I'm not sure if its the best idea though, as you suggested. I created a new question: http://stackoverflow.com/questions/8578346/request-ui-navigation-using-prism-4-on-an-asynchronous-wcf-response-thread – Hussein Khalil Dec 20 '11 at 16:08