程序未在调试模式下运行。但它没有调试模式运行。 [英] Program not running in debug mode. but it runs without debug mode.

查看:143
本文介绍了程序未在调试模式下运行。但它没有调试模式运行。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了简单的线程程序。它运行时没有调试模式。但是启动调试模式它不会运行。程序每次都会崩溃。怎么纠正这个



我尝试了什么:



i googled my问题。我找不到任何解决方案。



代码尝试:



i created simple threading program. it runs without debug mode. but start debugging mode it does not run.the program will crash every time. how to correct this

What I have tried:

i googled my problem.and i cant find any solution.

Code Try:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;


namespace thread_test_2
{
  public partial class Form1 : Form
  {
    Thread t;
    public Form1()
    {
       InitializeComponent();
    }
    public void tw()
    {
      for (int i = 0; i < 10000; i++)
      {
        textBox1.Text = i.ToString();
      }
    }
    private void button1_Click(object sender, EventArgs e)
    {
      t = new Thread(tw);
      t.Start();
    }
  }
}

推荐答案

您正在访问您创建的主题中的文本框。您在 tw 方法中执行此操作。这是不允许的。



您只是在调试时收到此错误,因为只有连接了调试器才会检查非法的跨线程调用。以下是来自 MSDN [ ^ ](第300行)。



You are accessing the textbox in the thread you have created. You are doing this in your tw method. This is not allowed.

You are only getting this error while debugging because illegal cross thread calls are only checked if debugger is attached. Here is the relevant part of code from MSDN[^] (Line 300).

// Initially check for illegal multithreading based on whether the
// debugger is attached.
[ResourceExposure(ResourceScope.Process)]
private static bool checkForIllegalCrossThreadCalls = Debugger.IsAttached;







你应该看一下 BackgroundWorker 。它可以帮助您实现您在此处尝试的操作。




You should take a look at BackgroundWorker. It may help you achieve what you are trying to do here.


作为使用后台工作程序的替代方法,您可以使用BeginInvoke来确保在主用户界面线程上更改文本。 br $> b $ b

此CodeProject文章提供了非常好的概述,并且有一个代码示例,几乎完全符合修复示例代码所需的内容。
As an alternative to using a background worker you can use BeginInvoke to ensure the text is changed on the main user interface thread.

This CodeProject article gives a very good overview and has a code example doing almost exactly what you need to fix your sample code.


这篇关于程序未在调试模式下运行。但它没有调试模式运行。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