user765168
user765168

Reputation: 77

How can I start two window form in a program?

I have two forms. I want to start both of them at the same time. In the main program, I follow Md Kamruzzaman Pallob's suggestion. The following code is the update version, but it is still not working.

Error is error C3350: 'System::Threading::ThreadStart' : a delegate constructor expects 1 argument(s)

   #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;

}

Upvotes: 1

Views: 1752

Answers (3)

James Lockhart
James Lockhart

Reputation: 1040

Try :

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

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

Upvotes: 0

SuperPrograman
SuperPrograman

Reputation: 1822

Why don't you just make a form1 load event like the following? :

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

Then every time Form1 opens, so does Form2. It seems to work for me.

Upvotes: 1

Md Kamruzzaman Sarker
Md Kamruzzaman Sarker

Reputation: 2407

You can do it by using threading. I am sorry because i do not know c++ well. But i can give you the solution in 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();
    }

Upvotes: 0

Related Questions