Action after ActivateItem in Caliburn.Micro - c#

I have a question. I have button where i launch ActivateItem:
public void LoadTaskManagerPage()
{
this.ActivateItem(new TaskManagerViewModel(this.LoggedUser, this.repository));
if (this.LoggedUser.GetUserTask() != null)
{
this.IsActiveTaskButtonVisible = Visibility.Visible;
this.NotifyOfPropertyChange(() => this.IsActiveTaskButtonVisible);
}
}
Is it possible to hang an app and go to if statement only, if ActivateItem ends?
How to wait for end of ActivateItem in Caliburn.Micro?
EDIT:
Trying something like:
public void LoadTaskManagerPage()
{
var taskManagerTask = Task.Run(() => this.ActivateItem(new TaskManagerViewModel(this.LoggedUser, this.repository)));
taskManagerTask.Wait();
if (!this.LoggedUser.GetUserTask().IsTaskTakenByUser())
{
this.IsActiveTaskButtonVisible = Visibility.Visible;
this.NotifyOfPropertyChange(() => this.IsActiveTaskButtonVisible);
}
}
With Tasks, but when i click on LoadTaskManagerPage() it wont show any window, application hangs forever
EDIT2
Based on my issue on github, i upgraded Caliburn to alpha 4.0:
public void LoadTaskManagerPage()
{
this.ActivateItemAsync(new TaskManagerViewModel(this.LoggedUser, this.repository));
if (!this.LoggedUser.GetUserTask().IsTaskTakenByUser())
{
//logic
}
}
i changed ActiveItem() to ActiveItemAsync() but still, it hit if statement before active item will close. using async/await do the same
EDIT3
When i make async/await it still works the same, look at my view on the right. Its clicked, User Control not appear. In the same time (even when my User Control not show up) im hitting if statement too early. I want to hit it on User Control close.

You should await the ActivateItemAsync method. This means that your LoadTaskManagerPage() should return a Task and you should await this one as well:
public async Task LoadTaskManagerPageAsynnc()
{
await this.ActivateItemAsync(new TaskManagerViewModel(this.LoggedUser, this.repository));
if (!this.LoggedUser.GetUserTask().IsTaskTakenByUser())
{
//logic
}
}

Related

How to process only last Task and discard previous in c#

I have a WPF application. Window includes datagrid for showing data and button for searching them. In viewmodel there is a method for loading data to the datagrid.
public List<MyObject> DataGridData { get; set; }
public void LoadDataToDatagrid(object obj)
{
Task.Run(() =>
{
DataGridData = new List<MyObject>(repository.GetData());
});
}
Everytime, when i click the button to load the data, the data is loaded to datagrid, but it takes a long time. So when i click the button more than once, the first data are loaded. I will start working with them and then new data are loaded again.
How can I stop previous tasks and load data only for last click button?
Thanks for your answers
You could maybe disable the button until the content is loaded?
You would have to set your button disabled property to have a dependency on a bool in your ViewModel and then set it to true when you are adding to the datagrid and then set it to false when you are done.
To do this, I am assuming you can raise events of some kind of:
NotifyPropertyChanged();
In XAML you would do something like this:
<Button IsEnabled="{Binding CanClose}"/>
Where CanClose is a bool in your ViewModel. When you change the state of the bool, you have to raise some kind of event. You can also disable the button first, then start filling the datagrid and when finished, enable it again?
Solved:
List<CancellationTokenSource> tokens = new List<CancellationTokenSource>();
public List<MyObject> DataGridData { get; set; }
public void LoadDataToDatagrid(object obj)
{
foreach (var token in tokens)
token.Cancel();
CancellationTokenSource tokenSource = new CancellationTokenSource();
tokens.Add(tokenSource);
var task = Task.Run(() =>
{
return repository.GetData();
}, tokenSource.Token);
try
{
DataGridData = await task;
}
finally
{
tokenSource.Dispose();
tokens.Remove(tokenSource);
}
}
That code show only data from last method call withou freeze application.
Thanks for answer Dmitry Bychenko.

