views:

207

answers:

7

Hi All

Just looking for something ultra simple. I need to spawn a method off to a new thread.

  1. I don't care when or how it ends.

Can somebody please help me with this?

Thank you

+1  A: 

Check out this MDSN article about the Thread Pool. This should have you ask for new threads and other thread realted stuff.

Sir Graystar
+3  A: 
Thread thread=new Thread(() => {
  // thread code here
});

thread.Start();
Blindy
+1  A: 

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

ho1
+8  A: 

For starting a new thread in winforms, the ThreadPool is hard to beat for simplicity:

ThreadPool.QueueUserWorkItem(state => 
{
    // put whatever should be done here
});
Fredrik Mörk
A: 

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!");
        }
    }
}
LaustN
+3  A: 

When using Winforms you could also use the 'BackgroundWorker'

KroaX
+2  A: 

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...
  });
Rusty