Note that there are some explanatory texts on larger screens.

plurals
  1. POawait not working properly
    primarykey
    data
    text
    <p>I am attempting to write a window manager for one of our programs. It should display a series of windows to the client with each window asking for different information and then processing that information before moving to the next window. New windows can be dynamically added or removed, thus the window manager. In order to do this, I have a main shell that provides a parent window. Each child window is inserted as content into the parent window. When the next button is clicked, the child should process the on-screen info and then let the parent know that it can move to the next window. Because the processing can take up to 30 seconds for some windows, I want to display a busy indicator while this processing is going on. I am accomplishing that by using the Telerik RadBusyIndicator on my parent view. In order to show the busy indicator properly, I am attempting to use await/async to call the OnNext method asynchronously. The OnNext method is being called and the processing is taking place, but the UI thread is being locked up during processing and the busy indicator is not being shown. The relevant parts of my code are as follows:</p> <p>ShellViewModel.cs (the view model for the parent window):</p> <pre><code> protected async void NextAction() { //This is the property that is bound to the RadBusyIndicator to indicate if it should show or not IsBusy = true; //windowManager is the class that tells the child window to start processing and provides the parent window with the next view bool? retVal = await windowManager.OnNext(); if (retVal == true) { GetCurrentView(); } else if (retVal == null) { SetupApp.IsRestarting = true; GetCurrentView(); SetupApp.Config.RestartPoint = windowManager.CurrentViewCounter; File.WriteAllText("Config.xml", SetupConfig.Serialize(SetupApp.Config)); System.Diagnostics.Process.Start(Application.ResourceAssembly.Location); Application.Current.Shutdown(); } IsBusy = false; } private async void GetCurrentView() { IsBusy = true; SetupViewInit retVal = await windowManager.InitializeView(); if (retVal == SetupViewInit.CloseApp) { RaiseCloseRequest(); IsBusy = false; return; } IsBusy = false; Content = windowManager.CurrentView as FrameworkElement; OnPropertyChanged("CanNext"); OnPropertyChanged("CanBack"); OnPropertyChanged("NextToolTip"); if (windowManager.IsFinalView) { CancelText = "Close"; SetupApp.IsComplete = true; } } </code></pre> <p>SetupWindowManager.cs:</p> <pre><code> public async Task&lt;bool?&gt; OnNext() { //calls into the current view model to begin processing bool? retVal = CurrentViewModel.OnNext(); if (retVal == true) CurrentViewCounter++; return retVal; } //Called from GetCurrentView to set the current view model and perform any initialization that view model might need public async Task&lt;SetupViewInit&gt; InitializeView() { CurrentView = currentViewList[CurrentViewCounter]; CurrentViewModel.FireNextAction += FireNextAction; CurrentViewModel.PropertyChanged += PropertyChanged; SetupViewInit retVal = CurrentViewModel.Init(); if (retVal == SetupViewInit.NextScreen) { CurrentViewCounter++; retVal = await InitializeView(); } else if (retVal == SetupViewInit.TypeChange) { SetupType = SetupApp.Config.SetupType; MachineType = SetupApp.Config.MachineType; GetViewList(); retVal = await InitializeView(); } return retVal; } </code></pre> <p>Again, all of the code is executing properly. The code is just locking up the UI thread. If it makes any difference, I'm using the Bcl compiler components with .net 4.0. It's a big project and we're not ready to move to .net 4.5 yet.</p> <p>Edit: My response to Sethcran's answer was going to be too long for a comment.</p> <p>I cant' use Task.Run because we aren't using the .Net 4.5 library yet. However, it got me looking at Task.Factory.StartNew. I changed my code as indicated below and ran into the problem indicated in the comment:</p> <pre><code> Task.Factory.StartNew&lt;SetupViewInit&gt;(new Func&lt;SetupViewInit&gt;(async () =&gt; { CurrentView = currentViewList[CurrentViewCounter]; CurrentViewModel.FireNextAction += FireNextAction; CurrentViewModel.PropertyChanged += PropertyChanged; SetupViewInit retVal = CurrentViewModel.Init(); if (retVal == SetupViewInit.NextScreen) { CurrentViewCounter++; retVal = await InitializeView(); } else if (retVal == SetupViewInit.TypeChange) { SetupType = SetupApp.Config.SetupType; MachineType = SetupApp.Config.MachineType; GetViewList(); retVal = await InitializeView(); } //On the following line, VS shows an "Unexpected return value" error return retVal; })); </code></pre> <p>So how do I get the return value from the task?</p> <p>I couldn't get either of these methods to work and ended up going a completely different route. Thanks for your suggestions, but I simply don't have time on this project to mess around with them any more.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload