Frank Schwieterman
Frank Schwieterman

Reputation: 24478

how to run winforms app in debug mode outside of Visual Studio

When I launch a winform application from visual studio, it does extra runtime checks to check for errors. In particular it will throw an exception if I access a form element outside of the thread it was created, with text "Cross-thread operation not valid".

When I run my integration tests, which start the process outside of visual studio, that checking is not enabled. I am running the executable build result, except starting it with Process.Start() and perhaps custom command-line arguments.

How can I enable that run-time checking when I run the executable outside of visual studio?

Upvotes: 0

Views: 389

Answers (2)

Hans Passant
Hans Passant

Reputation: 942399

This is controlled by the Control.CheckForIllegalCrossThreadCalls property. It is initialized with the value of Debugger.IsAttached. Simply set it to true to force checking even when your program isn't being debugged. For example:

    private void button1_Click(object sender, EventArgs e) {
        Control.CheckForIllegalCrossThreadCalls = true;
        var t = new System.Threading.Thread(() => {
            try {
                this.Text = "kaboom";
            }
            catch (Exception ex) {
                MessageBox.Show(ex.Message);
            }
        });
        t.Start();
    }

This brings up the message box when you start the program with Ctrl+F5 or run it outside of Visual Studio.

Upvotes: 2

ssmithstone
ssmithstone

Reputation: 1209

it builds a debug and release version can you not run the debug version in your integration tests?

Upvotes: 0

Related Questions