如何在silverlight中等待异步方法

时间:2012-11-26 11:40:04

标签: c# asp.net wcf silverlight

我在WCF服务中创建了两个函数,并使用异步在Silverlight中调用它们。我在另一个之后调用一个方法,但在第一个方法完成之前,silverlight执行第二个方法。我希望第一种方法在第二次方法调用之前完全执行。 感谢您的回复我粘贴我的代码,请在代码中建议我如何实施。

          private GDOperations.GDDoneOperationsClient _gdDoneOperation;
           private ImageOperationsClient proxy = null;
           foreach (var file in _filesCollection)
           {
            clsImageTransactionEntity _clsImageEntity = new clsImageTransactionEntity();
            _clsImageEntity.ImageByte = GetFileData(file.OpenRead());
            _clsImageEntity.ImageExtension = file.Extension;
            _clsImageEntity.ImageName = file.Name;
            _clsImageEntity.ImageType = 2;
            _clsImageEntity.ImagePath = "~/CMSImages/FinalImages/" + lblSelectedBarcode.Content.ToString() + "/" + file.Name;
            _clsImageEntity.JabongBarcode = lblSelectedBarcode.Content.ToString();


            GDOperations.clsImageTransactionEntity _clsImageGDEntity = new GDOperations.clsImageTransactionEntity();
            _clsImageGDEntity.ImageExtension = file.Extension;
            _clsImageGDEntity.ImageName = file.Name;
            _clsImageGDEntity.ImageType = 2;
            _clsImageGDEntity.ImagePath = "~/CMSImages/FinalImages/" + lblSelectedBarcode.Content.ToString() + "/" + file.Name;
            _clsImageGDEntity.JabongBarcode = lblSelectedBarcode.Content.ToString();
            _clsImageGDEntity.RoleId = roleID;
            _clsImageGDEntity.TaskID = taskID;
            _clsImageGDEntity.UserID = UserId;
            _clsImageGDEntity.SystemIP = systemIP;
            _clsGdAllotment.clsImageTransactionEntity.Add(_clsImageGDEntity);
            ----- first method calling-----                
           proxy.UploadFinalImageCompleted += (s, e) =>
            {
                if (e.Error == null)
                {

                }     
            };
            proxy.UploadFinalImageAsync(_clsImageEntity);
            countfile = countfile + 1;
            pbUploadFiles.Value = countfile;

        }
        _clsGdAllotment.GdID = int.Parse(lblUserID.Content.ToString());
        _clsGdAllotment.JabongBarcode = lblSelectedBarcode.Content.ToString();
        _clsGdAllotment.TaskID = taskID;
        --- after for loop completion calling second method -----
        _gdDoneOperation.InsertGDDoneInformationCompleted +=      _gdDoneOperation_InsertGDDoneInformationCompleted;
        _gdDoneOperation.InsertGDDoneInformationAsync(_clsGdAllotment);`

请紧急帮助。

2 个答案:

答案 0 :(得分:2)

如果您正在使用Task-Based Async Pattern

var task1 = CallFirstAsyncMethod();
task1.Wait(); // waiting task to finish

var task2 = CallAnotherAsyncMethod();    

// or subscribe to the task continuation to call second
// method when first operation will finished
task1.ContinueWith(t =>
  {
    // add error handling
    var task2 = CallAnotherAsyncMethod();
  });

如果你正在使用Classical Async Pattern(a.k.a. APM):

IAsyncResult ar1 = CallFirstAsyncMethod();
ar1.WaitHandle.Wait();
IAsyncResult ar2 = CallSecondAsyncMethod();

// or use the same technique asynchronously
CallFirstAsyncMethod(ar => // Suppose we should provide appropriate callback
 {
    // Call to appropriate EndInvoke method

    IAsyncResult ar2 = CallSecondAsyncMethod();
 }, state);

答案 1 :(得分:0)

你可以在第一个回调中调用第二个,不是吗?

或者是Visual Studio< 2012

您可以使用AutoResetEvent

MyServiceClient clientService;
AutoResetEvent autoResetEvent = new AutoResetEvent(false);
public void Execute()
{
    InvokeCompletedEventArgs data = null;
    clientService.InvokeCompleted += (e, f) =>
    {
        data = f;
        autoResetEvent.Set();
    };
    m_proxy.MyCallAync();
    autoResetEvent.WaitOne(); // Wait the set of autoResetEvent
    m_proxy.MySecondCallAync(data); // Data return by the first call
}

安托