开发者

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

开发者 https://www.devze.com 2023-02-19 22:00 出处:网络
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 wa

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 arg开发者_StackOverflow社区uments.

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


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号