当Task调用服务引用中的方法时,ThreadAbortException

时间:2014-11-19 13:44:01

标签: c# .net service task threadabortexception

我需要在服务引用中调用一个名为CancelTransactionByRefPos的方法。 此方法具有以下签名:

int CancelTransactionByRefPos(long refPos, out string resultDescription);

如果此方法返回' 0'取消成功。如果此方法返回另一个值,则必须再次调用该方法。

我编写了一个调用CancelTransactionByRefPos的Cancel方法。调用异步是非常重要的,因为它可能需要30秒才能给出响应。

我的方法如下:

public void Cancel(long refPos, GiftCard giftCard, Account_3_2 account)
    {
       var task =  Task.Factory.StartNew(() =>
        {
            int result;
            do
            {
                string resultDescription;
                result = _client.CancelTransactionByRefPos(refPos, out resultDescription);
                // logic to log the result with the giftcard and account parameter
            } while (result != 0);
        });
        // task.Wait(); // ######## Uncomment this -> code executes! ######## 
    }

当我在评论中使用task.Wait()部分取消时,我得到一个ThreadAbortException。

当我取消注释task.Wait()并再次执行时,一切都运行良好但是如果我必须等到执行任务,那么调用该方法是没有用的。

修改

我还配置了服务引用,因此它允许生成异步操作。同样的问题。

0 个答案:

没有答案