using System;
class abc
{
public static void fun()
{
Console.WriteLine("Function");
}
public static void Main(string[] args)
{
ThreadStart ts=new ThreadStart(fun());
Console.WriteLine("Main begins");
Thread t=new Thread(ts);
t.Start();
Console.WriteLine("Main ends");
}
}

Output showing
Main Begins
Main ends
Function.


My Question


The thread has been started and then the "Main ends" message has been written. When the t.Start() method is invoked the function func() is to be called, but it is completing the work of main function and only returning back to function, why is it so.


If i use static in the function the output is

Main Begins
Function
Main Ends

Why is this change happening. What is the order of execution of threads. Kinly help me.

Sorry previous question was wrong kindly answer for this question.


Snippet

using System;
class abc
{
public static void fun()
{
Console.WriteLine("Function");
}
public  void Main(string[] args)
{
abc obj=new abc();
ThreadStart ts=new ThreadStart(obj.fun());
Console.WriteLine("Main begins");
Thread t=new Thread(ts);
t.Start();
Console.WriteLine("Main ends");
}
}

Output showing
Main Begins
Main ends
Function.


My Question


The thread has been started and then the "Main ends" message has been written. When the t.Start() method is invoked the function func() is to be called, but it is completing the work of main function and only returning back to function, why is it so.


If i use static in the function the output is

Main Begins
Function
Main Ends

Why is this change happening. What is the order of execution of threads. Kinly help me.

When dealing with threads, consider the timing involved. The Start() is an invocation and not a direct call to the target thread function.
Therefore, (in your example), the Main has had no interupts to prevent it from completing before the thread function begins.
If you were to cause the main to sleep for a couple milliseconds after the Start, it would allow the thread function to startup before the Main completed (eg interupt).

The the static assignment allows the function to invoke quickly (timing).
Just because it is static does not mean it will always startup in the desired order, again timing.

Add a using System.Threading, and after the Start() call, use Thread.Sleep(2) and you should find the desired results even if the thread function is not static.
You may also wish to set the thread's IsBackground property to true before it is started. This will help the thread to perform better outside the main thread activity.

Hope this helps..
Jerry

Thanks for your reply Jerry.

Yes i understood the timing concept. Can you explain me the thread's IsBackground property. I searched in the book but i could'nt find. I brosed in net bt im not that clear.

IsBackGround
Quote from Visual Studio 2008 on-line help
A thread is either a background thread or a foreground thread. Background threads are identical to foreground threads, except that background threads do not prevent a process from terminating. Once all foreground threads belonging to a process have terminated, the common language runtime ends the process. Any remaining background threads are stopped and do not complete.


My Info - a background thread is more forgiving than a foreground thread. The main application can not stop while any foreground threads are active. All background threads are automatically aborted when the main thread (the application) terminates.

Thanks Jerry i have got the answer for my question. I have gave one reputation point for you.


I have marked the thread to be solved. Looking for best support form you in future.


I have posted another question, which deals with selection of platform. I will be very anxious to get the support for that question too.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.