No problem. I've gotten a lot of help from this forum so it's nice to be able to give back a bit.
To address your questions, you are able to run multiple instances of an exe at the same time (calculator.exe) because Windows reads the exe from disk to RAM, then executes the code from memory. So when you run a program again while another instance is still running, windows is actually giving each program it's own memory space with it's own global variables, etc.
Now, as far as I can tell, Windows doesn't let you delete an EXE while an instance of the code is loaded in memory. That is why in the solution we rename it (Move) then delete it the second time around once we know that the instance which was using the "old" code is now terminated. That is why I suggest passing the PID of the previous instance to the new one. We must make sure the old process is terminated before we try to delete the old EXE.
Now for most simple programs, having multiple instances loaded in memory isn't a big deal, and is sometimes desirable (notepad.exe). Where it becomes an issue is when both of them try to use the same resource and they aren't aware of each other. It very similar to the issues you could run in to when multithreading.
Luckily, Windows again gives us a few tools to deal with multiple instances of our app. I believe the System.Threading.Mutex is the easiest to use, and I think the MSDN code samples are pretty straight forward. The bottom line is that it gives you a single, system wide "token" that your program can check for on startup, and if the token is already in use, then it knows that there is another instance already running. I'm sure this is very similar to how a lot of apps implement the logic of Activating an existing instance when you try and run a new one, like Skype or MSN Messenger.
You can also set up a Client/Server type setup where the first instance starts a TCP server and all the other instances check for the server on startup. If it exists, then you can use the Client/Server to have your instances talk to one another. I believe in .NET 3.0 you can also use Named Pipes to talk between instances, but as I'm only able to program to .NET 2.0 for work, I don't have any experience with them.
The Mutex method can be implemented with a few lines of code (wrap it in a using statement!) and it is a proactive way to make sure that only one instance of your app is running at a time.
Wow, that turned in to a short story. Hope it all works out for you and you can get your head wrapped around everything!
i am newbe with C#, i got a problem when i am debug, the issue is as following:
Error 2 The type 'UIS_Application.MainDialog' already contains a definition for 'toolBtnRun' C:\Documents and Settings\LI ZHIYUAN\Desktop\UIS Application\UIS Application\UIS Application\MainDialog.Designer.cs 555 54 UIS Application
thanks sir, acutally i found already by this way, i couldn't find where i definded more than one. and moreever i couldn't display the "MainDialog GUI", when i double clicked the "maindialog" then the following dialog display and content is : object reference not set to an instances of an object. how to solve it. thanks
thanks sir, i solved already according to your help.
here another problem for me, when i set the "maindialog" as topmost, however when i click the mnubutton to display a dialog, it can't be display outside, it displayed the behind the maindialog, if i set the maindialog as normal then it can display, however when i want to run my maindialog, i want it as topmost. how to display the other dialog outside the maindialog when i set maindialog as topmost. thanks a lot
if you mean double-clicking a form in the solution pane leads to an exception, that must be because some of your code (including the default constructor) gets executed by Visual Designer and a fault of yours results in an Exception. You could always right-click the form, choose "View Code" and go look for the problem.
when we create a block inside text editor, by pressing any key, that block disappears,
while existing block, if suddenly u press DEL key, content of that block get detroyed.
i dont want this, i want to have a block remain persist. pay attention i am talking only
about text editor not data manippulate or SQL server or....... just inside of text editor.
i'm trying to crate an AVI file using some screen shots as frames.but when starting the process it gives the the exception saying
"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
the application that i use to create the avi file is loaded to my application using reflection.what i do is just pass the frames to this loaded application.