Simple Threading With C#

9 03 2008

Here is just a basic example for how to use threads.

using System;
using System.Threading; // <-- if you dont want to include the entire Threading namespace, just prefix your threading object with System.Threading.[object]
namespace ThreadingExample
{
class ThreadingExample
{
public void Main(String[] args)
{
Console.WriteLine("Starting...");
Thread myThread = new Thread(myThreadedMethod); // <-- remember, don't put the () after the method you want running on that thread!
myThread.Start();
while (true)
{
// do stuff here
}
}
public void myThreadedMethod()
{
while(true)
{
//do other stuff here too
}
}
}
}

Now, both of those while loops will be running forever simultaneously. Like i said, this is just a simple example. Threading can get extremely complicated. If you want to know about threading with a method that needs parameters, you should go look through the MSDN library on it. It has an -ok- example. and some explanation I believe.


Actions

Information

2 responses

9 03 2008
Michael Cromwell

One of the best resources I have found on using threads is http://www.yoda.arachsys.com/csharp/threads/ it pretty much covers everything

23 03 2008
Denisjs

thanks much, brother

Leave a comment