I'm trying to write a program that keeps executing a method without having to use a timer. For example, I want to have a program that shows a list of currently running processes. Obviously, it's constantly changing. I don't want to use a timer that gets the list of processes every second and adds them to the list, because it doesn't look very elegant to me. Is there any special way to do that? How can I make a service that would make this happen?
-
10This is exactly what Timer is for. – anthony Apr 13 '12 at 01:59
-
1I guess the only option to a timer would be a infinite while (not to mention you would have to do all the work of how it would act yourself, such as intervals, etc.), but I would still stand by what anthony said. – Prix Apr 13 '12 at 02:03
-
possible duplicate of [C# Process Monitor](http://stackoverflow.com/questions/1986249/c-sharp-process-monitor) – Hans Passant Apr 13 '12 at 02:07
-
1There isn't a `ProcessMonitor` class that receives events when Windows creates a Process (that I know of), so your only recourse is to poll which typically involves a timer of some sort. – M.Babcock Apr 13 '12 at 02:09
-
6I want to pound in a nail, but I just can't stand hammers! Anyone have a better solution? – Ed S. Apr 13 '12 at 02:10
-
@EdS. - Use a rock, or your head. – M.Babcock Apr 13 '12 at 02:10
-
@M.Babcock: Thanks! That seems sufficiently more difficult than using a hammer to me! – Ed S. Apr 13 '12 at 02:11
-
Alright, I'll go with a timer for now I guess... but one day I'll find a solution, and that day my friends, you will all remember me (lol just joking around :p) Thank guys. – Arian Motamedi Apr 13 '12 at 02:26
-
The part that throws me off for this question is your "For example, I want to have a program that shows a list of currently running processes." - do you mean to say that is exactly what you are trying to do, or that's one thing people use timers for that you think is inelegant? Are you asking a general question about repetitively calling a function or a specific question about a non-polling approach to determining running processes? – deepee1 Apr 13 '12 at 02:34
-
No that's exactly what I meant by the question, getting a list of processes is what I'm looking for. – Arian Motamedi Apr 13 '12 at 02:40
5 Answers
Personally, I would stick with a timer for this. It really is pretty elegant. But, if you really want something with a bit more pizazz then read on!
As the new async
and await
keywords (to be included in C# 5.0) catch on we may see timers getting more uncool1. Here is how you could do this without a timer right now using the Async CTP.
private async void ShowProcessesButton_Click(object sender, EventArgs args)
{
while (true)
{
await Task.Delay(1000); // TaskEx.Delay in the CTP
YourListBox.Items.Clear();
foreach (Process p in Process.GetProcesses())
{
YourListBox.Items.Add(p.ProcessName);
}
}
}
Pretty weird huh? It is probably not the ideal example of how async
and await
are suppose to be used, but you get the point. It is the wave of the future to come.
1Do not get me wrong. There will always be a need for the traditional timer classes.

- 18,753
- 6
- 54
- 84

- 47,849
- 13
- 107
- 150
You can spawn a lower priority thread with an infinite (until the process exits) loop to update your process list periodically. Without more information about what kind of control you're using or whatever I can't really help you out with Control update delegates, but the rough skeleton of your function would be:
private void processUpdater()
{
while (true) //not the most elegant way to implement a pseudo-infinite loop, but you can figure out how you want to signal it to end.
{
var runningProcesses = Process.GetProcesses().Where(p => !string.IsNullOrEmpty(p.MainWindowTitle)).Select(p => p.MainWindowTitle); //filter the running processes to get those with open windows.
this.lstProcesses.Invoke((MethodInvoker)delegate { this.lstProcesses.Items.Clear(); }); //thread-safe invocation to clear the current items
this.lstProcesses.Invoke((MethodInvoker)delegate { this.lstProcesses.Items.AddRange(runningProcesses.ToArray<string>()); }); //add the new set of windowed processes
System.Threading.Thread.Sleep(5000); //update the list every 5 seconds
}
}
In my case I just capture the processes with windows, and list those windows titles in a listbox (lstProcesses)
Now, to spawn this thread:
var tUpdater = new System.Threading.Thread(processUpdater);
tUpdater.Priority = System.Threading.ThreadPriority.BelowNormal;
tUpdater.Name = "Process List Updater";
tUpdater.Start(); //start the thread running.
Obviously this is a very basic implementation, but hopefully it'll give you a rough idea on how to do what you want.

- 5,289
- 25
- 28
-
Putting it on a thread and set its priority to low or below normal is actually a good idea in my case. That helped a lot, thanks! – Arian Motamedi Apr 13 '12 at 03:24
If you're dead set on not using a Timer, you will need to do something like:
- Get the list or running processes when your app starts up
- Detect when a process starts and add it to the list
- Detect when a process ends and remove it from the list
To detect process creation/termination, take a look at this answer:
-
I don't think the suggested solution in the linked answer will work with processes that are _headless_. – M.Babcock Apr 13 '12 at 02:10
Program1:
You can develop a Windows service which runs constantly. No timer is required. If you can give us some more details, we can come up with a better solution. But remember, no user interaction is possible for a window service.
The running processes are added to an observablecollection. When ever an item is added it raises CollectionChanged event.
Program2:
You can have a program where you want the list of running processes exposed as a Webservice, the webservice is consumed in Program1. In the CollectionChanged eventhandler of program1, you can invoke program2 webservice and notify about the newly added process.

- 7,156
- 12
- 45
- 57
-
You've provided a possible solution _and_ a reason not to use it in the same answer... without actually addressing the question. Did you read the question? – M.Babcock Apr 13 '12 at 02:23
-
windows service is what I came up with at first, but not sure how to implement it. I just need to get a list of running processes and add them to the list. It should be "live", meaning as soon as a process started, it's "automatically" added to the list. – Arian Motamedi Apr 13 '12 at 02:28
-
@programmer93 I have proposed a bit contrived solution. Hope this helps. – Sandeep Apr 13 '12 at 02:45
-
WebService? Sounds kind of confusing... can you give me an example? – Arian Motamedi Apr 13 '12 at 02:59
I don't agree with the comments suggesting that Timers are the only or best answer here. The desire to be alerted when a change occurs, rather than polling for it, is a noble pursuit. You're looking for a WinAPI "Hook", I believe, but I don't know which one or if it exists.
See http://msdn.microsoft.com/en-us/library/windows/desktop/ms644959%28v=vs.85%29.aspx

- 1
- 1

- 20,024
- 18
- 75
- 125