我需要在服务引用中调用一个名为 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() 的注释并再次执行时,一切都运行良好,但是如果我必须等到我的任务执行完毕,那么异步调用该方法是没有用的。
编辑
我还配置了服务引用,因此它允许生成异步操作。同样的问题。