Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# installer Process WinForm , +
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 7-Jul-12 13:31pm
Edited 7-Jul-12 13:32pm
v2
Comments
Wes Aday at 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 at 8-Jul-12 16:05pm
   
I think you are right.
And I think I basically answered, please see.
--SA
MR. AngelMendez at 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);
 

 

}
}
}
 

 
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  

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

  Print Answers RSS
0 Schatak 394
1 OriginalGriff 340
2 _Amy 115
3 Abhijit Ghosh (Subho) 115
4 Andreas Gieriet 100
0 OriginalGriff 7,107
1 Sergey Alexandrovich Kryukov 5,623
2 Maciej Los 3,504
3 Peter Leow 3,373
4 DamithSL 2,505


Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 7 Jul 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100