can';t以其他形式更新(或连续写入)计数器';s文本框 我是中级C++程序员。但是对于VisualStudioGUI编程来说是新的。请相信我,我真的搜索了以前的错误,其他用户也得到了,但真的不能继续下去。非常感谢你的进一步帮助

can';t以其他形式更新(或连续写入)计数器';s文本框 我是中级C++程序员。但是对于VisualStudioGUI编程来说是新的。请相信我,我真的搜索了以前的错误,其他用户也得到了,但真的不能继续下去。非常感谢你的进一步帮助,c++,winforms,visual-studio,c++-cli,C++,Winforms,Visual Studio,C++ Cli,我只想在小屏幕上看到一个连续计数器。当我按下开始按钮时,计数器必须启动,直到按下停止按钮 我调试窗体项目时会显示窗口,但当我按下“开始”按钮时会出现错误: An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll Additional information: Cross-thread operation not valid: Control 't

我只想在小屏幕上看到一个连续计数器。当我按下开始按钮时,计数器必须启动,直到按下停止按钮

我调试窗体项目时会显示窗口,但当我按下“开始”按钮时会出现错误:

An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll

Additional information: Cross-thread operation not valid: Control 'textBox1' accessed from a thread other than the thread it was created on.
我还尝试了在/**/中选择代码部分,但我还是不能做我想做的事情

#include <windows.h>

using namespace System;
using namespace System::Threading;

int formcounter, xx;

#pragma once

namespace denemeform {

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;

    public ref class Form1 : public System::Windows::Forms::Form
    {

    public:
        Form1(void)
        {
            InitializeComponent();
            formcounter=0;
        }

    protected:
        ~Form1()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::Windows::Forms::Button^  button1;
    protected: 
    private: System::Windows::Forms::Button^  button2;
    private: System::Windows::Forms::TextBox^  textBox1;

    private:
        System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
        void InitializeComponent(void)
        {
            this->button1 = (gcnew System::Windows::Forms::Button());
            this->button2 = (gcnew System::Windows::Forms::Button());
            this->textBox1 = (gcnew System::Windows::Forms::TextBox());
            this->SuspendLayout();

            this->button1->Location = System::Drawing::Point(27, 24);
            this->button1->Name = L"button1";
            this->button1->Size = System::Drawing::Size(75, 23);
            this->button1->TabIndex = 0;
            this->button1->Text = L"&Start";
            this->button1->UseVisualStyleBackColor = true;
            this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);

            this->button2->Location = System::Drawing::Point(166, 24);
            this->button2->Name = L"button2";
            this->button2->Size = System::Drawing::Size(75, 23);
            this->button2->TabIndex = 1;
            this->button2->Text = L"Sto&p";
            this->button2->UseVisualStyleBackColor = true;
            this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);

            this->textBox1->Location = System::Drawing::Point(82, 71);
            this->textBox1->Name = L"textBox1";
            this->textBox1->Size = System::Drawing::Size(100, 20);
            this->textBox1->TabIndex = 2;

            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(269, 139);
            this->Controls->Add(this->textBox1);
            this->Controls->Add(this->button2);
            this->Controls->Add(this->button1);
            this->Name = L"Form1";
            this->Text = L"Form1";
            this->ResumeLayout(false);
            this->PerformLayout();
        }
#pragma endregion

        void Test() //test code:: when "Start" is pushed, this code is what executes
        {
            int formcounter=0; //test variable

            while(xx) 
            {
                formcounter++;
                this->textBox1->Text = formcounter.ToString(); //sets first textbox to 'a'
                Sleep(100); //pause for 1 second before continuing
            }
                /*formcounter++;
                if (this->InvokeRequired)
                this->Invoke(gcnew MethodInvoker(this, &Form1::Test));
            else
                this->textBox1->Text = formcounter.ToString();
                Sleep(1000);*/
        }

    private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
    {
        xx=1;
        /*Thread^ oThread = gcnew Thread( gcnew ThreadStart( this, &Form1::Test ) );
        oThread->Start();*/
        Thread^ tThread = gcnew Thread(gcnew ThreadStart(this, &Form1::Test));
        tThread->Start();
    }
    private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) 
    {
        xx=0;
    }
};
}
#包括
使用名称空间系统;
使用名称空间系统::线程;
int formcounter,xx;
#布拉格语一次
命名空间denemeform{
使用名称空间系统;
使用名称空间System::ComponentModel;
使用名称空间系统::集合;
使用命名空间System::Windows::Forms;
使用名称空间System::Data;
使用名称空间系统::绘图;
public ref类Form1:public System::Windows::Forms::Form
{
公众:
表格1(无效)
{
初始化组件();
formcounter=0;
}
受保护的:
~Form1()
{
if(组件)
{
删除组件;
}
}
私有:系统::Windows::窗体::按钮^button1;
受保护的:
私有:系统::Windows::窗体::按钮^button2;
private:System::Windows::Forms::TextBox^textBox1;
私人:
系统::组件模型::容器^组件;
#pragma区域Windows窗体设计器生成的代码
void初始化组件(void)
{
此->按钮1=(gcnew System::Windows::Forms::Button());
此->按钮2=(gcnew System::Windows::Forms::Button());
这->textBox1=(gcnewsystem::Windows::Forms::TextBox());
此->SuspendLayout();
此->按钮1->位置=系统::图纸::点(27,24);
此->按钮1->名称=L“按钮1”;
该->按钮1->尺寸=系统::图纸::尺寸(75,23);
此->按钮1->选项卡索引=0;
此->按钮1->Text=L“&Start”;
此->按钮1->UseVisualStyleBackColor=true;
此->按钮1->Click+=gcnewsystem::EventHandler(此,&Form1::按钮1\u Click);
此->按钮2->位置=系统::图纸::点(166,24);
此->按钮2->名称=L“按钮2”;
该->按钮2->尺寸=系统::图纸::尺寸(75,23);
此->按钮2->选项卡索引=1;
此->按钮2->文本=L“Sto&p”;
此->按钮2->UseVisualStyleBackColor=true;
此->按钮2->单击+=gcnew System::EventHandler(此,&Form1::按钮2\u单击);
此->文本框1->位置=系统::绘图::点(82,71);
此->textBox1->Name=L“textBox1”;
此->文本框1->尺寸=系统::图纸::尺寸(100,20);
此->文本框1->TabIndex=2;
这->自动缩放尺寸=系统::图纸::尺寸(6,13);
这->AutoScaleMode=System::Windows::Forms::AutoScaleMode::Font;
这->ClientSize=System::Drawing::Size(269139);
此->控制->添加(此->文本框1);
此->控制->添加(此->按钮2);
此->控制->添加(此->按钮1);
此->名称=L“Form1”;
此->文本=L“Form1”;
此->恢复布局(错误);
此->执行布局();
}
#布拉格端区
void Test()//测试代码::当按下“Start”时,将执行此代码
{
int formcounter=0;//测试变量
while(xx)
{
formcounter++;
this->textBox1->Text=formcounter.ToString();//将第一个文本框设置为“a”
Sleep(100);//继续之前暂停1秒
}
/*formcounter++;
如果(此->调用需要)
this->Invoke(gcnewmethodinvoker(this,&Form1::Test));
其他的
此->textBox1->Text=formcounter.ToString();
睡眠(1000)*/
}
私有:系统::无效按钮1\u单击(系统::对象^sender,系统::事件参数^e)
{
xx=1;
/*Thread^oThread=gcnewthread(gcnewthreadstart(this,&Form1::Test));
oThread->Start()*/
线程^tThread=gcnew-Thread(gcnew-ThreadStart(this,&Form1::Test));
tThread->Start();
}
私有:系统::无效按钮2\u单击(系统::对象^sender,系统::事件参数^e)
{
xx=0;
}
};
}

您没有说使用注释掉的代码时会发生什么,但我相信我看到了发生的情况

您确实需要使用invokererequired&
this->Invoke
,但是您将错误的内容传递给了
this->Invoke
。您正在传递一个包含循环的方法(Form1::Test),但它不会返回。您应该传递一个只设置一次文本框的委托

尝试将
formcounter
移动为类字段而不是局部变量,并执行以下操作:

bool xx;
int formcounter;

void SetTextBoxOnce()
{
    this->textBox1->Text = formcounter->ToString();
}

void TestThread()
{
    this->formcounter = 0;
    while(xx)
    {
        this->formcounter++;
        // We're running on a newly created thread, we know that we ALWAYS need to Invoke.
        this->Invoke(gcnew MethodInvoker(this, &Form1::SetTextBoxOnce));
        Thread::Sleep(1000);
    }
}

代码现在可以正确执行

void SetTextBoxOnce()
{
    this->textBox1->Text = formcounter.ToString(); 
}

void Test()  
{
    formcounter=0; //test variable

    while(xx) 
    {
        formcounter++;
    this->Invoke(gcnew MethodInvoker(this, &Form1::SetTextBoxOnce));
        Sleep(100);  
        }
    }

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
{
    xx=1;
    Thread^ tThread = gcnew Thread(gcnew ThreadStart(this, &Form1::Test));
    tThread->Start();
}
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) 
{
    xx=0;
}

代码现在执行正确。

我还无法管理。我想我首先要学习如何处理事件和运行异步事件。感谢上帝。非常感谢大卫·欧。
void SetTextBoxOnce()
{
    this->textBox1->Text = formcounter.ToString(); 
}

void Test()  
{
    formcounter=0; //test variable

    while(xx) 
    {
        formcounter++;
    this->Invoke(gcnew MethodInvoker(this, &Form1::SetTextBoxOnce));
        Sleep(100);  
        }
    }

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
{
    xx=1;
    Thread^ tThread = gcnew Thread(gcnew ThreadStart(this, &Form1::Test));
    tThread->Start();
}
private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) 
{
    xx=0;
}