Simple Threads

In the following listing, you learn to create and manage simple threads. Let's have a look at the code:

using System;
using System.Threading;

class MyThread
{
        static void Main()
        {
                MyThread sth = new MyThread();
                Thread thr = new Thread(new ThreadStart(sth.Message));
                thr.Start();
        }

        public void Message()
        {
                Console.WriteLine("Hello from thread");
        }
}

The following example works without explicitly creating an instance of an object:

using System;
using System.Threading;

class MyThread
{
        static void Main()
        {
                Thread thr = new Thread(new ThreadStart(Message));
                thr.Start();
        }

        static public void Message()
        {
                Console.WriteLine("Hello from thread");
        }
}

We create a class called MyThread. Inside the Main function, we create an instance of ...

Get Mono Kick Start now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.