Implement user interface command with background computing in wpf & mvvm

I have some difficulty with implementation user interface commands.
I use wpf, prism and mvvm. My app has two region - main and menu.
While app is loading in the menu region (NavBarControl, Devexpress) registering menu items (NavBarGroup). Each NavBarGroup has some NavBarItem. When a NavBarItem is selected the command which is bound executing. Some commands allow to create an entity. But for that app has to load some data from server and in this time the user interfacу should be responsive. I tried to reach that use next way:
this.createAccount.Command = (ICommand)new DelegateCommand(this.ExecuteCreateAccount);
private void ExecuteCreateAccount()
{
AppEvent.OnShowNotificationEvent(UTNotificationType.ChangeMainLoaderStatus, "show", null);
if (this.isCreateAccountProcessing)
{
return;
}
this.isCreateAccountProcessing = true;
Task.Factory.StartNew(() => this.AccountListViewModel.LoadUsersCollection()).GetAwaiter().OnCompleted(this.ShowAccountEditor);
}
private void ShowAccountEditor()
{
AppEvent.OnShowNotificationEvent(UTNotificationType.ChangeMainLoaderStatus, null, null);
this.isCreateAccountProcessing = false;
if (this.createAccount.IsSelected)
{
this.AccountListViewModel.CreateNewItem();
}
}
But maybe there is a better way to rich this goal?
While background computing takes place the app shows loader (AppEvent.OnShowNotificationEvent). If an user select another menu item the command is considered cancelled and account editor shouldn't be shown.
Since you are using the DevExpress framework, I suggest you to use the AsyncCommand. According to documentation, it designed for the scenarios like you've described.
Prism's DelegateCommand can handle async tasks. What about this:
this.createAccount.Command = (ICommand)new DelegateCommand(this.ExecuteCreateAccount);
private async Task ExecuteCreateAccount()
{
AppEvent.OnShowNotificationEvent(UTNotificationType.ChangeMainLoaderStatus, "show", null);
if (this.isCreateAccountProcessing)
{
return;
}
this.isCreateAccountProcessing = true;
await this.AccountListViewModel.LoadUsersCollection());
AppEvent.OnShowNotificationEvent(UTNotificationType.ChangeMainLoaderStatus, null, null);
this.isCreateAccountProcessing = false;
if (this.createAccount.IsSelected)
{
this.AccountListViewModel.CreateNewItem();
}
}
That is, if AccountListViewModel.LoadUsersCollection() can be made async. Otherwise you should wrap it in a Task.Run like this
await Task.Run( () => this.AccountListViewModel.LoadUsersCollection() );

Hide all visible Metro Dialogs before showing another one

I'm using MahApps.Metro on my WPF project, and I am building a class to help me showing Dialogs. I would like to know if there's a way of closing all visible dialogs before showing up another one.
Sometimes, when I show a ProgressDialog and then a MessageDialog the ProgressDialog isn't correctly closed, and stays in the background, so when I close the MessageDialog, it stays there freezing the UI.
Here's how I'm currently trying to hide all Dialogs:
public static async void HideVisibleDialogs(MetroWindow parent)
{
BaseMetroDialog dialogBeingShow = await parent.GetCurrentDialogAsync<BaseMetroDialog>();
while (dialogBeingShow != null)
{
await parent.HideMetroDialogAsync(dialogBeingShow);
dialogBeingShow = await parent.GetCurrentDialogAsync<BaseMetroDialog>();
}
}
I call it like this:
public static MessageDialogResult ShowMessage(String title, String message, MetroWindow parent, Int32 timeout, MessageDialogStyle style, MetroDialogSettings settings, MessageDialogResult defaultResult)
{
AutoResetEvent arEvent = new AutoResetEvent(false);
App.Current.Dispatcher.Invoke(() =>
{
HideVisibleDialogs(parent);
arEvent.Set();
});
arEvent.WaitOne();
[Rest of method]
}
Any help is appreciated. Thank you!
#EDIT
Apparently, the problem seems to be solved, thanks to Thomas Freudenberg
This is how it is now:
public static Task HideVisibleDialogs(MetroWindow parent)
{
return Task.Run(async () =>
{
await parent.Dispatcher.Invoke(async () =>
{
BaseMetroDialog dialogBeingShow = await parent.GetCurrentDialogAsync<BaseMetroDialog>();
while (dialogBeingShow != null)
{
await parent.HideMetroDialogAsync(dialogBeingShow);
dialogBeingShow = await parent.GetCurrentDialogAsync<BaseMetroDialog>();
}
});
});
}
And I call it like this:
HideVisibleDialogs(parent).Wait();
HideVisibleDialogs is an async method. I'd try to change its return type to Task and wait for it, i.e. HideVisibleDialogs(parent).Wait(). Otherwise the call would immediately return.

