How can we force a thread to sleep for an infinite period?

Call the Thread.Interupt() method.

How to pause the execution of a thread in .NET?

The thread execution can be paused by invoking the Thread.Sleep(IntegerValue) method where IntegerValue is an integer that determines the milliseconds time frame for which the thread in context has to sleep

What is a Thread?

A thread is an activity started by a process and it is the basic unit to which an operating system allocates processor resources.

What is the difference between a Thread and Process?

A process is a collection of virtual memory space, code, data, and system resources. A thread is code that is to be serially executed within a process. A processor executes threads, not processes, so each application has at least one process, and a process always has at least one thread of execution, known as the primary thread. A process can have multiple threads in addition to the primary thread. Prior to the introduction of multiple threads of execution, applications were all designed to run on a single thread of execution.

When a thread begins to execute, it continues until it is killed or until it is interrupted by a thread with higher priority (by a user action or the kernel’s thread scheduler). Each thread can run separate sections of code, or multiple threads can execute the same section of code. Threads executing the same block of code maintain separate stacks. Each thread in a process shares that process’s global variables and resources.

Can garbage collector control the activities of a thread?

Garbage Collection – Garbage collection is a heap-management strategy where a run-time component takes responsibility for managing the lifetime of the memory used by objects. This concept is not new to .NET – Java and many other languages/runtimes have used garbage collection for some time. The garbage collector runs periodically. It runs through a list of objects that are currently being referenced by an application. All objects that it does not find during this search are ready to be destroyed (using the finalize method) and hence free the memory. However, the runtime gets notified of the object that has been destroyed, only in the next round of the garbage collector’s periodic cycle.

In the class System.GC, there is a method called collect( ). This forces the garbage collector to collect all unreferenced objects immediately, thereby giving the developer some control over the garbage collector.

There is a gcConcurrent setting that can be set through the applications’s .config file. This specifies whether or not the garbage collector performs its activities on a specified thread or not.

We can view the performance monitor to view the activities of the garbage collector.

What is a thread? How to use and create a thread in .NET?

Threads – When we want to run one or more instances of a method, we make use of threading. Suppose we have a method like this…
Private Sub OnGoingProcess()
Dim i As Integer = 1
Do While True
ListBox1.Items.Add(“Repeatitions: ” + i)
i += 1
Loop
End Sub

Dim t As Thread
t = New Thread(AddressOf Me.OnGoingProcess)
t.Start()
The AddressOf operator creates a delegate object to the BackgroundProcess method. A delegate within VB.NET is a type-safe, object-oriented function pointer. After the thread has been instantiated, you begin the execution of the code by calling the Start() method of the thread. After the thread is started, you have some control over the state of it by using methods of the Thread object. You can pause a thread’s execution by calling the Thread.Sleep method. This method takes an integer value that determines how long the thread should sleep. If you wanted to slow down the addition of items to the listbox in the example above, place a call to the sleep method in this code:
Private Sub OnGoingProcess()
Dim i As Integer = 1
Do While True
ListBox1.Items.Add(“Repeatitions: ” + i)
i += 1
Thread.CurrentThread.Sleep(2000)
Loop
End Sub
You can also place a thread into the sleep state for an indeterminate amount of time by calling Thread.Sleep (System.Threading.Timeout.Infinite). To interrupt this sleep you can call the Thread.Interrupt method. Similar to Sleep and Interrupt are Suspend and Resume. Suspend allows you to block a thread until another thread calls Thread.Resume. The difference between Sleep and Suspend is that the latter does not immediately place a thread in the wait state. The thread does not suspend until the .NET runtime determines that it is in a safe place to suspend it. Sleep will immediately place a thread in a wait state. Lastly, Thread.Abort stops a thread from executing. In our simple example, we would want to add another button on the form that allows us to stop the process. To do this all we would have to do is call the Thread.Abort method as follows:

Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
t.Abort()
End Sub
This is where the power of multithreading can be seen. The UI seems responsive to the user because it is running in one thread and the background process is running in another thread. The cancel button immediately responds to the user’s click event and processing stops. The next example shows a rather simple situation. Multithreading has many complications

What the way to stop a long running thread ?

System.Threading.Thread.Abort