Multiple Threads

Program.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.IO;
namespace l3p1
{
class Program
{
public const int NUM_THREADS = 10;
static FileStream myFile = new FileStream(@”E:\1311\lab3\l3p1\l3p1\f.txt”, FileMode.Open,
FileAccess.Read);
static StreamReader fileRead = new StreamReader(myFile);
static void Main(string[] args)
{
List<Thread> readThreads = new List<Thread>();
for (int i = 0; i < NUM_THREADS; i++)
{
Reader rd = new Reader(myFile);
Thread myThread = new Thread(rd.DoWork);
myThread.Name = i.ToString();
readThreads.Add(myThread);
}
foreach (Thread t in readThreads)
{
t.Start();
}
Console.ReadLine();
}
static void readFile()
{
while [...]

Monitors

A monitor is the most basic synchronization construct.
Any object can have a monitor associated with it, and no monitor can be associated with more than one object.
Monitors have a “lock,” which may be acquired by only one thread at a time. It must be released by that thread before another thread can acquire it.
You can [...]

WatchDog

Computer science frequently employs the concept of a watchdog—an entity whose responsibility is to ensure the correct function, or handling of incorrect functions, in another entity. A common pattern is the watchdog timer, usually responsible for making sure that another task completes in a reasonable time.
Console Application

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace pr9
{
class Program
{
static void [...]

Thread.Join

Before .NET, I was often asked questions about how to wait for a Win32 thread to exit. The solution was to acquire a handle to the thread and wait on the handle. Or alternatively, you could setup an event that was triggered at the end of the thread and wait on that event. .NET provides [...]

Another Thread Example

Many times, you will have a slightly different task to perform in each thread and will want to pass each thread a parameter of some sort to differentiate its task from that of the others.
While there are several reasonable ways of doing this, the most straightforward is to create a Task object that holds the [...]

Simple Thread Example

Console Application

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace pr6
{
class Program
{
static void SayHello()
{
Console.WriteLine(“Hello, “);
[...]