-1

我正在处理我的第一个需要表单的 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(); 它不再没有响应,不幸的是进度条的值也没有更新,我相信这就是多线程发挥作用的地方。

4

2 回答 2

0

我认为你应该使用ShowDialog这样你的表单有一个消息循环,并将你的“每 50 毫秒”的东西放在一个监听你的计时器事件的事件处理程序Tick中。

于 2013-10-02T13:05:18.947 回答
0

作为您应该始终使用的第一个表单System::Windows::Forms::Application::Run(CDwin),您只能在已经运行表单时使用Show()和方法。ShowDialog()

如果你使用Run()你会遇到一些问题。此函数仅在表单关闭后返回一些内容,这意味着代码之后的Run()代码在表单关闭之前不会运行。

所以你必须忘记在主函数中使用你的表单。我建议您创建两个表单,例如“Form1”和“Form2”(使用线程是另一种方式,但这种方式更容易)。

使用“Form1”作为隐藏的基本表单,用于通过刷新计时器来更新应用程序。

使用“Form2”作为向用户显示的表单。为此,您应该使用Show()or在“Form1”中运行“Form2” ShowDialog()

所以这意味着:

// This is Form1 or base.
#include "Form2.h"
Form2 ^openForm2 = gcnew Form2();
openForm2->Show();
// while(true) Refresh the other form and other codes...
于 2014-01-12T14:06:51.403 回答