Click here to Skip to main content
15,884,099 members
Home / Discussions / C#
   

C#

 
AnswerRe: Automatically Install Updated Application Version Pin
Matthew Klein30-Aug-10 14:38
Matthew Klein30-Aug-10 14:38 
GeneralRe: Automatically Install Updated Application Version Pin
Luc Pattyn30-Aug-10 14:54
sitebuilderLuc Pattyn30-Aug-10 14:54 
GeneralRe: Automatically Install Updated Application Version Pin
Chuck O'Toole30-Aug-10 16:16
Chuck O'Toole30-Aug-10 16:16 
GeneralRe: Automatically Install Updated Application Version Pin
PDTUM30-Aug-10 16:51
PDTUM30-Aug-10 16:51 
GeneralRe: Automatically Install Updated Application Version Pin
Matthew Klein31-Aug-10 4:44
Matthew Klein31-Aug-10 4:44 
AnswerRe: Automatically Install Updated Application Version Pin
Matthew Klein31-Aug-10 8:27
Matthew Klein31-Aug-10 8:27 
GeneralRe: Automatically Install Updated Application Version Pin
PDTUM31-Aug-10 9:24
PDTUM31-Aug-10 9:24 
AnswerRe: Automatically Install Updated Application Version Pin
Matthew Klein31-Aug-10 10:24
Matthew Klein31-Aug-10 10:24 
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!

Matt
QuestionCOM PORT SETTING THROUGH C# IDE Pin
venkat swaminathan29-Aug-10 7:40
venkat swaminathan29-Aug-10 7:40 
QuestionUIS Application\MainDialog.Designer.cs Pin
zhiyuan1629-Aug-10 5:19
zhiyuan1629-Aug-10 5:19 
AnswerRe: UIS Application\MainDialog.Designer.cs Pin
Luc Pattyn29-Aug-10 5:24
sitebuilderLuc Pattyn29-Aug-10 5:24 
GeneralRe: UIS Application\MainDialog.Designer.cs Pin
zhiyuan1629-Aug-10 5:31
zhiyuan1629-Aug-10 5:31 
GeneralRe: UIS Application\MainDialog.Designer.cs Pin
Dan Mos29-Aug-10 5:41
Dan Mos29-Aug-10 5:41 
GeneralRe: UIS Application\MainDialog.Designer.cs Pin
zhiyuan1629-Aug-10 5:45
zhiyuan1629-Aug-10 5:45 
GeneralRe: UIS Application\MainDialog.Designer.cs Pin
Dan Mos29-Aug-10 6:02
Dan Mos29-Aug-10 6:02 
GeneralRe: UIS Application\MainDialog.Designer.cs Pin
Luc Pattyn29-Aug-10 5:43
sitebuilderLuc Pattyn29-Aug-10 5:43 
QuestionA simple question. Persistence block in C# text editor? Pin
Fred 3429-Aug-10 3:50
Fred 3429-Aug-10 3:50 
AnswerRe: A simple question. Persistence block in C# text editor? Pin
Abhinav S29-Aug-10 4:10
Abhinav S29-Aug-10 4:10 
GeneralRe: A simple question. Persistence block in C# text editor? Pin
Fred 3429-Aug-10 23:06
Fred 3429-Aug-10 23:06 
AnswerRe: A simple question. Persistence block in C# text editor? Pin
PIEBALDconsult29-Aug-10 5:18
mvePIEBALDconsult29-Aug-10 5:18 
Questiongetting "attempted to read or write protected memory in .net" when trying to create a AVI file. Pin
Member 425763228-Aug-10 20:36
Member 425763228-Aug-10 20:36 
AnswerRe: getting "attempted to read or write protected memory in .net" when trying to create a AVI file. Pin
Luc Pattyn29-Aug-10 1:46
sitebuilderLuc Pattyn29-Aug-10 1:46 
QuestionC# loading data Pin
C.CoderCreator28-Aug-10 16:23
C.CoderCreator28-Aug-10 16:23 
AnswerRe: C# loading data Pin
dan!sh 28-Aug-10 18:54
professional dan!sh 28-Aug-10 18:54 
GeneralRe: C# loading data Pin
C.CoderCreator28-Aug-10 18:55
C.CoderCreator28-Aug-10 18:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.