Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the cross-threading exception raises only when debugging? [duplicate]

I have a small app that implements a plugin system based on MEF. The application load plugins dynamically, and new plugins can be placed in the plugins folder at any time. In order to achieve this, I use automatic recomposition and the event Changed, that raises when new parts are available. When certain new parts are available, I update a listbox in the UI.

The code that caused problems was in the method called when the Changed event fired:

public void OnUserViewPluginCatalogChanged
             (object sender, ComposablePartCatalogChangeEventArgs e)
{
    listBox1.Items.Clear();
    foreach (var item in fPluginStore.PluginsAvailable)
        listBox1.Items.Add(item.Metadata["Caption"] as string);
}

When the above method was called from the UI thread, like in the Main Form ctor, everything was ok. But as soon as I would place a new plugin in the plugins folder, this method would be called and, while in a "normal execution" (calling the app from outside VS 2010 or through ctrl+F5) it seemed to work, showing in the listbox the new plugin, when calling from inside VS 2010 with F5 (debugging), it would raise an exception with the bessage Control 'listbox1' accessed from a thread other than the thread it was created on.

I soved this problem through the code below:

public void OnUserViewPluginCatalogChanged
             (object sender, ComposablePartCatalogChangeEventArgs e)
{
    if( listBox1.InvokeRequired )
    {            
        this.Invoke((MethodInvoker) delegate { listBox1.Items.Clear(); });
        foreach (var item in fPluginStore.PluginsAvailable)
            this.Invoke((MethodInvoker) delegate 
                        {listBox1.Items.Add(item.Metadata["Caption"] as string);});
    }
    else
    {    
        listBox1.Items.Clear();
        foreach (var item in fPluginStore.PluginsAvailable)
           listBox1.Items.Add(item.Metadata["Caption"] as string);
    }
}

My question is why the exception raised only in debug mode?

Is there an option that in debugging mode is active that check for this kind of cross-threading problem that is not active in Release mode?

I'm assuming that the problem is therein release mode, but for some reason it do not shows up because is not being checked.

Or am I missing something?

Thanks in advance!

like image 711
Jauch Avatar asked Mar 22 '23 11:03

Jauch


1 Answers

Those exceptions are only checked is the debugger is attached to the process, so they don't show up when running the process without debugging (or standalone outside of VS).

This behavior can be controlled through CheckForIllegalCrossThreadCalls, a static property exposed on the Control class. I strongly advice against disabling it though, those exceptions are thrown to let the developer know something is potentially very wrong with their multi-threading (Control-derived classes are not thread-safe)

like image 172
Alex Avatar answered Apr 14 '23 17:04

Alex