"Only a single ContentDialog can be open at any time." error while opening another contentdialog

I am using Windows.UI.Xaml.Controls.ContentDialog to show a confirmation. And based on the response from the first dialog I would (or would not) show another dialog. But, when I am trying to open the second content dialog it throws : "Only a single ContentDialog can be open at any time." error. Even though in the UI, first dialog would be closed but somehow I am still not able to open the second dialog. Any idea?
I have created some code to handle this type of conundrum in my Apps:
public static class ContentDialogMaker
{
public static async void CreateContentDialog(ContentDialog Dialog, bool awaitPreviousDialog) { await CreateDialog(Dialog, awaitPreviousDialog); }
public static async Task CreateContentDialogAsync(ContentDialog Dialog, bool awaitPreviousDialog) { await CreateDialog(Dialog, awaitPreviousDialog); }
static async Task CreateDialog(ContentDialog Dialog, bool awaitPreviousDialog)
{
if (ActiveDialog != null)
{
if (awaitPreviousDialog)
{
await DialogAwaiter.Task;
DialogAwaiter = new TaskCompletionSource<bool>();
}
else ActiveDialog.Hide();
}
ActiveDialog = Dialog;
ActiveDialog.Closed += ActiveDialog_Closed;
await ActiveDialog.ShowAsync();
ActiveDialog.Closed -= ActiveDialog_Closed;
}
public static ContentDialog ActiveDialog;
static TaskCompletionSource<bool> DialogAwaiter = new TaskCompletionSource<bool>();
private static void ActiveDialog_Closed(ContentDialog sender, ContentDialogClosedEventArgs args) { DialogAwaiter.SetResult(true); }
}
To use these Methods, you need to create the ContentDialog and its content in a variable, then pass the variable, and bool into the Method.
Use CreateContentDialogAsync(), if you require a callback in your app code, say if you have a button in your Dialog, and you want wait for a button press, and then get the value from the form in code after the dialog.
Use CreateContentDialog(), if you don't need to wait for the Dialog to complete in your UI Code.
Use awaitPreviousDialog to wait for the previous dialog to complete before showing the next Dialog, or set false, to remove the previous Dialog, then show the next Dialog, say, if you want to show an Error Box, or the next Dialog is more important.
Example:
await ContentDialogMaker.CreateContentDialogAsync(new ContentDialog
{
Title = "Warning",
Content = new TextBlock
{
Text = "Roaming Appdata Quota has been reached, if you are seeing this please let me know via feedback and bug reporting, this means that any further changes to data will not be synced across devices.",
TextWrapping = TextWrapping.Wrap
},
PrimaryButtonText = "OK"
}, awaitPreviousDialog: true);
William Bradley's approach above is good. Just to polish it up a bit, here is an extension method to submit and await the showing of a content dialog; the dialog will be shown after all the other content dialogs that have already been submitted. Note: by the time the user clicks through earlier backlogged dialogs you may no longer want to show the dialog that you have submitted; to indicate this you may pass a predicate that will be tested after the other dialogs have been dismissed.
static public class ContentDialogExtensions
{
static public async Task<ContentDialogResult> EnqueueAndShowIfAsync( this ContentDialog contentDialog, Func<bool> predicate = null)
{
TaskCompletionSource<Null> currentDialogCompletion = new TaskCompletionSource<Null>();
TaskCompletionSource<Null> previousDialogCompletion = null;
// No locking needed since we are always on the UI thread.
if (!CoreApplication.MainView.CoreWindow.Dispatcher.HasThreadAccess) { throw new NotSupportedException("Can only show dialog from UI thread."); }
previousDialogCompletion = ContentDialogExtensions.PreviousDialogCompletion;
ContentDialogExtensions.PreviousDialogCompletion = currentDialogCompletion;
if (previousDialogCompletion != null) {
await previousDialogCompletion.Task;
}
var whichButtonWasPressed = ContentDialogResult.None;
if (predicate == null || predicate()) {
whichButtonWasPressed = await contentDialog.ShowAsync();
}
currentDialogCompletion.SetResult(null);
return whichButtonWasPressed;
}
static private TaskCompletionSource<Null> PreviousDialogCompletion = null;
}
Another way might be to use a SemaphoreSlim(1,1).
"Only a single ContentDialog can be open at a time"
This statement is not entirely true. You can only ShowAsync one ContentDialog at a time. All you need to do is hide the current ContentDialog before opening another one. Then, after the "await ShowAsync" of the second ContentDailog, your simply call "var T = this.ShowAync()" to unhide it. Example:
public sealed partial class MyDialog2 : ContentDialog
{
...
}
public sealed partial class MyDialog1 : ContentDialog
{
...
private async void Button1_Click(object sender, RoutedEventArgs e)
{
// Hide MyDialog1
this.Hide();
// Show MyDialog2 from MyDialog1
var C = new MyDialog2();
await C.ShowAsync();
// Unhide MyDialog1
var T = ShowAsync();
}
}
I know this is slightly old, but one simpler solution instead of going through all this pain is to just register a callback for the ContentDialog_Closed event. By this point you can be sure the previous dialog has been closed, and can open your next dialog. :)
Only a single ContentDialog can be open at any time.
That is a fact. (I was really surprised, but just for a moment)
You can't have more than one at any time and it is more like guideline from Microsoft, because it's really messy to have multiple dialogs on top of each other filled with content.
Try to change your UX to display only one sophisticated ContentDialog and for all other messages use MessageDialog - it supports multiple buttons(only two for phones, but more on desktop) for user response but without Checkboxes or similar "smart"-content stuff.
In my case MessageDialogs were really helpful, but in some areas I used chained ContentDialogs but for that you must await the first one, and open second right after without any exceptions. In your case it seems like ContentDialog was not fully closed when you tried to open next one.
Hope it helps!
I like this answer https://stackoverflow.com/a/47986634/942855, this will allow us ot handle binding all events.
So extended it a little to check the multiple calls to show dialog.
private int _dialogDisplayCount;
private async void Logout_OnClick(object sender, RoutedEventArgs e)
{
try
{
_dialogDisplayCount++;
ContentDialog noWifiDialog = new ContentDialog
{
Title = "Logout",
Content = "Are you sure, you want to Logout?",
PrimaryButtonText = "Yes",
CloseButtonText = "No"
};
noWifiDialog.PrimaryButtonClick += ContentDialog_PrimaryButtonClick;
//await noWifiDialog.ShowAsync();
await noWifiDialog.EnqueueAndShowIfAsync(() => _dialogDisplayCount);
}
catch (Exception exception)
{
_rootPage.NotifyUser(exception.ToString(), NotifyType.DebugErrorMessage);
}
finally
{
_dialogDisplayCount = 0;
}
}
modified predicate
public class Null { private Null() { } }
public static class ContentDialogExtensions
{
public static async Task<ContentDialogResult> EnqueueAndShowIfAsync(this ContentDialog contentDialog, Func<int> predicate = null)
{
TaskCompletionSource<Null> currentDialogCompletion = new TaskCompletionSource<Null>();
// No locking needed since we are always on the UI thread.
if (!CoreApplication.MainView.CoreWindow.Dispatcher.HasThreadAccess) { throw new NotSupportedException("Can only show dialog from UI thread."); }
var previousDialogCompletion = _previousDialogCompletion;
_previousDialogCompletion = currentDialogCompletion;
if (previousDialogCompletion != null)
{
await previousDialogCompletion.Task;
}
var whichButtonWasPressed = ContentDialogResult.None;
if (predicate == null || predicate() <=1)
{
whichButtonWasPressed = await contentDialog.ShowAsync();
}
currentDialogCompletion.SetResult(null);
return whichButtonWasPressed;
}
private static TaskCompletionSource<Null> _previousDialogCompletion;
}

