Hi All
Just looking for something ultra simple. I need to spawn a method off to a new thread.
- I don't care when or how it ends.
Can somebody please help me with this?
Thank you
Hi All
Just looking for something ultra simple. I need to spawn a method off to a new thread.
Can somebody please help me with this?
Thank you
Check out this MDSN article about the Thread Pool. This should have you ask for new threads and other thread realted stuff.
Thread thread=new Thread(() => {
// thread code here
});
thread.Start();
Just look at the MSDN page for the class System.Threading
, they've got an easy sample there.
http://msdn.microsoft.com/en-us/library/system.threading.thread.aspx
For starting a new thread in winforms, the ThreadPool
is hard to beat for simplicity:
ThreadPool.QueueUserWorkItem(state =>
{
// put whatever should be done here
});
A short program that never stops saying "Hello!", using a thread.
using System;
using System.Threading;
namespace Treading
{
class Program
{
static void Main(string[] args)
{
Thread noiseMaker = new Thread(Noisy);
noiseMaker.Start();
}
public static void Noisy()
{
while(true)
Console.WriteLine("Hello!");
}
}
}
Just for the sake of completeness... With .Net 4.0 you have the Task Parallel Library. Simple example....
Task task = Task.Factory.StartNew(() =>
{
...doing stuff in a thread...
});