Answered by:
How can start and stop the threads [Parameterized thread]?
Question
-
Answers
-
Hi, Prabu
When you call Thread.Suspend on a thread, the system notes that a thread suspension has been requested and allows the thread to execute until it has reached a safe point before actually suspending the thread. A safe point for a thread is a point in its execution at which garbage collection can be performed.
Once a safe point is reached, the runtime guarantees that the suspended thread will not make any further progress in managed code. A thread executing outside managed code is always safe for garbage collection, and its execution continues until it attempts to resume execution of managed code.
Note: In order to perform a garbage collection, the runtime must suspend all the threads except the thread performing the collection. Each thread must be brought to a safe point before it can be suspended.
And you can check the current state of a thread using the Thread’s
ThreadStateproperty.A Thread can be in one the following state.
And if you have further problems, please show the details and create a new post so that more people in forum can see and discuss together.Thread State
Description
Unstarted
Thread is Created within the common language run time but not Started still.
Running
After a Thread calls Start method
WaitSleepJoin
After a Thread calls its wait or Sleep or Join method.
Suspended
Thread Responds to a Suspend method call.
Stopped
The Thread is Stopped, either normally or Aborted.
Thank you
BR
All replies
-
Ill just post some code and you can dig in it. Also can refer to the msdn documentation.
If you have any questions just ask and ill explain then. Sorry, about to eat dinner, but figure i would make a quick code post for you:)
public
delegate void UpdateBarCallback(int i);public
void StartProgressBarThread()
{
Thread t = new Thread(new ThreadStart(RunProgressBar));
t.Start();
} public void RunProgressBar()
{
for (int i = myBar.Minimum; i <= myBar.Maximum; i++)
{
this.Invoke(new UpdateBarCallback(this.UpdateBar),
new object[] { i });
}
} private void UpdateBar(int i)
{
myBar.Value = i;
}Oh yea, thought i would add i tend to avoid threads at all possible costs =D Usually there is a better way of doing something, but every now and then the need comes along.
-
Hi, Prabu
. Net Framework has thread-associated classes in System.Threading namespace.
The following steps demonstrate how to create a thread in C#.
Step 1. Create the call back function
This method will be a starting point for our new thread. It may be an instance function of a class or a static function. Incase of instance function, we should create an object of the class, before we create the
ThreadStartdelegate. For static functions we can directly use the function name to instantiate the delegate. The callback function should have void as both return type and parameter. Because theThreadStartdelegate function is declared like this. (For more information on delegate see MSDN for “Delegates”).Step 2: Create a
System.Threading.Threadobject.Creating an object to System.Threading.Thread creates a managed thread in .Net environment. The Thread class has only one constructor, which takes a
ThreadStartdelegate as parameter. TheThreadStartdelegate is wrap around the callback method, which will be called when we start the thread.Step 3: Starting the Thread.
We can start the newly created thread using the Thread’s
Startmethod. This is an asynchronous method, which requests the operating system to start the current thread.For Example:
// This is the Call back function for thread.
Public static void MyCallbackFunction()//--step 1
{
while (true)
{
System.Console.WriteLine(“ Hey!, My Thread Function Running”);
………
}
}
public static void Main(String []args)
{
// Create an object for Thread
Thread MyThread = new Thread(new ThreadStart
(MyCallbackFunction));//--step 2
MyThread.Start()//--step 3
……
}Killing a Thread:
We can kill a thread by calling theAbortmethod of the thread. Calling theAbortmethod causes the current thread to exit by throwing theThreadAbortException.MyThread.Abort();Suspend and Resuming Thread:
We can suspend the execution of a thread and once again start its execution from another thread using the Thread object’s
SuspendandResumemethods.MyThread.Suspend() // causes suspend the Thread Execution.MyThread.Resume() // causes the suspended Thread to resume its execution.For more details about it: http://msdn2.microsoft.com/en-us/library/7a2f3ay4.aspxBR -
Hi Figo Fei,
Thanks for your detailed explanation about the threading concept. It really helps me to learn about it and usage of threading. I am having some difficulties for use this thread in my application. Can you please help?
Here is my usage scenario.
I have written an application project that build all the CS & VB project file in given directory up to its leaf directory. Once I started the testing process then I couldn’t able to stop the process. I need to have both the options like stop the process & resumes the process. I am getting idea about suspend & resume the process but not stop process. Please help to complete my application.
Thanks in advance for your time.
-
Hi, Prabu
When you call Thread.Suspend on a thread, the system notes that a thread suspension has been requested and allows the thread to execute until it has reached a safe point before actually suspending the thread. A safe point for a thread is a point in its execution at which garbage collection can be performed.
Once a safe point is reached, the runtime guarantees that the suspended thread will not make any further progress in managed code. A thread executing outside managed code is always safe for garbage collection, and its execution continues until it attempts to resume execution of managed code.
Note: In order to perform a garbage collection, the runtime must suspend all the threads except the thread performing the collection. Each thread must be brought to a safe point before it can be suspended.
And you can check the current state of a thread using the Thread’s
ThreadStateproperty.A Thread can be in one the following state.
And if you have further problems, please show the details and create a new post so that more people in forum can see and discuss together.Thread State
Description
Unstarted
Thread is Created within the common language run time but not Started still.
Running
After a Thread calls Start method
WaitSleepJoin
After a Thread calls its wait or Sleep or Join method.
Suspended
Thread Responds to a Suspend method call.
Stopped
The Thread is Stopped, either normally or Aborted.
Thank you
BR
-
I recommend that you read the whitepaper that Albahari Joseph wrote on Thread Programming in C#. You will find a lot of information about the different lock mechanisms and more background information:
-
Hi Figo Fei,
Here after I will post my query in new post. This is related to the previous one. So I remain this query in the sample thread. When I use the following statement, the condition always seems to be false.
if(this.myThread.ThreadState == System.Threading.ThreadState.SuspendRequested)
{
this.myThread.Suspend();
}
if(this.myThread.ThreadState == System.Threading.ThreadState.Suspended)
{
this.myThread.Resume();
}
if(this.myThread.ThreadState == System.Threading.ThreadState.AbortRequested ||
this.myThread.ThreadState == System.Threading.ThreadState.Running ||
this.myThread.ThreadState == System.Threading.ThreadState.StopRequested ||
this.myThread.ThreadState == System.Threading.ThreadState.Suspended ||
this.myThread.ThreadState == System.Threading.ThreadState.Unstarted)
{
this.myThread.Abort();
this.myThread.Join();
this.myThread = null;
}
Please let me know where I made wrong. Thanks in advance.
-
Hi, Prabu
Sorry for having misused Thread.Suspend and Thread.Resume to show you how threads work. Actually, these two methods are both obsolete now.
And do not use the Suspend and Resume methods to synchronize the activities of threads. You have no way of knowing what code a thread is executing when you suspend it. If you suspend a thread while it holds locks during a security permission evaluation, other threads in the AppDomain might be blocked. If you suspend a thread while it is executing a class constructor, other threads in the AppDomain that attempt to use that class are blocked. Deadlocks can occur very easily.
BR
-
-
Hi, Prabu
That's all about the other classes in System.Threading issues, please use other classes in System.Threading, such as Monitor, Mutex, Event, and Semaphore, to synchronize Threads or protect resources. And static method Thread.Sleep is not affected in .NET2.0.
More details about System.Threading: http://msdn2.microsoft.com/en-us/library/system.threading.aspx
Thank you
BR