Reputation: 86
I am developing a program which will run on thin clients with minimum multitasking capabilities and other hardware resources. I wish to have two methods running continuously but not hold the flow of the program. During my search I came across some posts that try to address similar concepts but they are quite old and either in a different programming language or do not solve my problem.
In my program I have a file system watcher, a resources monitor method, and some other methods.
Currently my program runs file system watcher and ResourcesMonitor() method in parallel but the program does not get past ResourcesMonitor() method so the ThirdMethod() and other following methods never execute.
How can I have my program run ResourcesMonitor() and also execute the remaining methods below this method?
class Program
{
static void Main(string[] args)
{
// call the FSW
var task = MyFileSystemWatcher(path);
// call another method that works same way as file system watcher
ResourcesMonitor();
// call another method
ThirdMethod();
// more methods
}
private void ResourcesMonitor()
{
// on a thin client with limited hardware resources
using (an API)
{
// monitors...
// if number of processes is higher than recommended, close some uncessary background processes
// in the cases of resources overflow, write to log
// to have the this method run infinitely
System.Windows.Forms.Application.Run();
}
}
private static void ThirdMethod()
{
// does something
}
private static async Task MyFileSystemWatcher(string path)
{
// file system watcher
}
}
Currently I have two such methods but may have more methods with a similar logic later that I will have to run in parrallel.
Upvotes: 0
Views: 45
Reputation: 309
Create new Thread with the method you need, optionally make it background and assign priority level as you need (there are several).
Upvotes: 0
Reputation: 11891
You can use Task
to accomplish this:
// call another method that works same way as file system watcher
var task1 = Task.Run(() => ResourcesMonitor());
// call another method
var task2 = Task.Run(() => ThirdMethod());
Task.WaitAll(new { task1, task2 } );
Upvotes: 1