Basic Thread

static private void myThread() {
  Thread.Sleep(2000);
  Console.WriteLine("hello world!");
} //

static void Main(string[] args) {
  Thread t = new Thread(new ThreadStart(myThread));
  t.IsBackground = true;
  t.Name = "MyBackgroundThread";
  t.Priority = ThreadPriority.Normal;
  Console.WriteLine("starting new thread");
  t.Start();

  Console.WriteLine("waiting for started thread to finish");
  t.Join();
  Console.WriteLine("press return to exit the program");
  Console.ReadLine();
} //

Nearly all PCs these days have multiple cores and/or CPUs. There is a speed benefit using them simultaneously. Windows can run applications independently. You can eg. run Notpad and Paint in parallel. Each application runs as a process. And each process can have many threads. A thread is more or less some code that is executed for a certain time period. Windows decides on its own when to pause a thread and switch to another thread. This is called context switching.

To run a simple thread we need the “System.Threading” namespace. You declare it with “using System.Threading;”.

The output console is a static and thread safe class. You neither have to instantiate the class nor synchronise the output. This is quite helpful, otherwise the example program would have to be a bit more complex. You would not be able to get access to the console from two different threads without risking deadlocks.

It is good practice to define the thread priority. That way Windows knows how important your code is and how much time it should assign for its execution. In a stock trading system you would eg. assign a high priority to the trade decision thread and a low priority to any screen output.

The join method is called on the main thread to let it wait until our newly generated thread terminates. You cannot restart a thread once it has terminated. In our example we started a background thread by setting t.IsBackground to true. All background threads (and the application) automatically stop as soon as all Foreground threads have terminated. Thus Foreground threads need to handle the shut down process, otherwise the application keeps on running unintentionally.

Some threads need parameters. Have a look at the following code. It uses a class to pass two parameters to the new thread.

private class parameters {
  public  double d;
  public  string s;
}//

static private void myThread(object xParameters) {
  parameters p = xParameters as parameters;
  Console.WriteLine("your parameters are " + p.d + " " + p.s);
} //

static void Main(string[] args) {
  Thread t = new Thread(new ParameterizedThreadStart(myThread));
  t.IsBackground = true;
  t.Name = "MyBackgroundThread";
  t.Priority = ThreadPriority.Normal;
  parameters p = new parameters{d = 2.5, s = "meters"};
  t.Start(p);
  t.Join();
  Console.WriteLine("press return to exit the program");
  Console.ReadLine();
} //

 

Advertisements

About Bastian M.K. Ohta

Happiness only real when shared.

Posted on December 2, 2013, in Basic, C#, Threading and tagged , , , , , , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: