Avoid multiple instances of a program -> INCL. PARAMETER PASSING to already running instance
Hey Guys,
I am programming a timer thats shuts down Windows automatically after a certain time. When I start the program, it adds 30 mins to a counter and counts them down. Because it doesn't make sense to have multiple timers at once, I wanted my program to run in only ONE instance - this works fine like this.
static class Program
{
[STAThread]
static void Main(string[] args)
{
Mutex Mu = new Mutex(false, "{XEQRT97T57B1PNGJ1LRG83JEKGLUWX10}");
if (Mu.WaitOne(0, false))
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1(true));
}
else
{
}
}
}
I placed the actualy timer in a Form class, so I can put it nicely into the system tray.. The thing I want to do is the following.
When I first start the program, it adds 30 minutes to the timer and it starts to count down, if I start the program the second time, I don't want it to create a second instance. Instead I want to give my already running instance a sign to add another 30 minutes on the already counting timer. It is supposed to simulate the Sleep function of a normal hifi system. Is there any possibilty to do that?!
Here is my Form1 class for completion.
public partial class Form1 : Form
{
public Int32 timeToShutdown = 0;
public Int16 standardIntervalStep = 30;
public Form1(Boolean addMoreMinutes)
{
if (addMoreMinutes == true)
{
InitializeComponent();
this.startMinimized();
}
this.timeToShutdown = this.getTimeFromCommandLine();
this.setTimer(this.timeToShutdown);
}
public void startMinimized()
{
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
}
public Int32 getTimeFromCommandLine()
{
string[] args = Environment.GetCommandLineArgs();
foreach (String arg in args)
{
try
{
this.timeToShutdown += Convert.ToInt32(arg);
}
catch (Exception)
{
this.timeToShutdown += this.standardIntervalStep;
}
}
Int16 minutesToShutdown = Convert.ToInt16(timeToShutdown);
timeToShutdown = timeToShutdown * 60 * 1000;
DateTime shutdownTime = DateTime.Now.AddMilliseconds(timeToShutdown);
this.systemTrayIcon.ShowBalloonTip(500, "Shutdown", "Windows wird in " + minutesToShutdown + " Minuten heruntergefahren. (" + shutdownTime + ")", ToolTipIcon.Info);
return this.timeToShutdown;
}
public void setTimer(Int32 milliSeconds)
{
Timer shutdownTimer = new Timer();
shutdownTimer.Interval = milliSeconds;
shutdownTimer.Tick += new EventHandler(exitWindows);
shutdownTimer.Start();
}
public void exitWindows(object sender, EventArgs e)
{
this.systemTrayIcon.ShowBalloonTip(500, "Shutdown", "Windows wird heruntergefahren.", ToolTipIcon.Info);
StartShutDown("-f -s -t 5");
}
private static void StartShutDown(string param)
{
ProcessStartInfo proc = new ProcessStartInfo();
proc.FileName = "cmd";
proc.WindowStyle = ProcessWindowStyle.Hidden;
proc.Arguments = "/C shutdown " + param;
Process.Start(proc);
}
private void closeShutdown_Click(object sender, EventArgs e)
{
Application.Exit();
}
Maybe somebody can help me out with this, I would really appreciate that..
greets pat