C ++表单在开始时没有响应

时间:2013-10-01 17:13:05

标签: winforms visual-c++ c++-cli

我正在开发我的第一个需要表单的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);
    }
}

form.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-&gt; Show();.如果我把它切换到ShowDialog();它不再没有反应,不幸的是进度条的值也没有更新,我认为这是多线程发挥作用的地方。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

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

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

使用&#34; Form1&#34;作为隐藏的基本表单,它用于通过刷新计时器来更新应用程序。

使用&#34; Form2&#34;以向用户显示的形式。为此,您应该运行&#34; Form2&#34;在里面&#34; Form1&#34;使用Show()ShowDialog()

这意味着:

// This is Form1 or base.
#include "Form2.h"
Form2 ^openForm2 = gcnew Form2();
openForm2->Show();
// while(true) Refresh the other form and other codes...