Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 未显示在其他线程中运行的Windows窗体_.net_Winforms_Visual Studio 2010_Visual C++_C++ Cli - Fatal编程技术网

.net 未显示在其他线程中运行的Windows窗体

.net 未显示在其他线程中运行的Windows窗体,.net,winforms,visual-studio-2010,visual-c++,c++-cli,.net,Winforms,Visual Studio 2010,Visual C++,C++ Cli,请看一下下面的代码 #pragma once #include "Email.h" using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Threading;

请看一下下面的代码

#pragma once

#include "Email.h"

    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Threading;
    using namespace SMSLib;

    /// <summary>
    /// Summary for NotifyAlarm
    /// </summary>
    public ref class NotifyAlarm : public System::Windows::Forms::Form
    {
        int count;
        Email ^email;

    public:
        NotifyAlarm(void)
        {
            InitializeComponent();
            //
            //TODO: Add the constructor code here
            //
            count = 10;
            //email = gcnew Email();
        }

    protected:
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        ~NotifyAlarm()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::Windows::Forms::Label^  label1;
    protected: 
    private: System::Windows::Forms::Label^  secondsLabel;
    private: System::Windows::Forms::Label^  label2;
    private: System::Windows::Forms::Button^  sendNowBtn;
    private: System::Windows::Forms::Button^  cancelBtn;
    private: System::Windows::Forms::Timer^  timer1;
    private: System::ComponentModel::IContainer^  components;


    private:
        /// <summary>
        /// Required designer variable.
        /// </summary>


#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->components = (gcnew System::ComponentModel::Container());
            this->label1 = (gcnew System::Windows::Forms::Label());
            this->secondsLabel = (gcnew System::Windows::Forms::Label());
            this->label2 = (gcnew System::Windows::Forms::Label());
            this->sendNowBtn = (gcnew System::Windows::Forms::Button());
            this->cancelBtn = (gcnew System::Windows::Forms::Button());
            this->timer1 = (gcnew System::Windows::Forms::Timer(this->components));
            this->SuspendLayout();
            // 
            // label1
            // 
            this->label1->AutoSize = true;
            this->label1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
                static_cast<System::Byte>(0)));
            this->label1->Location = System::Drawing::Point(13, 27);
            this->label1->Name = L"label1";
            this->label1->Size = System::Drawing::Size(405, 20);
            this->label1->TabIndex = 0;
            this->label1->Text = L"Intruder Detected. An Email and SMS will be sent within ";
            // 
            // secondsLabel
            // 
            this->secondsLabel->AutoSize = true;
            this->secondsLabel->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
                static_cast<System::Byte>(0)));
            this->secondsLabel->ForeColor = System::Drawing::Color::Red;
            this->secondsLabel->Location = System::Drawing::Point(408, 27);
            this->secondsLabel->Name = L"secondsLabel";
            this->secondsLabel->Size = System::Drawing::Size(51, 20);
            this->secondsLabel->TabIndex = 1;
            this->secondsLabel->Text = L"label2";
            // 
            // label2
            // 
            this->label2->AutoSize = true;
            this->label2->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
                static_cast<System::Byte>(0)));
            this->label2->Location = System::Drawing::Point(465, 27);
            this->label2->Name = L"label2";
            this->label2->Size = System::Drawing::Size(69, 20);
            this->label2->TabIndex = 2;
            this->label2->Text = L"seconds";
            // 
            // sendNowBtn
            // 
            this->sendNowBtn->Location = System::Drawing::Point(370, 70);
            this->sendNowBtn->Name = L"sendNowBtn";
            this->sendNowBtn->Size = System::Drawing::Size(75, 23);
            this->sendNowBtn->TabIndex = 3;
            this->sendNowBtn->Text = L"Send Now";
            this->sendNowBtn->UseVisualStyleBackColor = true;
            this->sendNowBtn->Click += gcnew System::EventHandler(this, &NotifyAlarm::sendNowBtn_Click);
            // 
            // cancelBtn
            // 
            this->cancelBtn->Location = System::Drawing::Point(469, 70);
            this->cancelBtn->Name = L"cancelBtn";
            this->cancelBtn->Size = System::Drawing::Size(75, 23);
            this->cancelBtn->TabIndex = 4;
            this->cancelBtn->Text = L"Cancel";
            this->cancelBtn->UseVisualStyleBackColor = true;
            this->cancelBtn->Click += gcnew System::EventHandler(this, &NotifyAlarm::cancelBtn_Click);
            // 
            // timer1
            // 
            this->timer1->Enabled = true;
            this->timer1->Interval = 1000;
            this->timer1->Tick += gcnew System::EventHandler(this, &NotifyAlarm::timer1_Tick);
            // 
            // NotifyAlarm
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(566, 105);
            this->Controls->Add(this->cancelBtn);
            this->Controls->Add(this->sendNowBtn);
            this->Controls->Add(this->label2);
            this->Controls->Add(this->secondsLabel);
            this->Controls->Add(this->label1);
            this->Name = L"NotifyAlarm";
            this->Text = L"NotifyAlarm";
            this->ResumeLayout(false);
            this->PerformLayout();

        }
#pragma endregion
    private: System::Void timer1_Tick(System::Object^  sender, System::EventArgs^  e) 
             {
                 count--;
                 DoUpdateText(count);

             }
private: System::Void sendNowBtn_Click(System::Object^  sender, System::EventArgs^  e) 
         {
             timer1->Stop();


         }

public: System::Void showGUI() 
                  {
                      this->Show();
                  }
private: System::Void cancelBtn_Click(System::Object^  sender, System::EventArgs^  e) 
         {
             timer1->Stop();
             this->Close();
         }





        delegate void UpdateTextDelegate(int count);

private: void DoUpdateText(int count)
         {
             ISynchronizeInvoke^ i = this;

             if (i->InvokeRequired)
             {
                 UpdateTextDelegate^ tempDelegate =
                     gcnew UpdateTextDelegate(this, &NotifyAlarm::DoUpdateText);
                 cli::array<System::Object^>^ args = gcnew cli::array<System::Object^>(1);
                 args[0] = count;
                 i->BeginInvoke(tempDelegate, args);
                 return;
             }

             if(count>0 || count==0)
             {
                 secondsLabel->Text = ""+count;
             } 
             else
             {
                 timer1->Stop();



                 this->Close();

             }
         }
};
我遇到的问题是,表单显示得很好,但过了一秒钟,它就消失了。无论在表单中运行的进程看起来是在运行,但是在后台,没有显示表单

请注意:

    delegate void UpdateTextDelegate(int count);

private: void DoUpdateText(int count)
         {
             ISynchronizeInvoke^ i = this;

             if (i->InvokeRequired)
             {
                 UpdateTextDelegate^ tempDelegate =
                     gcnew UpdateTextDelegate(this, &NotifyAlarm::DoUpdateText);
                 cli::array<System::Object^>^ args = gcnew cli::array<System::Object^>(1);
                 args[0] = count;
                 i->BeginInvoke(tempDelegate, args);
                 return;
             }

             if(count>0 || count==0)
             {
                 secondsLabel->Text = ""+count;
             } 
             else
             {
                 timer1->Stop();



                 this->Close();

             }
         }
};
我犯了这个错误

1>c:\users\yohan\documents\visual studio 2010\projects\ eesystem\eesystem\NotifyAlarm.h(182): error C2665: 'System::Windows::Forms::Application::Run' : none of the 3 overloads could convert all the argument types
1>          c:\program files\reference assemblies\microsoft\framework\.netframework\v4.0\system.windows.forms.dll: could be 'void System::Windows::Forms::Application::Run(System::Windows::Forms::Form ^)'
1>          c:\program files\reference assemblies\microsoft\framework\.netframework\v4.0\system.windows.forms.dll: or       'void System::Windows::Forms::Application::Run(System::Windows::Forms::ApplicationContext ^)'
1>          while trying to match the argument list '(NotifyAlarm)'

这里的问题是,您只是在调用
this->Show()
。这最多只能显示一次表单。它不会设置消息循环,因此不会响应任何用户操作,也不会在第一次之后重新绘制自身。要解决此问题,您需要使用
Application::Run

System::Void showGUI() {
  Application::Run(this);
}

你忽视了以前得到的建议。当你不使用答案时,问问题没有多大意义。@HansPassant:不,我没有。我用的是第二个答案!我检查了语音识别问题,我已经在为引擎使用异步方法。所以,我不得不接受第二个答案:)嗨,非常感谢你的回复。不幸的是,我得到了一个错误,它在我的问题的“更新”部分非常感谢你的回答。我来看看这个
1>c:\users\yohan\documents\visual studio 2010\projects\ eesystem\eesystem\NotifyAlarm.h(182): error C2665: 'System::Windows::Forms::Application::Run' : none of the 3 overloads could convert all the argument types
1>          c:\program files\reference assemblies\microsoft\framework\.netframework\v4.0\system.windows.forms.dll: could be 'void System::Windows::Forms::Application::Run(System::Windows::Forms::Form ^)'
1>          c:\program files\reference assemblies\microsoft\framework\.netframework\v4.0\system.windows.forms.dll: or       'void System::Windows::Forms::Application::Run(System::Windows::Forms::ApplicationContext ^)'
1>          while trying to match the argument list '(NotifyAlarm)'
System::Void showGUI() {
  Application::Run(this);
}