从GUI启动服务器端操作并在操作完成后更新GUI

时间:2018-04-18 12:19:09

标签: c# web-services wcf asynchronous soap

我有一个WCF服务接口:

[OperationContract]
void BeginDelete(MyParams params);

,看起来像:

private CancellationTokenSource _cancelSource;
...
public void BeginDelete(MyParams params)
{
    ....
    _cancelSource = new CancellationTokenSource();
    var cancelToken = _cancelSource.Token;
    var task = System.Threading.Tasks.Task.Factory.StartNew(() =>
    {
        try
        {
            bool done = false;
            while (!done)
            {
                cancelToken.ThrowIfCancellationRequested();

                // Deleting stuff from database...
                // Carry on deleting until there is no more to delete
                // or user cancels midway.
            }

            // Operation is now complete so update GUI (e.g. callback).
        }
        catch
        {
            throw;
        }
    }, cancelToken);

    task.ContinueWith(t =>
    {
        Console.WriteLine("Faulted...");
    }, System.Threading.Tasks.TaskContinuationOptions.OnlyOnFaulted);

    task.ContinueWith(t =>
    {
        Console.WriteLine("Canceled...");
    }, System.Threading.Tasks.TaskContinuationOptions.OnlyOnCanceled);
}   

我通过我的网页GUI(例如带有按钮的jQuery对话框'删除')来调用这项服务。

当操作正在进行时,我会显示一个带进度条的对话框,但这是我的问题......

对话框保持打开状态,因为它不知道操作已完成。

如何从服务器端传达更新GUI的请求?

1 个答案:

答案 0 :(得分:0)

在您的其他ContinueWith之后出现类似的内容:

task.ContinueWith(t =>
    {
        dialog.Close()
    });