4

I have a project with two forms, and I need to start form2 in a new process, how can I do this? I know there is

Form2 f2 = new Form2();
f2.Show();
this.Hide();

But in this case that is not good for me. I need to start in a new process (as an another .exe file).

So anyway how can I do this?

[UPDATE]

I forget to tell you that I need to pass some information to the form2, like

Form2 f2 = new Form2(someInformation);
f2.Show();
this.Hide();
davidgereb
  • 119
  • 4
  • 13
  • 2
    Why do you want to do that? – Joe Apr 13 '13 at 17:59
  • 3
    If you really need this, you could proceed with @Tomtom answer, or you could add functionality in your `main` to call the `.exe` with an argument representing the form you need to open. But it seems really difficult (and unnecessary) if you also want to pass data. – chaliasos Apr 13 '13 at 18:05
  • Not a bad idea, I'm thinking on that. – davidgereb Apr 13 '13 at 18:09
  • If you *really* need to go with different processes, try this... http://stackoverflow.com/questions/56121/ipc-mechanisms-in-c-sharp-usage-and-best-practices – Machinarius Apr 13 '13 at 18:21

3 Answers3

4

You can create your Form2 in a separate project and call the builded exe-file by

System.Diagnostics.Process.Start("Form2.exe");
Tomtom
  • 8,386
  • 7
  • 45
  • 83
1

In case you mean a new thread, do this:

 var secondFormThread = new Thread(() => Application.Run(new Form(someInformation)));

 this.Hide();                       // Hide the current form

 secondFormThread .Start();         // now show the other one in a new thread
 secondFormThread .WaitForExit();   // wait for this thread to finish or
                                    // maybenot, may add a timeout. Whatever 
                                    // suits your needs.

 this.Show();                       // Show the first form again
CSharpie
  • 8,354
  • 2
  • 42
  • 66
  • 1
    He said new *process*, not a new *thread*. There's a *huge* difference between the two. – Servy Apr 13 '13 at 18:21
  • 1
    Yeah but sometimes people dont know the difference between thread and process. So that difference isnt so _huge_... – CSharpie Apr 13 '13 at 18:23
  • 1
    Yes, the difference is huge. The fact that people confuse the two doesn't mean it's not a big difference. If you suspect that the OP meant that he wanted another process then you could consider commenting, asking if he was sure he meant process vs. thread. As it is, the question is *very* clear that it wants a new process, not a new thread, so this answer is very clearly incorrect. If the OP realizes he made a mistake an edits the question to indicate that he wants a new thread, not a new process, then that would make an answer such as this appropriate. – Servy Apr 13 '13 at 18:27
  • Yeah BUT I really mean _process_ and not _thread_, I know the differences. :) But thanks for your time. – davidgereb Apr 13 '13 at 18:28
  • @Servy Thats why i wrote : _In case_ you mean a new thread, do this: – CSharpie Apr 13 '13 at 18:28
  • Thanks @Servy. You know what I mean. – davidgereb Apr 13 '13 at 18:29
0

Create instance of the Form and display it in separate thread.

new Thread(() => {
    Form2 f2 = new Form2(someInformation);
    f2.ShowDialog();
}).Start();
  • How is this supposed to work? I'm launching it from a form which is actually being ShowDialog'd and it shows and goes... – Gonzo345 Sep 25 '18 at 07:50