我正在处理我的第一个需要表单的 C++ 项目,但我似乎已经过时了。很大程度上,我认为我的问题与我没有对表单进行多线程处理有关,但我不确定正确的实现。我希望有人能准确地指出我去了哪里,让自己看起来像个布偶。(警告虚心,我使用全局变量来快速创建概念证明,一旦我的一切工作或多或少正确,我会回去并妥善保护一切)
*编辑:我想澄清一下,看起来问题是我在主线程中执行所有内容,可以为整个表单创建一个新线程,或者我是否需要为每个单独的控件创建一个新线程表格?
Winmain.cpp
初始化表单并更新表单信息/刷新表单的主要功能。
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
using namespace Interface;
cooldowns ^ CDwin = gcnew cooldowns;
CDwin->Show();
CDwin->Location = Point(0,0);
while (true)
{
CDwin->timer1->Text = timer1Duration.ToString();
CDwin->timer1Progress->Value = timer1Value;
CDwin->timer1->Refresh();
CDwin->timer1Progress->Refresh();
//collect info to populate CDwin values for next cycle
//something tells me this sleep could be part of the problem?
Sleep(50);
}
}
表格.h
#pragma once
namespace Interface {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Threading;
/// <summary>
/// Summary for cooldowns
/// </summary>
public ref class cooldowns : public System::Windows::Forms::Form
{
public:
cooldowns(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~cooldowns()
{
if (components)
{
delete components;
}
}
public: System::Windows::Forms::ProgressBar^ timer1Progress;
protected:
public: System::Windows::Forms::Label^ timer1;
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->timer1Progress = (gcnew System::Windows::Forms::ProgressBar());
this->timer1 = (gcnew System::Windows::Forms::Label());
this->SuspendLayout();
//
// timer1Progress
//
this->timer1Progress->ForeColor = System::Drawing::Color::Fuchsia;
this->timer1Progress->Location = System::Drawing::Point(3, 12);
this->timer1Progress->Maximum = 30000;
this->timer1Progress->Name = L"timer1Progress";
this->timer1Progress->Size = System::Drawing::Size(244, 18);
this->timer1Progress->Style = System::Windows::Forms::ProgressBarStyle::Continuous;
this->timer1Progress->TabIndex = 0;
this->timer1Progress->Value = 10000;
//
//timer1
//
this->timer1->AutoSize = true;
this->timer1->ForeColor = System::Drawing::Color::White;
this->timer1->Location = System::Drawing::Point(253, 108);
this->timer1->Name = L"Timer1";
this->timer1->Size = System::Drawing::Size(34, 13);
this->timer1->TabIndex = 9;
this->timer1->Text = L"00.00";
//
// cooldowns
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->BackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(64)), static_cast<System::Int32>(static_cast<System::Byte>(64)),
static_cast<System::Int32>(static_cast<System::Byte>(64)));
this->ClientSize = System::Drawing::Size(294, 138);
this->Controls->Add(this->Timer1);;
this->Controls->Add(this->timer1Progress);
this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::None;
this->Name = L"cooldowns";
this->StartPosition = System::Windows::Forms::FormStartPosition::Manual;
this->Text = L"cooldowns";
this->TopMost = true;
this->Load += gcnew System::EventHandler(this, &cooldowns::cooldowns_Load);
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void cooldowns_Load(System::Object^ sender, System::EventArgs^ e) {
}
};
}
经过大量试验和错误后进行编辑,问题似乎围绕着 CDwin->Show();。如果我将其切换到 ShowDialog(); 它不再没有响应,不幸的是进度条的值也没有更新,我相信这就是多线程发挥作用的地方。