|
Something else I do to deal with the possible race condition that you're talking about is I pass the Process ID of the current (old) exe to the new exe via arguments.
Here is a slightly modified version of what I posted earlier:
void UpdateAndRestart()
{
string myNewExeLoc = @"C:\My Folder\myNewExe.new";
string myCurrentExe = System.Reflection.Assembly.GetExecutingAssembly().Location;
string myBakExe = myCurrentExe + ".bak";
if (System.IO.File.Exists(myBakExe))
System.IO.File.Delete(myBakExe);
System.IO.File.Move(myCurrentExe, myBakExe);
System.IO.File.Move(myNewExeLoc, myCurrentExe);
System.Diagnostics.Process p = System.Diagnostics.Process.GetCurrentProcess();
System.Diagnostics.Process newP = new System.Diagnostics.Process();
newP.StartInfo.FileName = myCurrentExe;
newP.StartInfo.Arguments = string.Format("/restart {0}", p.Id);
newP.Start();
System.Environment.Exit(0);
}
void Main(string[] args)
{
if (args.Length > 0)
{
switch (args[0].ToLower())
{
case "/restart":
int oldPid;
if (args.GetUpperBound(0) < 1 || !int.TryParse(args[1], out oldPid))
break;
Process p = System.Diagnostics.Process.GetProcessById(oldPid);
if (p != null)
p.Kill();
break;
}
}
}
|
|
|
|
|
Matt,
Thank you for your excellent and detailed response. Although my simpler solution seems to be working, I do not trust it. Frankly, it's very confusing to have something work and not really understand it, especially when everything I have ever done says that you cannot run these two .exe's simultaneously (I keep waiting for it to crash...it's very disconcerting). I am going to see if I can get your solution to work with my application...at least I can understand the logic behind it. I will keep you informed. I am also marking your answer as the solution, since I am sure that it is at least one correct way of resolving this issue. Thanks again...Best Regards, Pat
|
|
|
|
|
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
|
|
|
|
|
hi all
i am a newbie to C# and i am trying to devlop a application for SENDING SMS through PC and i am trying to decode a application (myphoneexplorer) using portmonitor.
now i am in a portion of understanding the handling the setting of COMPORT.
this is the portmon result
IRP_MJ_CREATE Options: Open
IOCTL_SERIAL_SET_BAUD_RATE Rate: 921600
IOCTL_SERIAL_SET_LINE_CONTROL StopBits: 1 Parity: NONE WordLength: 8
IOCTL_SERIAL_SET_CHAR EOF ERR BRK EVT XON XOFF
IOCTL_SERIAL_SET_HANDFLOW Shake:1 Replace:40 XonLimit XoffLimit
IOCTL_SERIAL_SET_QUEUE_SIZE InSize: 16384 OutSize: 16384
IOCTL_SERIAL_SET_QUEUE_SIZE InSize: 16384 OutSize: 16384
IOCTL_SERIAL_SET_WAIT_MASK Mask: RXCHAR
IOCTL_SERIAL_SET_TIMEOUTS RI:20 RM:10 RC:2000 WM:10 WC:200
Can somebody guideon this explain the pasted content and how to set it through C#
Regards
Venkat.S
|
|
|
|
|
dear all
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
how to solve this issu. thanks a lot.
|
|
|
|
|
So you declared toolBtnRun more than once. Use "Find in Files (entire project)" to locate the redundant one(s).
|
|
|
|
|
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
|
|
|
|
|
the object reference not set to an instances of an object refers to the fact that you have no
instance of MainDialog initialized.
Ex:
SomeClass sc;
sc.SomeProperty = "Blah";
would yield the same error message => object reference not set to an instances of an object
That is because the sc isn't initialized. In fact it's null. It references/points to nothing.
Needed sc = new SomeClass(); before using the reference.
I bug
|
|
|
|
|
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
|
|
|
|
|
Make it (the other form top most through code):
Form2 f = new Form2();
f.TopMost = true;
f.ShowDialog();
I bug
|
|
|
|
|
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 eriting source code, how we can have Persistence block in C# text editor?
|
|
|
|
|
Do you mean reusing the same code again and again?
You could write your own code snippet[^].
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.
My latest tip/trick
Visit the Hindi forum here.
|
|
|
|
|
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.
|
|
|
|
|
Write a Data Access Layer.
|
|
|
|
|
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.
any idea why this occurs.
thanx n advance..
|
|
|
|
|
because you took a wrong turn somewhere.
Member 4257632 wrote: using reflection
why?
|
|
|
|
|
This is my code
Note newNote = new Note();
Search newSearch = new Search();
SRLS newSRLS = new SRLS();
Alarm newAlarm = new Alarm();
QuickLaunch newQuickLaunch = new QuickLaunch();
Calender newCalender = new Calender();
Time newformtime = new Time();
private void Form1_Load(object sender, EventArgs e)
{
notifyIcon1.Visible = true;
if (timeToolStripMenuItem.Checked == true)
{
newformtime.Show();
}
if (alarmToolStripMenuItem.Checked == true)
{
newAlarm.Show();
}
if (calenderToolStripMenuItem.Checked == true)
{
newCalender.Show();
}
if (sRLSToolStripMenuItem.Checked == true)
{
newSRLS.Show();
}
if (searchToolStripMenuItem.Checked == true)
{
newSearch.Show();
}
if (noteToolStripMenuItem.Checked == true)
{
newNote.Show();
}
if (quickLaunchToolStripMenuItem.Checked == true)
{
newQuickLaunch.Show();
}
timeToolStripMenuItem.Checked = Settings.Default.timecheck;
alarmToolStripMenuItem.Checked = Settings.Default.calendercheck;
calenderToolStripMenuItem.Checked = Settings.Default.alarmcheck;
sRLSToolStripMenuItem.Checked = Settings.Default.srlscheck;
searchToolStripMenuItem.Checked = Settings.Default.searchcheck;
noteToolStripMenuItem.Checked = Settings.Default.notecheck;
quickLaunchToolStripMenuItem.Checked = Settings.Default.quickLaunchcheck;
}
So id some one checks the box then closes the program and then reopens it the other form should be visible can anyone see the problem in my code? coz I can't
|
|
|
|
|
Are you saving the checked states of the checkboxes anywhere?
|
|
|
|
|
Yeh when They press the button they save the box if its checked or not and that works, but it just doesn't open when the form1 loads
|
|
|
|
|
Where is the state saved? In the configuration file?
|
|
|
|
|
private void timeToolStripMenuItem_Click(object sender, EventArgs e)
{
((ToolStripMenuItem)sender).Checked ^= false;
Settings.Default["timecheck"] = timeToolStripMenuItem.Checked;
Settings.Default.Save();
}
private void calenderToolStripMenuItem_Click(object sender, EventArgs e)
{
((ToolStripMenuItem)sender).Checked ^= false;
Settings.Default["calendercheck"] = calenderToolStripMenuItem.Checked;
Settings.Default.Save();
}
private void alarmToolStripMenuItem_Click(object sender, EventArgs e)
{
((ToolStripMenuItem)sender).Checked ^= false;
Settings.Default["alarmcheck"] = alarmToolStripMenuItem.Checked;
Settings.Default.Save();
}
private void sRLSToolStripMenuItem_Click(object sender, EventArgs e)
{
((ToolStripMenuItem)sender).Checked ^= false;
Settings.Default["srlscheck"] = sRLSToolStripMenuItem.Checked;
Settings.Default.Save();
}
private void searchToolStripMenuItem_Click(object sender, EventArgs e)
{
((ToolStripMenuItem)sender).Checked ^= false;
Settings.Default["searchcheck"] = searchToolStripMenuItem.Checked;
Settings.Default.Save();
}
private void noteToolStripMenuItem_Click(object sender, EventArgs e)
{
((ToolStripMenuItem)sender).Checked ^= false;
Settings.Default["notecheck"] = noteToolStripMenuItem.Checked;
Settings.Default.Save();
}
private void quickLaunchToolStripMenuItem_Click(object sender, EventArgs e)
{
((ToolStripMenuItem)sender).Checked ^= false;
Settings.Default["quickLaunchcheck"] = quickLaunchToolStripMenuItem.Checked;
Settings.Default.Save();
}
|
|
|
|
|
So, on the form load, you should check the value of the setting and then check the appropriate check box and show the corresponding form.
You are checking the checked state of the checkboxes which will be unchecked by default on the form load.
|
|
|
|
|
Yay I fixed most of it!, but for some reason the code dose not work for the calender part
|
|
|
|
|
systemerror121 wrote: ((ToolStripMenuItem)sender).Checked ^= false;
And what is that supposed to do?
It's worse than that awful == true ..
|
|
|
|