【问题标题】:How to wait for an asynchronization method in silverlight如何在silverlight中等待异步方法
【发布时间】:2012-11-26 11:40:03
【问题描述】:

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

请帮助其紧急。

【问题讨论】:

  • 请考虑发布一些代码以及您已经尝试过的内容

标签: c# asp.net wcf silverlight


【解决方案1】:

如果您使用的是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(又名 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);

【讨论】:

  • 感谢您的回复,我正在粘贴我的代码,请在代码中建议我如何实现
【解决方案2】:

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

或者是 Visual Studio

你可以使用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
}

安东尼

【讨论】:

  • 注意,你不能用 AutoResetEvent.WaitOne 阻塞 UI 线程!您需要在后台线程中等待并使用 (Deployment.Current/Control).Dispatcher.BeginInvoke 来使用 UI Thread。
  • 如果 Silverlight 5 则使用 Task
猜你喜欢
  • 1970-01-01
  • 2017-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-06
  • 1970-01-01
  • 2015-12-21
相关资源
最近更新 更多