Note that there are some explanatory texts on larger screens.

plurals
  1. POWhen exactly is a CallbackMethod invoked on an Async operation?
    primarykey
    data
    text
    <p>I am adding some data to a list and it might take a while to do this. Therefore i perform this action asynchronously. I do it like this:</p> <pre><code>ScanDelegate worker = StartScan; AsyncCallback completedCallback = ScanCompletedCallback; lock (_sync) { var async = AsyncOperationManager.CreateOperation(null); worker.BeginInvoke(completedCallback, async); } </code></pre> <p>The information added in StartScan() is correctly added to the list. When the scan is complete, i want to perform a different scan, depending on the data in the list. So i start the different scan in the ScanCompletedCallback() method. But at this point, my list is empty. Im guessing that this is because the callback method is invoked when the worker has been started, and not when it returns.</p> <p>Is this true?</p> <p>And if it is, how can i know when my worker has completed its tasks?</p> <p>Edit: I can't get my head around this. It doesn't make sense. I came to think of the list i am adding to. I couldn't just add to it, i had to wrap it in a Dispatcher thread. This must be the problem, right? Is there a way i can make my Async method StartScan() wait for this Dispatcher ?</p> <p>Thanks in advance!</p> <p>StartScan()</p> <pre><code>private void StartScan() { //For testing for (int t = 0; t &lt; 1; t++) { var app2 = Application.Current; if (app2 != null) { app2.Dispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(AddComputerToList), new ComputerModel() { HostName = "Andreas-PC", IpAddress = "192.168.1.99", ActiveDirectoryPath = "ikke i AD", Name = "Andreas-PC", OperatingSystem = "Microsoft Windows 7 Enterprise" }); } } } </code></pre> <p>ScanCompletedCallback()</p> <pre><code> private void ScanCompletedCallback(IAsyncResult ar) { var worker = (ScanDelegate)((AsyncResult)ar).AsyncDelegate; var async = (AsyncOperation)ar.AsyncState; worker.EndInvoke(ar); lock (_sync) { IsScanning = false; } var completedArgs = new AsyncCompletedEventArgs(null, false, null); async.PostOperationCompleted(e =&gt; OnScanCompleted((AsyncCompletedEventArgs)e), completedArgs); } </code></pre> <p>AddComputerToList()</p> <pre><code>private object AddComputerToList(Object objComputer) { var computer = objComputer as ComputerModel; if (computer != null) { ComputersList.Add(computer); OnPropertyChanged("ComputersList"); } return null; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload