Skip to main content
Home Forums Silverlight Programming Programming with .NET - General dispatcher.begininvoke - I need to know when it's completed
8 replies. Latest Post by msalsbery on November 1, 2009.
(0)
celikc
Member
1 points
3 Posts
10-30-2009 9:15 AM |
Hi,
I've this code running on a thread other than the UI thread
//do somethingSomeControl.Dispatcher.BeginInvoke(() =>{ SomeControl.SomeFunction();});//Here I want the thread to sleep until SomeFunction returnsThe control is not my code so I cannot apply an asynchronus programming pattern there.
thanks,
-can
Ardman
Contributor
3190 points
884 Posts
10-30-2009 10:14 AM |
You could use:
DispatcherOperation op = Dispatcher.BeginInvoke( DispatcherPriority.Normal, new Action<string>(SetStatus), "From Other Thread (Async)"); DispatcherOperationStatus status = op.Status; while (status != DispatcherOperationStatus.Completed) { status = op.Wait(TimeSpan.FromMilliseconds(1000)); if (status == DispatcherOperationStatus.Aborted) { // Alert Someone } }
10-30-2009 10:23 AM |
DispatcherOperation does not seem to have the Status property in Silverlight unlike the full .Net
10-30-2009 10:31 AM |
Whoops! My bad.
ksleung
5352 points
1,025 Posts
10-30-2009 1:15 PM |
Assuming you are calling BeginInvoke() from a background thread, you can use ManualResetEvent to wait for the event to complete: Please take a look at my answer to this thread.
http://forums.silverlight.net/forums/t/101078.aspx
The code you want to use is this (copied from the thread):
ManualResetEvent m = new ManualResetEvent(false);WebClient wc = new WebClient();wc.Completed += () => { ... m.Set();};wc.OpenRead(new Uri(uri), ...);m.WaitOne();
Edit: I didn't read your question carefully but the answer is still somewhat valid. Basically you enqueue a job to the UI queue, and setup and wait for a ManualResetEvent to finish. In the Completed event, set the ManualResetEvent to one. Before completion, the WaitOne() operation will fail and block, until the job is finished in the UI thread. This applies to any Background/Foreground operations including but not exclusive to WebClient.
jackbond
2820 points
725 Posts
10-30-2009 2:36 PM |
If you'd like to see Invoke in Silverlight 4, hopefully voting for it will help: http://silverlight.uservoice.com/pages/4325-feature-suggestions/suggestions/312882-add-invoke-method-to-dispatcher-class?ref=title
msalsbery
Participant
1814 points
318 Posts
11-01-2009 3:48 PM |
*deleted*
11-01-2009 6:53 PM |
msalsbery:all you've accomplished is an overly complex way to use one thread, right? :)
Unless I've misread, it looks like he's wanting to block his background thread until the operation on the UI thread has completed. How is that "an overly complex way to use one thread?"
11-01-2009 7:31 PM |
jackbond:wanting to block his background thread until the operation on the UI thread has completed.
In the words of Ardman... "Whoops! My bad."
I'm the one that COMPLETELY misread!