Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, I am trying to create an auto shutdown program that will shutdown the computer whenever the user is done downloading or installing things. Right now I'm running into trouble, I tried making the program read for processes that close but the problem is that most downloads and software installations don't automatically exit when they finished so that approach doesn't work. My next move is to try to get my program to read the selected process of the program for idleness, how can I do this? If there is another way that is accurate in which my program can tell when a specific process is done with installation please show me how?

Windows 7

Thanks
Posted
Updated 7-Jul-12 13:32pm
v2
Comments
[no name] 7-Jul-12 21:38pm    
I have read this over and over and quite frankly very little of this makes sense.
"user is done downloading"... why would you need to shutdown or restart the computer when the user downloads a file?
What kind of download? Someone opens a web page in a browser and it downloads a graphic does the computer shutdown for that?
"or installing things"... it's more usual that the installation program restarts the computer if it's necessary.
"software installations don't automatically exit"... where do you get this idea? In my experience over the past 30 years approximately 99.9999999999999% of installation programs do exit when they are finished. So why would you think that they do not?
Unless you perform an advanced technique called code injection into the process that you want to monitor then there is no way to detect "idleness". I do not know much about code injection as I have never found a need for it. It is documented though. The problem with this approach, as I understand it, is that the code injection might just depend on the process that you are going to inject code into. So, if you want to monitor an InstallShield installation program, the code injection might be vastly different than a Wise installer or NullSoft, or any of the other installation programs. I do not know this for sure so I would not bet on it.

"another way"... sure find the process that you want to monitor and wait for it to drop off the running processes list. Or start the process yourself and wait for it to exit. Either way is easy to do.
Sergey Alexandrovich Kryukov 8-Jul-12 16:05pm    
I think you are right.
And I think I basically answered, please see.
--SA
MR. AngelMendez 9-Jul-12 2:07am    
first off, the reason why someone would want to shutdown their computer when they are done downloading or installing something is I don't know.. maybe they have to go somewhere or their pulling a huge all nighter download and they want their PC to shutdown while their asleep.

Secondly, any type of download or installation. I got my program to list all of the processes that are running in a checklistbox. The design was that the user checks on the programs that they wish to monitor. If those processes closed or is "idle" then the program was supposed to shutdown the computer.

Thirdly, not all programs close when they are done installing, some stay open and say installation is complete and the user has to close it manually like video games, old software and non commercial applications.

Since there is no other way to do this then how do I check to see if the processes that the user selects from the checklistbox exited?

here is my code

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

int timer = 100;

bool restart;



Process[] pro = Process.GetProcesses();

private void Form1_Load(object sender, EventArgs e)
{



foreach (Process prolist in pro)
{
checkedListBox1.Items.Add(prolist.ProcessName);

}

}

private void button1_Click(object sender, EventArgs e)
{

MessageBox.Show("timer started");
timer1.Interval = timer;
timer1.Enabled = true;

timer1.Start();



}

private void timer1_Tick(object sender, EventArgs e)
{
if (timer1.Enabled == true)
{



timer = timer = -1;
}
if (timer == -1)
{

checkedListBox1.Refresh();

foreach (Process prolist in pro)
checkedListBox1.Items.Add(prolist.ProcessName);
{
if (listBox1.Items.Equals(listBox1.SelectedItems.Equals(0)))
{
MessageBox.Show("not found");

}


else
{
timer = 100;
}


}
}
}







private void button2_Click(object sender, EventArgs e)
{
timer1.Stop();
timer = 10;
MessageBox.Show("timer stopped");
}

private void button3_Click(object sender, EventArgs e)
{
checkedListBox1.Items.Clear();

Process[] pro = Process.GetProcesses();
foreach (Process prolist in pro)
{
checkedListBox1.Items.Add(prolist.ProcessName);
}
}

private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{


listBox1.Items.Add(checkedListBox1.SelectedItem);




}
}
}




1 solution

No. The concept if "idle" is not applicable for all processes, only to a part of them, when a process awaits of a user input from a console. "Normal" UI threads do not use it, but the thread is kept in a wait state (and uses zero CPU) until the user input is dispatches to the application main window, which wakes up the thread. This is not the same as idleness. Besides, you cannot know what the process is about to do next. So, in a general case, there is no a way to tell if a process is "safe to terminate". That's why, if you try to shutdown the system in a regular way, it can request you for the confirmation for termination of some processes. This thing is pretty trivial and is based on the fact that normal UI processes can process notifications from the OS which is about to shut down and terminate themselves in a graceful way. Some applications can veto or block shutdown or simply "hang". It nothing happens "soon enough", some application can be considered "handing".

There is an interesting short article explaining Microsoft patented way shutting down the OS:
http://www.conceivablytech.com/2530/products/microsoft-patents-operating-system-shutdown[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900