Button is Clicking Before File Upload is Complete in Webbrowser C#

On a Windows Form, I am using a Webbrowser control in C#. It's job is to upload a file and then press the submit button. My only problem is that my code tries to press the submit button before the file is finished uploading. I tried using:
System.Threading.Thread.Sleep(1000);
In between the two tasks (commented out below). This seems to pause the entire process so that didn't work. Can anyone tell me what the best way to do this is?
private void imageBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
this.imageBrowser.DocumentCompleted -= imageBrowser_DocumentCompleted;
Populate().ContinueWith((_) =>
{
//MessageBox.Show("Form populated!");
}, TaskScheduler.FromCurrentSynchronizationContext());
//System.Threading.Thread.Sleep(10000);
try
{
var buttons = imageBrowser.Document.GetElementsByTagName("button");
foreach (HtmlElement button in buttons)
{
if (button.InnerText == "done")
{
button.InvokeMember("click");
}
}
}
catch
{
//debug
}
}
async Task Populate()
{
var elements = imageBrowser.Document.GetElementsByTagName("input");
foreach (HtmlElement file in elements)
{
if (file.GetAttribute("name") == "file")
{
file.Focus();
await PopulateInputFile(file);
}
}
}
async Task PopulateInputFile(HtmlElement file)
{
file.Focus();
// delay the execution of SendKey to let the Choose File dialog show up
var sendKeyTask = Task.Delay(500).ContinueWith((_) =>
{
// this gets executed when the dialog is visible
SendKeys.Send("C:\\Users\\00I0I_c0OlVXtE6FO_600x450.jpg" + "{ENTER}");
System.Threading.Thread.Sleep(1000);
SendKeys.Send("{ENTER}");
}, TaskScheduler.FromCurrentSynchronizationContext());
file.InvokeMember("Click"); // this shows up the dialog
await sendKeyTask;
// delay continuation to let the Choose File dialog hide
await Task.Delay(500);
//SendKeys.Send("{ENTER}");
}
Is the WebBrowser loading a local file? Can you post the html code as well?
I came across such a situation when I was working with google-maps-api-3. I was setting some markers on the form in the WebBrowser_DocumentCompleted but was getting a null object exception. So I moved the code for set marker to a .NET Button control. I noticed that the exception was not thrown when I set the marker after the map tiles completed loading. DocumentCompleted was firing before the tiles got loaded and I was getting a null object exception.
So what I did was to use a tilesLoaded event in my javascript. In this event, I set a property back in C# code which set the markers in the OnPropertyChanged event.
I know what I am posting here is not a solution. But if you post your html code, I can give you answer with some code.
I solved this. The code I was using to click the button was in the wrong spot. The code now looks like so:
private void imageBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
this.imageBrowser.DocumentCompleted -= imageBrowser_DocumentCompleted;
try
{
Populate().ContinueWith((_) =>
{
var buttons = imageBrowser.Document.GetElementsByTagName("button");
foreach (HtmlElement button in buttons)
{
if (button.InnerText == "done")
{
button.InvokeMember("click");
}
}, TaskScheduler.FromCurrentSynchronizationContext());
}
catch
{
//debug
}
}
My mistake was thinking in terms of having a certain amount of seconds pass before executed the next line of code, when I should have been thinking in terms of having the next line of code execute when the previous task was complete.

Categories

Resources