|
-
Jun 9th, 2005, 05:26 PM
#1
Thread Starter
Hyperactive Member
fork vs. exec
I need to exec a process. I have been using:
Code:
Form fMain = new frmMain();
fMain.Show();
but of course that just forks another form. I want the child to stay alive after the parent has been killed.
Is there a way to do this without shelling itself? I mean, I suppose I could:
Code:
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName="path to self";
proc.Start();
How to you reference the app path? Like VB's app.path & "\" & app.exename?
Are there actual fork and exec type commands in c#?
-
Jun 10th, 2005, 12:39 AM
#2
Re: fork vs. exec
With no VB6 experience, I'm guessing that Application.StartupPath and Application.ExecutablePath are what you are after.
In this situation your child window will only close if its parent is the app's startup object. If you use a Main method to start your app you can arrange it so that closing your first form will not exit the app if other forms are still open.
You'll be pleased to know that VS 2005 has a project property that defines whether the app exits when the first window is closed or when all windows are closed.
Edit:
The middle paragraph is not 100% accurate. Even with a Main method as the startup object you do still need to do something other than just call Application.Run(new Form1()). I don't know specifically as I've never done it but I've seen examples of others who've done it.
Last edited by jmcilhinney; Jun 10th, 2005 at 12:42 AM.
-
Jun 15th, 2005, 05:56 PM
#3
Frenzied Member
Re: fork vs. exec
i could be wrong here but i'm pretty sure there is no way to keep a child alive if you kill the parent. Unless you change the child's parent, which i've never done. but that's worth a try.
-
Jun 15th, 2005, 06:13 PM
#4
Re: fork vs. exec
Let's say that your app consists of two forms, with form1 being the startup object. At the moment, form1 opens form2, so if form1 is closed then form2 closes and the whole app exits. If you make a Main method the startup object and open both forms independently from that method, there is no parent-child relationship between the forms and the app doesn't exit until the Main method completes.
-
Jun 16th, 2005, 12:52 PM
#5
Frenzied Member
Re: fork vs. exec
what is a 'fork' anyway? is that a c++ thing?
-
Jun 16th, 2005, 02:36 PM
#6
Thread Starter
Hyperactive Member
Re: fork vs. exec
(My text diagram didn't come out so good. Just imaging that the pipes are below the "fork" calls)
Fork is a multithreading thing. It is not specific to C++. Any language that supports threading will have fork and exec (c, java, c++, c#,etc..)
Fork:
original process---->fork---->original process
|
|------>second process
Exec:
original process---->exec---->original process
|
|------>Shelled process (like notepad or something
In exec, you can call it so that the original process will wait for a return from the shelled process, or not wait.
Each process can spawn as many processes as it wants.
-
Jun 17th, 2005, 11:43 PM
#7
Frenzied Member
Re: fork vs. exec
very interesting indeed. I'm gonna do some more reasearch on that. I've never heard that term. I only started using multi-threading about a year ago though
-
Jun 18th, 2005, 11:25 AM
#8
Thread Starter
Hyperactive Member
Re: fork vs. exec
How could you do multithreading and not know about forking? Well, I guess on Windows it is called CreateProcess()? Doesn't it have like 9 parameters or something? On linux it is called fork() and has very few parameters. Also when you fork in windows, the child starts from the beginning of the routine that called CreateProcess, where as in linux the child starts at the fork statement with all the variables initialized to the same contents as the parent.
-
Jun 18th, 2005, 01:19 PM
#9
Re: fork vs. exec
In VB.NET, you create a new thread and then call Thread.Start. This is what you are calling forking. If you want to create a new, independent process you call Process.Start. If you do a help search on "fork" you'll get a grand total of 3 results. The term is not commonly used in .NET. Create a new thread or create a new process. In either case, you can wait for the new entity to complete, using Thread.Join or Process.WaitForExit, or you can carry on execution.
-
Jun 18th, 2005, 04:27 PM
#10
Frenzied Member
Re: fork vs. exec
that's one problem my last supervisor had. He had NO CLUE about .net and was using older, unused terms to try and explain how he wanted things done. He never learned the .net framwork and insisted that vb6 code, for example, would simply port over to .net line by line. the framwork has done a lot to hide certain low-level processes from the developer and if that developer is new, he'll never have a clue about the low-level stuff that had to be done by hand.
-
Jun 18th, 2005, 06:50 PM
#11
Re: fork vs. exec
 Originally Posted by jmcilhinney
In VB.NET, you create a new thread and then call Thread.Start. This is what you are calling forking. If you want to create a new, independent process you call Process.Start. If you do a help search on "fork" you'll get a grand total of 3 results. The term is not commonly used in .NET. Create a new thread or create a new process. In either case, you can wait for the new entity to complete, using Thread.Join or Process.WaitForExit, or you can carry on execution.
I do the majority of my posting in the VB.NET forums and I just realised that this is the C# forum. It would be reasonable to believe that, since C# is based on C, fork might be a more common term than it is in VB. However, while a help search filtered on VB yields 3 results for fork, a search filtered on C# yields none.
-
Jun 19th, 2005, 02:15 PM
#12
Frenzied Member
Re: fork vs. exec
I know is straying from the subject but...I did a search and i got NOTHING from my help files...I havent searched the online MSDN but rather my installed help files.
-
Jun 19th, 2005, 08:35 PM
#13
Re: fork vs. exec
Like I said, Visual Basic returns 3 matches and C# returns none. C++ returns 19 matches, including one entitled "Port from UNIX to Win32" that may be of interest. It mentions the CreateProcess and CreateThread APIs, although using .NET there are other methods available so APIs are unnecessary.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|