简体   繁体   English

为什么WPF应用程序在客户端代理异步调用WCF后没有关闭?

[英]Why WPF app not closing after Client Proxy Async Call to WCF?

Hello I have next problem with WCF. 您好,我有WCF的下一个问题。 I generate my client proxy class with "task-based" async mode. 我使用“基于任务”的异步模式生成客户端代理类。 With sync methods all works good, but when I call async method (void or not - irrelevant) and close my WPF MainWindow (through cross on title bar), window closed but process not killed. 使用sync方法,所有方法都可以正常工作,但是当我调用async方法(是否无效-不相关)并关闭我的WPF MainWindow(通过标题栏上的叉号)时,窗口关闭但进程未终止。 And this happen only after async calls. 而这仅在异步调用之后发生。

i try this: 我试试这个:

var client = new Service.ServiceClient();
await client.syncWithDevicesAsync();
client.Close();

and with 'using': 和“使用”:

using (var client = new Service.ServiceClient())
{
  await client.syncWithDevicesAsync();
}

I've seen similar questions, but I could not understand them, please explain why this is going. 我见过类似的问题,但我听不懂,请解释为什么会这样。 Thx for help. 谢谢。

UPD: In debug thread window I saw when I call async method, created 4 threads, but after response released only 3.. GC.Collect() in close window event not help. UPD:在调试线程窗口中,我看到当我调用异步方法时,创建了4个线程,但是响应仅释放3个。关闭窗口事件中的GC.Collect()没有帮助。

UPD2: after Julian answer I try next: UPD2:在朱利安回答之后,我尝试下一个:

private async void Button_Click(object sender, RoutedEventArgs e)
{
    var proxy = new ServiceReference1.ServiceClient();

    var photo = await proxy.getEmployeePhotoAsync(12);
    label.Content = photo.Length; //everything is good, label show correct size. So the asynchronous method is completed?
    proxy.Close();
    //Environment.Exit(0);  //- if uncomment window freeze forever. label nothing show. 
}

and try this: 并尝试这个:

await Task.Run(async () => 
                {
                    var photo = await proxy.getEmployeePhotoAsync(12);
                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        label.Content = photo.Length;
                    });
                });

UPD3: Oh..Now I am completely stuck. UPD3:哦..现在我完全被卡住了。 I download github example by Mahesh Sabnis . 我下载了Mahesh Sabnis的github示例 and the same behavior, the process hangs in task Manager after closing. 和相同的行为,该进程关闭后挂在任务管理器中。 Can someone tested at itself? 有人可以自我测试吗?

Using async methods with Task.Run : Task.Run使用异步方法:

queues that Task for execution on a threadpool thread. 将该任务排队以在线程池线程上执行。 Threads execute in the context of the process (eg. the executable that runs your application) 线程在进程的上下文中执行(例如,运行您的应用程序的可执行文件)

thus it's not running on the UI Thread ( SO ) 因此它不在UI线程( SO )上运行

I presume that in your case some async method is still running (probably even looping) in the background after you have closed the WPF window and the UI thread stopped. 我假设在您的情况下,在关闭WPF窗口并且UI线程停止后,某些异步方法仍在后台运行(可能甚至循环)。 Consequently the process is still running. 因此,该过程仍在运行。

You could pass a CancellationToken to the background task(s) and then call Cancel in the application exit event. 您可以将CancellationToken传递给后台任务,然后在应用程序退出事件中调用Cancel

As a last resort you could also call the System.Environment.Exit() method which terminates the process. 作为最后的选择,您还可以调用System.Environment.Exit()方法来终止进程。

I'm not sure anyone else face this, but still I will write an answer. 我不确定其他人是否会面对此问题,但是我仍然会写一个答案。 I first encountered this.. 我第一次遇到这个

Problem was in my pc or VS2013.. I reboot my pc and all work with: 问题出在我的PC或VS2013中。

private async void Button_Click(object sender, RoutedEventArgs e)
{
    var proxy = new ServiceReference1.ServiceClient();

    var photo = await proxy.getEmployeePhotoAsync(12);
    label.Content = photo.Length; 
    proxy.Close();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM