无法在表单的文本框中更新(或连续写入)计数器
can't update (or write continuously) counter in ther form's textbox
我是中级 c++ 程序员。但是Visual Studio GUI编程的新手。请相信我,我确实搜索了其他用户也得到的以前的错误,但真的无法继续下去。非常感谢您的进一步帮助。
我只想在小屏幕上看到一个连续的计数器。当我按下"开始"按钮时,计数器必须启动,直到按下"停止"按钮。
我调试表单项目时显示窗口,但是当我按"开始"按钮时,出现错误:
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;
}
};
}
代码现在可以正确执行。
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;
}
代码现在可以正确执行。
你没有说当你使用注释掉的代码时会发生什么,但我相信我看到了发生了什么。
你确实需要使用InvokeRequired和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);
}
}
相关文章:
- 模板元程序查找相似的连续类型名称
- 循环在计数器中不起作用
- EASTL矢量<向量<int>>连续的
- python集合的C++等价物是什么.计数器
- 当需要超过16GB的连续内存时,内存分配失败
- 我应该如何表示我拥有的连续元素序列?
- Python 集合.计数器,如何避免重复查找
- 蛇在C++不会连续转两次
- 有没有一种优雅而快速的方法来测试整数中的 1 位是否位于连续区域
- 请解释字谜的代码,我看不懂计数器数组,每个值已经是0
- 如何知道n!是否可以表示为三个连续数字的乘法?
- 连续枚举 C++11
- 两个连续的 OpenMP 并行区域会相互减慢速度
- 可以将两个相同类型的连续数组视为一个数组吗?
- C++,您能否设计一种数据结构,将指针保存在连续内存中并且不会使它们失效?
- 我已经建立了递归关系,它找到了两个字符串之间最长的连续公共字符串,我怎么能跳过其中一个字符串中的一个字符
- 这种用于查找连续子数组中最大和的递归算法有什么优势吗?
- std::shared_ptr 自定义参考计数器
- 英特尔 TBB 程序不会终止,可能会误用参考计数器
- 无法在表单的文本框中更新(或连续写入)计数器