Reputation: 1749
I'm implementing a screensaver to restart a background application. My need is to restart firefox to reset the home page in a windows kiosk. I would like to do this with a Screen Saver. The language I use is C#.
The code is not so complex and is a sort of copy and paste from
http://msdn.microsoft.com/en-us/library/windows/desktop/ms686421(v=vs.85).aspx
and
http://www.harding.edu/fmccown/screensaver/screensaver.html
It should work in this manner: the screensaver starts, then after 10 sec. kills the application (if it exists) and then, after 10 sec. again it restarts the application. After all the screensaver quit (there is a timer that drives this).
The problem is that the process started by the screensaver has a sort of link to the killed screensaver, because until the user doesn't kill the application, the screensaver doesn't restart!
I use this function to restart the application:
public static void StartAProcess(string executableName)
{
//Process.Start(new ProcessStartInfo(executableName));
RunThread ext = new RunThread();
Thread t = new Thread(new ParameterizedThreadStart(ext.OpenProcess));
t.Start(executableName.ToString());
}
public class RunThread
{
public void OpenProcess(object executableName)
{
ProcessStartInfo si = new ProcessStartInfo();
si.UseShellExecute = true;
si.FileName = (string) executableName;
Process proc = Process.Start(si);
---> if (null != proc)
proc.WaitForExit(); // Block until exit**
}
}
The behaviour is different depending on the OS and the WaitForExit instruction:
Upvotes: 0
Views: 548
Reputation: 86708
Windows protects itself from the behavior you're trying to code by running the screen saver in a job object that prevents any process started from "escaping" from the job. That way the screensaver can't leave any processes running when the user doesn't expect. Windows will either terminate the job (and thus all of the processes started within it) when the screensaver process terminates (WinXP) or it will wait until the the job object itself terminates (i.e. every process in the job terminates).
It's not clear why you're using a screensaver for this. I probably would either use a program that runs on startup or a service to do something like this.
Regardless, if you have to use a screensaver, you have to find a way to create a process from outside your job object. One thing to consider is the Create method of the Win32_Process Class.
Based on your indication that you want to restart Firefox, it sounds like you have an Internet kiosk type of application where you want to restart the browser when the user has left the kiosk. In that case I would recommend something else entirely:
Create a program that starts Firefox, waits for it to exit, then restarts it, all running in an infinite loop. Run this program using some sort of AutoRuns-like mechanism.
Make your screensaver simply kill Firefox and exit. When the screensaver activates, it will kill Firefox and exit, causing the looping program to restart it.
Upvotes: 2