Tom
Tom

Reputation: 1373

Need Help Understanding C# Threading

I have a Console Application like this, which is using threads.

namespace ConsoleApplication1
{
    public static class Program
    {
        public static void ThreadMethod()
        {
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("ThreadProc: {0}", i);              
                Thread.Sleep(0);
            }
            Console.ReadLine();
        }
        public static void Main()
        {
            Thread t = new Thread(new ThreadStart(ThreadMethod));
            t.Start();
            for (int i = 0; i < 4; i++)
            {
                Console.WriteLine("Main thread: Do some work.");
                Thread.Sleep(0);
            }
            t.Join();
        }
    }
}

Output of program is the following:

Main thread: Do some work.
ThreadProc: 0
Main thread: Do some work.
ThreadProc: 1
Main thread: Do some work.
ThreadProc: 2
Main thread: Do some work.
ThreadProc: 3
ThreadProc: 4
ThreadProc: 5
ThreadProc: 6
ThreadProc: 7
ThreadProc: 8
ThreadProc: 9
ThreadProc: 10

Program executing properly. But I can't understand how this output is generated. How does this program work? I don't have good understanding of threads.

Can someone explain this for me?

Upvotes: 0

Views: 148

Answers (3)

Balaji
Balaji

Reputation: 1515

Threads are light weight processes in any of the programming language.

Normally, programs are executed as a Process. Similarly Threads are small processes which is grouped into one Main Process.

For Example : If a program is executing in system (say a, Web browser, the web browser is the process, and within the browser tabs,http connection,browser rendering are threads).

Actually, i didnt used threading in C#, one day i was using smtp.send(Mail) for sending mail. That send(mail) tooks upto 5secs of time to execute, because it needs internet connection. so i used separate thread to run this send() method outside of Main().

i hope you understand somewhat about THREADS.. is it?

Upvotes: 1

Glenn Ferrie
Glenn Ferrie

Reputation: 10410

As a beginner, if your objective is to understand how Thread operates in .NET I would recommend opening up the Threads window in Visual Studio and observe your program as it progresses through the procedure.

Instead of debugging your application by pressing F5, try pressing F11 to step into the first line of your code. It should bring you into debug mode with the procedure paused at the first line.

To open the Threads window press Ctrl + Alt + H or you can also find it by navigating the menus as follows: Debug > Windows > Threads. This menu is only visible when you are debugging.

As you step through the code you should be able to see the active thread toggle between the first and second thread each time that you call Thread.Sleep(0);

Hope this helps.

Upvotes: 2

Jan Zahradn&#237;k
Jan Zahradn&#237;k

Reputation: 2497

Main() is the starting point of your application.

t.Start() creates second thread which runs in parallel with main one. From now on code in method Main is executed along with method ThreadMethod. There is no quarantee in console output order. Change the sleep time and you will see different results.

t.Join will hold Main's execution until ThreadMethod finishes. Below this line, the program has only single thread and everything will be executed sequentially.

This article, mentioned in the comments, is a good explanation of threading in a C application, but these same principals apply to almost all languages.

This article explains threading in C# in more detail.

Upvotes: 7

Related Questions