【问题标题】:How to use void method with Task.factory.fromAsync如何在 Task.factory.fromAsync 中使用 void 方法
【发布时间】:2015-07-16 12:27:56
【问题描述】:

我正在使用 wcf 来执行插入数据的任务。我正在使用 wcf void 函数之一来插入用户数据

AceVqbzServiceClient aceVqbzClientService = new AceVqbzServiceClient();
aceVqbzClientService.OpenAsync();
IAceVqbzService aceVqbzTypeService = aceVqbzClientService as IAceVqbzService;

Task.Factory.FromAsync(
            aceVqbzTypeService.BeginSaveUserOrganizationLinking,
            aceVqbzTypeService.EndSaveUserOrganizationLinking,
            objUser_OrganizationDetail, 
            TaskCreationOptions.None);

aceVqbzClientService.CloseAsync();

这是函数

当我使用时,功能没有任何问题,但数据没有通过它插入。 请给我解决方案,以便我可以实现这个

【问题讨论】:

  • @HenkHolterman 比如?

标签: c# android-asynctask xamarin async-await


【解决方案1】:

您需要异步等待(使用await)从FromAsync 返回的Task

public async Task FooAsync()
{
    AceVqbzServiceClient aceVqbzClientService = new AceVqbzServiceClient();
    await aceVqbzClientService.OpenAsync();
    IAceVqbzService aceVqbzTypeService = aceVqbzClientService as IAceVqbzService;

    await Task.Factory.FromAsync(
                       aceVqbzTypeService.BeginSaveUserOrganizationLinking,
                       aceVqbzTypeService.EndSaveUserOrganizationLinking,
                       objUser_OrganizationDetail, TaskCreationOptions.None);

    await aceVqbzClientService.CloseAsync();
}

【讨论】:

  • 你不认为你也应该await 其他异步调用吗?
  • 感谢您提供答案,但它在我的代码中不起作用
【解决方案2】:

这是我的问题的答案

 try
        {
            AceVqbzServiceClient aceVqbzClientService = new AceVqbzServiceClient();
            aceVqbzClientService.OpenAsync();
            IAceVqbzService aceVqbzTypeService = aceVqbzClientService as IAceVqbzService;

            var asyncResult = aceVqbzTypeService.BeginSaveUserOrganizationLinking( objUser_OrganizationDetail, null, null );
            asyncResult.AsyncWaitHandle.WaitOne();


        }
        catch ( Exception ex )
        {
            throw;
        }

这行代码运行良好

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-27
    • 2011-01-17
    相关资源
    最近更新 更多