1

我在 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);`

请帮助其紧急。

4

2 回答 2

2

如果您使用的是基于任务的异步模式

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();
  });

如果您使用的是经典异步模式(又名 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);
于 2012-11-26T11:44:05.507 回答
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
}

安托万

于 2012-11-26T15:29:18.147 回答