简体   繁体   中英

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

Hello I have next problem with 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. 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.

UPD2: after Julian answer I try next:

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. I download github example by Mahesh Sabnis . and the same behavior, the process hangs in task Manager after closing. Can someone tested at itself?

Using async methods with 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 )

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. 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.

As a last resort you could also call the System.Environment.Exit() method which terminates the process.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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