We have the following method in our application to show dialog boxes. The method is called from UI thread and non UI threads. Is it OK to call InvokeRequired on messageDialog to ensure that the dialog will be displayed on the UI thread?
public static DialogResult ShowMessageDialog(MessageInfo messageInfo,ButtonFlags flags,IconType iconId,HorizontalAlignment btnAlignment,bool topMost)
{
DialogResult retDialogResult = DialogResult.None;
MessageDialog messageDialog = new MessageDialog(messageInfo.GetLanguageBasedFieldValue(MessageInfoField.Message), iconId);
messageDialog.TopMost = topMost;
// Initialize the layout of Message Dialog.
messageDialog.Initialize();
retDialogResult = messageDialog.ShowDialog();
return retDialogResult;
}
No,
InvokeRequiredwill never return true becauseMessageDialogis constructed on the thread you are executing on. Also,InvokeRequiredwill always return false until the control’s handle is created, which doesn’t happen until later (see MSDN).Either make sure you always call
ShowMessageDialogfrom the UI thread or have a way forShowMessageDialogto get onto the UI thread, such as storing aISynchronizeInvoke. I think the former is a better solution.