I’ve created my custom MessageBox using MessagePrompt from the Coding4Fun toolkit.
The problem occurs when I run ResetData_Click. I expected that after launching ComplexMessage.Show rest of the code inside ResetData_Click stops executing while ComplexMessage is open. As occurred it is completely different. All code is executed at once and it doesn’t matter what user will chose in ComplexMessage because
if (ComplexMessage.Result)...
is already executed.
What should I do to make my ComplexMessage act like System.Windos.MessageBox? It means when MessageBox is called the parent’s thread is waiting for the user’s decision.
private void ResetData_Click(object sender, RoutedEventArgs e)
{
ComplexMessage.Show("You are about to delete all data", "Are you sure?", true);
if (ComplexMessage.Result)
{
DataControl.DataFileReset();
}
}
public class ComplexMessage
{
private static MessagePrompt messageprompt;
private static bool messageresult;
public static void Show(string message, string title, bool vibrate)
{
if (!(!(messageprompt == null) && messageprompt.IsOpen))
{
messageprompt = new MessagePrompt
{
Title = title,
Message = message
};
messageprompt.Completed += new EventHandler<PopUpEventArgs<string, PopUpResult>>(messageprompt_Completed);
messageprompt.IsCancelVisible = true;
messageprompt.Show();
if (vibrate) { Tools.VibrateMessage(); }
}
}
static void messageprompt_Completed(object sender, PopUpEventArgs<string, PopUpResult> e)
{
if (!e.PopUpResult.Equals(PopUpResult.Cancelled))
{
messageresult = true;
}
else
{
messageresult = false;
}
((MessagePrompt)sender).Completed -= messageprompt_Completed;
}
public static bool Result
{
get { return messageresult; }
}
}
Since you are displaying the MessageBox from a click event, you are running on the UI thread, which you don’t want to freeze.
One option is to make ComplexMessage expose a static event, which it fires in messageprompt_Completed.
Then in ResetData_Click subscribe to the event prior to calling ComplexMessage.Show, and in the event handler, depending on the result, call DataControl.DataFileReset, and unsubscribe.
An alternative is to rethink making the members of ComplexMessage static, and instead to pass an “Action<bool> callback” parameter to the Show method, which you store away in a private member, and then invoke the callback in messageprompt_Completed.