Modification of Utility Method to Support Multi-Threading

MoonKnight

I have been given a application to multi-thread and this has been done. All message boxes in this application are correctly called by a single static method in a utility class. This method is:

public static void ErrMsg(String strMsg, Exception ex = null)
{
    ...
    if (ex == null)
        MessageBox.Show(strMsg, "MyApp", MessageBoxButtons.OK, MessageBoxIcon.Warning);
    ...
}

Clearly this does not provide the IWin32Window owner and this is now causing me a problem when I invoke an error message from a background thread-pool thread using this method. The problem is a known one of the message box showing behind my main form. See:

  1. Popping a MessageBox for the main app with Backgroundworker in WPF

et al. I could pass in the SynchronisationContext of the current thread to ErrMsg and do

synchronizationContext.Send(callback => 
    MessageBox.Show("Some error message for the user"), null);

But there are 700+ calls to this method, the majority of which are on the UI thread and do not cause a problem. My question is: how can I amend the ErrMsg method so that my message box appears in front reguardless of the current SynchronisationContex and without having to ammend all 700+ calls to the method?

Thanks for your time.


Edit. @Dmitry's idea was great. However, if the active form does not have focus or is an MdiChild form then the Form.ActiveForm will return null. To get around this I use Application.OpenForms.Cast<Form>().Last() to get the last active form, beit MdiChild or whatever. The code is now...

Form activeForm = Application.OpenForms.Cast<Form>().Last();
if (ex == null)
{
    activeForm.Invoke(new ShowErrMsg(text => 
        MessageBox.Show(activeForm, 
            text, 
            "UserCost",
            MessageBoxButtons.OK, 
            MessageBoxIcon.Warning)), 
        strMsg);
}
Dmitry

Try to use the Form.ActiveForm static property as a first argument for MessageBox.Show(...). Also the thread-safe invocation should be used.
Example:

private delegate void ShowErrMsgMethod(string text);

public static void ErrMsg(String strMsg, Exception ex = null)
{
    ...
    if (ex == null)
    {
        Form activeForm = Form.ActiveForm ?? Application.OpenForms.Cast<Form>().Last();
        activeForm.Invoke(new ShowErrMsgMethod(text => MessageBox.Show(activeForm, text, "MyApp", MessageBoxButtons.OK, MessageBoxIcon.Warning)), strMsg);
    }
    ...
}

EDIT: Improved for a circumstances when other forms are opened. The reference to main form was eliminated.
EDIT2: Improved for a circumstances when Form.ActiveForm == null.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Does Entity Framework support Multi-Threading?

From Dev

Does the Canon MF Scan Utility Support scanning multi page pdfs?

From Dev

Does C++ provide built in multi threading support?

From Dev

Will be whether multi-threading support in mongodb ( without locking database)

From Dev

Does Swift have any native concurrency and multi-threading support?

From Dev

Adding multi-threading possibility to a single-threaded all-files-in-directory iterator utility function

From Dev

Multi threading

From Dev

Multi threading

From Dev

multi-threading: thread gets own copy of method, but parameter is shared

From Dev

Multi Threading perform different task in one Run Method

From Dev

Start a method when another finishes using multi-threading

From Dev

Multi processing / Multi threading in BASH

From Dev

Multi threading issues with database

From Dev

JavaScriptCompressor, CssCompressor and multi threading

From Dev

Multi Threading in Android

From Dev

How to avoid multi Threading

From Dev

Multi threading database reading

From Dev

JMS with akka and multi threading

From Dev

Multi-threading with bash

From Dev

Multi threading unit test

From Dev

Java Multi threading semaphore

From Dev

Multi threading JMX client

From Dev

Multi-threading and queuing

From Dev

Multi threading with ObservesProperty

From Dev

Multi Threading Java ScriptEngine

From Dev

Multi threading Raytracer

From Dev

cURL Multi Threading?

From Dev

Static Variables and Multi Threading

From Dev

JavaScriptCompressor, CssCompressor and multi threading