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

本文关键字:void 方法 fromAsync Task factory | 更新日期: 2023-09-27 18:03:06

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

这是函数

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

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

您需要异步等待(与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();
}

这就是我的问题的答案

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

这行代码运行正常