I suppose the best way to achieve this is by adding it to the thread pool, it's easy and quick.
Example:
public static void Main(string[] args)
{
check_news();
}
private static void check_news()
{
ThreadPool.QueueUserWorkItem((obj) =>
{
// Fetch the news here
Thread.Sleep(1000); // Dummy
});
}
Or if you really want to deal with it yourself, this is something you could use:
public static void Main(string[] args)
{
check_news();
Console.ReadKey();
}
private static void check_news()
{
Thread t = new Thread(() =>
{
// Check news here
Thread.Sleep(1000); // Dummy
});
t.Priority = ThreadPriority.Lowest; // Priority of the thread
t.IsBackground = true; // Set it as background (allows you to stop the application while news is fetching
t.Name = "News checker"; // Make it recognizable
t.Start(); // And start it
}
But you should know that this takes a longer time to start, it doesn't reuse threads, and there's not a real advantage in it.
Or if you want more control you could use the async platform:
public static void Main(string[] args)
{
check_news(); // You can add an argument 'false' to stop it from executing async
Console.WriteLine("Done");
Console.ReadKey();
}
public delegate void Func();
public static void check_news(bool async = true)
{
Func checkNewsFunction = () =>
{
//Check news here
Thread.Sleep(1000);
};
if (async)
{
AsyncCallback callbackFunction = ar =>
{
// Executed when the news is received
};
checkNewsFunction.BeginInvoke(callbackFunction, null);
}
else
checkNewsFunction();
}
Note that the lambda expressions in all examples can just as well be replaced by regular functions. But I just use them right now, because it seems nicer as example.