如何在Task.factory.fromAsync中使用void方法

时间:2015-07-16 12:27:57

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

我使用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();

这是函数

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

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

这是我的问题的答案

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

这行代码工作正常