开发者

When exactly is a CallbackMethod invoked on an Async operation?

开发者 https://www.devze.com 2023-03-30 23:56 出处:网络
I am adding some data to a list and it might take a while to do this. There开发者_JAVA百科fore i perform this action asynchronously. I do it like this:

I am adding some data to a list and it might take a while to do this. There开发者_JAVA百科fore i perform this action asynchronously. I do it like this:

ScanDelegate worker = StartScan;
AsyncCallback completedCallback = ScanCompletedCallback;
lock (_sync)
{
    var async = AsyncOperationManager.CreateOperation(null);
    worker.BeginInvoke(completedCallback, async);
}

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.

Is this true?

And if it is, how can i know when my worker has completed its tasks?

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 ?

Thanks in advance!

StartScan()

private void StartScan()
    {
        //For testing
        for (int t = 0; t < 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"
                                                });
            }
        }
   }

ScanCompletedCallback()

        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 => OnScanCompleted((AsyncCompletedEventArgs)e), completedArgs);
    }

AddComputerToList()

private object AddComputerToList(Object objComputer)
    {
        var computer = objComputer as ComputerModel;
        if (computer != null)
        {
            ComputersList.Add(computer);
            OnPropertyChanged("ComputersList");
        }
        return null;
    }


As a direct answer to your question the callback will occur on completion of ScanCompletedCallback.

If you have a list that you believe should have some data in it at this point then there appears to be something else wrong, posting some more code may help with this.

0

精彩评论

暂无评论...
验证码 换一张
取 消