Skip to main content

Microsoft Silverlight

Answered Question dispatcher.begininvoke - I need to know when it's completedRSS Feed

(0)

celikc
celikc

Member

Member

1 points

3 Posts

dispatcher.begininvoke - I need to know when it's completed

Hi,

I've this code running on a thread other than the UI thread

//do something

SomeControl.Dispatcher.BeginInvoke(() =>
{
 SomeControl.SomeFunction();
});

//Here I want the thread to sleep until SomeFunction returns

The control is not my code so I cannot apply an asynchronus programming pattern there.

thanks,

-can

 

 

Ardman
Ardman

Contributor

Contributor

3190 points

884 Posts

Re: dispatcher.begininvoke - I need to know when it's completed

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
        }
    }

celikc
celikc

Member

Member

1 points

3 Posts

Re: Re: dispatcher.begininvoke - I need to know when it's completed

DispatcherOperation does not seem to have the Status property in Silverlight unlike the full .Net

Ardman
Ardman

Contributor

Contributor

3190 points

884 Posts

Re: Re: Re: dispatcher.begininvoke - I need to know when it's completed

Whoops!  My bad.

ksleung
ksleung

Contributor

Contributor

5352 points

1,025 Posts

Re: Re: Re: dispatcher.begininvoke - I need to know when it's completed

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
jackbond

Contributor

Contributor

2820 points

725 Posts

Answered Question

Re: dispatcher.begininvoke - I need to know when it's completed

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
msalsbery

Participant

Participant

1814 points

318 Posts

Re: dispatcher.begininvoke - I need to know when it's completed

*deleted*

Mark Salsbery
Microsoft MVP - Visual C++

jackbond
jackbond

Contributor

Contributor

2820 points

725 Posts

Re: dispatcher.begininvoke - I need to know when it's completed

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?"

msalsbery
msalsbery

Participant

Participant

1814 points

318 Posts

Re: dispatcher.begininvoke - I need to know when it's completed

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!

 

Mark Salsbery
Microsoft MVP - Visual C++
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities