【问题标题】:How can I start two window form in a program?如何在一个程序中启动两个窗口窗体?
【发布时间】:2012-12-10 08:47:49
【问题描述】:

我有两种形式。我想同时启动它们。在主程序中,我遵循 Md Kamruzzaman Pallob 的建议。以下代码是更新版本,但还是不行。

错误是错误 C3350:'System::Threading::ThreadStart':委托构造函数需要 1 个参数

   #include "stdafx.h"
  #include "Form1.h"
  #include "Form3.h"
   using namespace MySearch;
   using namespace System;
   using namespace System::Threading;



 public ref class ThreadX{
 public: ThreadX(){}
 public: static void func1()
{
    Application::Run(gcnew Form1());
}

public: static void func2()
{
    Application::Run(gcnew Form3());
}


};


[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false); 

// Create the main window and run it


    ThreadX^ o1 = gcnew ThreadX();
    ThreadX^ o2 = gcnew ThreadX();

    Thread^ th = gcnew Thread(gcnew ThreadStart(o1, &ThreadX::func1));
    Thread^ th1 = gcnew Thread(gcnew ThreadStart(o2, &ThreadX::func2));
    th->Start();
    th1->Start();



return 0;

}

【问题讨论】:

    标签: visual-studio-2010 c++-cli


    【解决方案1】:

    你为什么不像下面那样做一个 form1 加载事件? :

    private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
             Form2^ form2 = gcnew Form2;
             form2->Show();
         }
    

    然后每次打开 Form1,Form2 也会打开。它似乎对我有用。

    【讨论】:

      【解决方案2】:

      你可以通过使用线程来做到这一点。我很抱歉,因为我不太了解 C++。但我可以用c#给你解决方案

       public static void func1()
          {
              Application.Run(new Form1());
          }
      
         public static void func2()
          {
              Application.Run(new Form2());
          }
      
          static void Main()
          {
              Application.EnableVisualStyles();
              Application.SetCompatibleTextRenderingDefault(false);
              Thread th = new Thread(func1);
              Thread th1 = new Thread(func2);
              th.Start();
              th1.Start();
          }
      

      【讨论】:

        【解决方案3】:

        试试:

        Thread^ th = gcnew Thread(gcnew ThreadStart( &ThreadX::func1 ) );
        Thread^ th1 = gcnew Thread(gcnew ThreadStart( &ThreadX::func2 ) );
        

        见http://msdn.microsoft.com/en-us/library/system.threading.threadstart.aspx

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-07-13
          • 2023-02-08
          • 2023-03-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多