Daily Archives: December 2, 2013

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();
} //

 

Introduction

Programming is not about knowing the programming language. You can learn any language in a few days. It is about algorithms. How do you solve a problem efficiently? All roads lead to Rome, but some are shorter or faster than others.
In this blog I will try building up a reference guide for hardcore programming. This includes techniques to solve problems in most efficient ways.
I aim at developers, who have some experience with C#, but want to deepen their knowledge and make sure they are ready to at least be able to pass the C# Microsoft Solution Developer (MCSD) 70-483 easily.

I am active in the stock market trading business since 1997. So I will finally go down that road and concentrate on speed. I wish I could explain everything in a single day, but realistically we are looking at roughly two years before I get there. My time is limited, this is my second blog and I know a good blog needs some dedication.

You should have at least one year of experience programming essential business logic.