56
Object Oriented Programming Multithreading Multithreading Dr. Mike Spann Dr. Mike Spann [email protected] [email protected]

Object Oriented Programming Multithreading Dr. Mike Spann [email protected]

Embed Size (px)

Citation preview

Page 1: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Object Oriented Programming

MultithreadingMultithreading

Dr. Mike SpannDr. Mike Spann

[email protected]@bham.ac.uk

Page 2: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Contents IntroductionIntroduction Creating and executing threadsCreating and executing threads Simple example – a counter threadSimple example – a counter thread Thread priority and thread schedulingThread priority and thread scheduling Thread statesThread states Thread synchronisationThread synchronisation Multi-threading and GUI’sMulti-threading and GUI’s SummarySummary

Page 3: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Introduction Multi-threading allows program statements to be Multi-threading allows program statements to be

executed concurrentlyexecuted concurrently For example, we can create an application that For example, we can create an application that

downloads a large multimedia file whilst at the downloads a large multimedia file whilst at the same time it can support other taskssame time it can support other tasks

The CLR automatic garbage collector runs The CLR automatic garbage collector runs concurrently with the rest of a .NET applicationconcurrently with the rest of a .NET application

The .NET framework provides extensive support for The .NET framework provides extensive support for multithreadingmultithreading It is easy to specify that certain sections of code It is easy to specify that certain sections of code

reside in different execution threadsreside in different execution threads

Page 4: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Introduction Its important to understand the difference between Its important to understand the difference between multi-taskingmulti-tasking (as carried (as carried

out by an operating system) and out by an operating system) and multi-threading multi-threading (as carried out by a single (as carried out by a single application)application)

Multi-tasking refers to an operating system running several Multi-tasking refers to an operating system running several processes processes concurrentlyconcurrently Each process has its own completely independent data Each process has its own completely independent data Multi-tasking is difficult to incorporate in application programs requiring Multi-tasking is difficult to incorporate in application programs requiring

system programming primitivessystem programming primitives A thread is different from a process in that threads A thread is different from a process in that threads share the same datashare the same data

Switching between threadsSwitching between threads involves much less overhead than switching involves much less overhead than switching between programsbetween programs

Sharing data can lead to programming complications (for example in Sharing data can lead to programming complications (for example in reading/writing databases)reading/writing databases)

Page 5: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Creating and executing threads A thread can be initiated using the A thread can be initiated using the ThreadStartThreadStart delegate delegate This delegate object is passed into the constructor of the This delegate object is passed into the constructor of the Thread Thread

objectobject The delegate is initialized with a method which runs in a The delegate is initialized with a method which runs in a

separate threadseparate thread In simple use, this method must have no parameters and In simple use, this method must have no parameters and

return return voidvoid Calling Calling Thread.Start()Thread.Start() starts the thread starts the thread Similar but more flexible than to the Java approach where the Similar but more flexible than to the Java approach where the

code running in a separate thread is always in the code running in a separate thread is always in the run() run() methodmethod Java also requires the use inheritance or interfaces Java also requires the use inheritance or interfaces

Page 6: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Creating and executing threads

using System;using System.Threading;

class ThreadApp {

public static void Main(string[] args) { MyClass myObject=new MyClass();

Thread thread=new Thread(new ThreadStart(myObject.myMethod));thread.Name="thread";thread.Start();

}}

class MyClass{

public void myMethod(){

Thread thisThread=Thread.CurrentThread;Console.WriteLine("Thread " + thisThread.Name + “ running");

}}

Page 7: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Creating and executing threads

Main() thread

new thread

Code in myMethod() executed

new thread created

Page 8: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Simple example – a counter thread We will create a simple application which We will create a simple application which

creates separate threads to update a count within creates separate threads to update a count within a graphical windowa graphical window

The application will initiate any number of The application will initiate any number of threads from a simple GUIthreads from a simple GUI

Initially each thread will be given equal Initially each thread will be given equal scheduling priorityscheduling priority We will look thread scheduling and priority We will look thread scheduling and priority

laterlater

Page 9: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Simple example – a counter thread Demos\Counter Thread\CounterThread.exeDemos\Counter Thread\CounterThread.exe

Page 10: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Simple example – a counter thread

Main thread

Create counter thread

Create counter thread

…..

Page 11: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Simple example – a counter thread

using System;using System.Windows.Forms;using System.Threading;

public partial class CounterThreadApp : Form{ private int nThread = 0;

public CounterThreadApp() { InitializeComponent(); }

private void button1_Click(object sender, EventArgs e) { CounterForm c = new CounterForm(nThread++); c.Show(); Thread thread = new Thread(new ThreadStart(c.Count)); thread.Start(); }}

Page 12: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

using System;using System.Threading;using System.Drawing;using System.Windows.Forms;

public partial class CounterForm : Form{ private int count = 0; private int threadCount;

public CounterForm(int tc) { InitializeComponent(); threadCount = tc; }

public void Count() { do { count++; Thread.Sleep(10); countLabel.Text = "Count= " + count; if (threadCount == 0) BackColor = Color.Blue; else if (threadCount == 1) BackColor = Color.Yellow; else if (threadCount == 2) BackColor = Color.Red; else BackColor = Color.Green; Invalidate(); } while (true); }}

Page 13: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Thread priority and thread scheduling Each thread has a priority which determines how .NET Each thread has a priority which determines how .NET

schedules that threadschedules that thread Obviously threads with higher priority will be given more Obviously threads with higher priority will be given more

CPU timeslices than lower priority threadsCPU timeslices than lower priority threads The priority can range between The priority can range between ThreadPriority.LowestThreadPriority.Lowest to to

ThreadPriority.Highest ThreadPriority.Highest ((ThreadPriorityThreadPriority is an enumeration) is an enumeration) By default each thread is initialized to By default each thread is initialized to

ThreadPriority.NormalThreadPriority.Normal A thread’s priority can be adjusted through the A thread’s priority can be adjusted through the Priority Priority

propertyproperty

Page 14: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Thread priority and thread scheduling

..NET schedules threads of equal priority in a NET schedules threads of equal priority in a round robinround robin fashion fashion Each thread is given a CPU timeslice and then Each thread is given a CPU timeslice and then

pre-emptedpre-empted Threads of lower priority are only scheduled Threads of lower priority are only scheduled

when higher priority threads have completed or when higher priority threads have completed or are sleeping or waiting for system resources are sleeping or waiting for system resources

The thread priority can be set through the The thread priority can be set through the Priority Priority property of the property of the Thread Thread classclass

Page 15: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Thread priority and thread scheduling

Highest Priority

AboveNormal Priority

Normal Priority

BelowNormal Priority

Lowest Priority

A B

C

D E

F

Page 16: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Thread states

At anyone time, a thread can exist in one of a number of statesAt anyone time, a thread can exist in one of a number of states The thread state affects its schedulingThe thread state affects its scheduling It’s important to understand what these states are and how a It’s important to understand what these states are and how a

thread undergoes a transition between the statesthread undergoes a transition between the states The full transition diagram is fairly complicated and we only The full transition diagram is fairly complicated and we only

need the basic ideas initiallyneed the basic ideas initially Initially a thread is in an Initially a thread is in an UnstartedUnstarted state and calling the state and calling the

thread’s thread’s Start() Start() method puts it in the method puts it in the RunningRunning state state The newly running thread can then run to completion The newly running thread can then run to completion

(alongside the original thread) until it reaches its (alongside the original thread) until it reaches its StoppedStopped state state However, other more complex scenarios are possibleHowever, other more complex scenarios are possible

Page 17: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Thread states

Unstarted

Running

Start()

WaitSleepJoin Stopped Blocked

Page 18: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Thread states A running thread can also enter the A running thread can also enter the StoppedStopped state state

if its if its Abort()Abort() method is called method is called This causes an exception to be thrown in the This causes an exception to be thrown in the

thread which calls the thread which calls the Abort() Abort() methodmethod

Unstarted

Running

Start()

Stopped

CompletedAbort()

Page 19: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Thread states A thread becomes A thread becomes BlockedBlocked if it issues an IO if it issues an IO

request or an object lock is not available (see later)request or an object lock is not available (see later)

Unstarted

Running

Start()

Blocked

Lock unavailableIssues IO requestLock available

IO request completed

Page 20: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Thread states

The The WaitSleepJoinWaitSleepJoin state can be entered and exited in a state can be entered and exited in a number of waysnumber of ways A thread can calls its A thread can calls its Sleep Sleep method to put itself to sleep for method to put itself to sleep for

a number of millisecondsa number of milliseconds The thread can call the The thread can call the Monitor Monitor object’s object’s Wait() Wait() method to method to

suspend its executionsuspend its executionWaiting threads are in a queue to resume execution Waiting threads are in a queue to resume execution

using the using the Moniter Moniter object’s object’s Pulse()Pulse() method methodAlso one thread can call the waiting or sleeping thread’s Also one thread can call the waiting or sleeping thread’s

Interrupt() Interrupt() method causing it to re-enter the method causing it to re-enter the Running Running statestate

Page 21: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Thread states The The Join()Join() method of method of ThreadThread causes the calling causes the calling

thread to suspend execution until the thread who’s thread to suspend execution until the thread who’s Join()Join() method has been called terminates method has been called terminates It’s a simple method of synchronising the It’s a simple method of synchronising the

execution of 2 threadsexecution of 2 threads There is also a There is also a Join(int t)Join(int t) method whereby the method whereby the

calling thread resumes execution in calling thread resumes execution in tt milliseconds no matter whatmilliseconds no matter what

This version is useful in implementing timersThis version is useful in implementing timers

Page 22: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Thread states

Main thread

Creates thread t

Calls t.Join()

Main thread suspends execution Thread t terminates

Main thread resumes

Page 23: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Thread states

Unstarted

Running

WaitSleepJoin

Start()

Wait(),Sleep(t) Join(t)

Sleep interval expires

Pulse(), PulseAll()

Interrupt()

Page 24: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Thread states

Example 1. Putting a thread to sleepExample 1. Putting a thread to sleep

using System;using System.Threading;class SleepTest{

static void SayHello() { Console.WriteLine("Hello, "); Thread.Sleep(10000); Console.WriteLine("World"); }

static void Main(string[] args) { Thread thread1 = new Thread(new ThreadStart(SayHello)); thread1.Start(); } }

Page 25: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Thread states

Example 2. Interrupting a sleeping threadExample 2. Interrupting a sleeping thread Causes an exception to be thrownCauses an exception to be thrownusing System;using System.Threading;class SleepTest{ static void SayHello() { Console.WriteLine("Hello, ");

try {Thread.Sleep(10000); }

catch (ThreadInterruptedException e) {Console.WriteLine("World");} } static void Main(string[] args) { Thread thread1 = new Thread(new ThreadStart(SayHello)); thread1.Start(); thread1.Interrupt(); } }

Page 26: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Thread states

Demos\Thread States\SleepTest1.exeDemos\Thread States\SleepTest1.exe Demos\Thread States\SleepTest.exeDemos\Thread States\SleepTest.exe

Page 27: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

ThreadStates

Example 3. Synchronising 2 threads using Example 3. Synchronising 2 threads using Join()Join()

using System;using System.Threading;class JoinTest1{ static int total = 0;

static void Adder() { for (int i = 0; i < 100000; i++) total += i; } static void Main(string[] args) { Thread thread1 = new Thread(new ThreadStart(Adder)); thread1.Start(); thread1.Join(); Console.WriteLine("The total= " + total);

}}

Page 28: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

ThreadStates

Without the synchronising Without the synchronising Join()Join(), a sum of , a sum of zero is displayedzero is displayed The main thread must wait for the adder The main thread must wait for the adder

thread to completethread to complete The main thread waits for the adder thread The main thread waits for the adder thread

using using Join()Join() Demos\Thread States\ThreadSynch.exeDemos\Thread States\ThreadSynch.exe

Page 29: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Thread states

Example 4. A watchdog timer using Example 4. A watchdog timer using Join()Join()

using System;using System.Threading;class JoinTest{ static void SayHello() { Console.WriteLine("Hello, "); Thread.Sleep(15000); // Something goes wrong, here. Console.WriteLine("World"); } static void Main(string[] args) { Thread thread1 = new Thread(new ThreadStart(SayHello)); thread1.Start(); thread1.Join(10000); if ( thread1.IsAlive ) { Console.WriteLine("Thread 1 timed out. I am killing it."); thread1.Abort(); } } }

Page 30: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Thread states

Demos\Thread States\Watchdog.exeDemos\Thread States\Watchdog.exe

Page 31: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Thread synchronisation

Threads need to share access to objects and may update Threads need to share access to objects and may update shared objectsshared objects For example multiple threads may access a database For example multiple threads may access a database

for an online flight booking systemfor an online flight booking system One thread may be updating a database entry whilst One thread may be updating a database entry whilst

another is reading it may lead to problemsanother is reading it may lead to problems We can We can synchronise synchronise threads so that they must complete threads so that they must complete

their action before another thread is scheduledtheir action before another thread is scheduled We do this by giving one thread at a time exclusive We do this by giving one thread at a time exclusive

access to code that manipulates shared objectsaccess to code that manipulates shared objects

Page 32: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Thread synchronisation

Unsynchronised threads

Thread 1 Thread 2

Update database

Read database

Pre-empt

Pre-empt

Page 33: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Thread synchronisation

Synchronised threads

Thread 1 Thread 2

Update database

Read database

Page 34: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Thread synchronisation

A thread can A thread can locklock a shared object so that it has exclusive a shared object so that it has exclusive access to itaccess to it

Class Class MonitorMonitor provides methods for locking objects provides methods for locking objects Monitor.Enter(anObject) Monitor.Enter(anObject) acquires a lock on acquires a lock on anObjectanObject All other threads are blocked if they try to access All other threads are blocked if they try to access

anObjectanObject Monitor.Exit(anObject) Monitor.Exit(anObject) releases the lock on releases the lock on anObjectanObject A thread waiting to access A thread waiting to access anObjectanObject is then unblocked is then unblocked

Page 35: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Thread synchronisation

C# also provides a C# also provides a locklock keyword in front of a keyword in front of a piece of codepiece of code

A thread entering this piece of code will then A thread entering this piece of code will then have exclusive access to shared object have exclusive access to shared object anObjectanObject

lock(anObject){ // Synchronized code here}

Page 36: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Thread synchronisation

Example – A seat reservation systemExample – A seat reservation system Seats for a concert can be reserved through Seats for a concert can be reserved through

booking agentsbooking agents

The processing for each booking agent runs in a The processing for each booking agent runs in a separate thread – possibly on a different separate thread – possibly on a different processorprocessor

Each booking transaction must check seat Each booking transaction must check seat availability before reserving the seatavailability before reserving the seat

Page 37: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Thread synchronisation

Seat reservation database

Booking agent 1

Booking agent 2

….. Booking agent n

Check availability

Reserve seat

Check availability

Reserve seatCheck availability

Reserve seat

Page 38: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Thread synchronisation

Pseudo-code for booking a seatPseudo-code for booking a seat

if seat n is available book seat n;

Code not Code not atomicatomic For an For an unsynchronised thread unsynchronised thread it can be pre-empted it can be pre-empted

by another thread after the by another thread after the if if statementstatement The thread might think the seat is available but it The thread might think the seat is available but it

then might be booked by the pre-empting threadthen might be booked by the pre-empting thread The seat will be double bookedThe seat will be double booked

Page 39: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Thread synchronisation

Booking agent 1 Booking agent 2

check availability

book seat

pre-empted

re-schedule

Unsynchronised threads

Page 40: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Thread synchronisation

We will first show the code for an We will first show the code for an unsynchronised applicationunsynchronised application This will lead to This will lead to double bookingsdouble bookings where a where a

seat can be booked twiceseat can be booked twice We will then show how we can sychronize We will then show how we can sychronize

the thread access to the shared object thus the thread access to the shared object thus eliminating double bookings with minimal eliminating double bookings with minimal code changescode changes

Page 41: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Thread synchronisation

2 main classes2 main classes SeatBookingsSeatBookings

Contains the seat booking information (just a Contains the seat booking information (just a simple array)simple array)

Contains Contains isAvailable()isAvailable() and and bookSeat() bookSeat() methods methods which access the seat booking informationwhich access the seat booking information

BookingAgentBookingAgentContains a reference to a Contains a reference to a SeatBookingsSeatBookings object object

which is thus a shared object between all of the which is thus a shared object between all of the BookingAgent BookingAgent objects objects

Page 42: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Thread synchronisation

class SeatBookings{ private int[] seats; public int totalBooked = 0;

public SeatBookings(int[] s) { seats=s; } public bool isAvailable(int seatno) { return (seats[seatno] == 0); }

public void bookSeat(int seatno) { if (isAvailable(seatno)) {

seats[seatno]++; totalBooked++; Console.WriteLine("Seat " + seatno + " booked.

Total booked= " + totalBooked); }

}

public bool doubleBooked(int seatno) { return (seats[seatno] > 1); }}

Page 43: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Thread synchronisationclass BookingAgent { private SeatBookings sb; private int agent; private Random randomNumers=new Random();

public BookingAgent(SeatBookings s, int a) { sb=s; agent=a; }

public int getAgent() { return agent; }

public void bookSeat() { do { int seatno = (int)(randomNumers.Next(0, 1000)); sb.bookSeat(seatno);

if (sb.doubleBooked(seatno)) Console.WriteLine("Seat number " + seatno +

" double booked!"); } while (sb.totalBooked<1000);

Console.WriteLine("All bookings complete"); }}

Page 44: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Thread synchronisation

using System;using System.Threading;

public class SeatBookingApp{ static void Main(string[] args) { int[] seats=new int[1000];

for (int j=0; j<1000; j++) seats[j]=0;

BookingAgent[] b=new BookingAgent[10]; SeatBookings s=new SeatBookings(seats); Thread[] bookingThreads= new Thread[10];

for (int i=0; i<10; i++)

{ b[i] = new BookingAgent(s, i); bookingThreads[i]=new Thread(new ThreadStart(b[i].bookSeat));

bookingThreads[i].Start();

} }}

Page 45: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Thread synchronisation

Demos\Thread Synch\Demos\Thread Synch\SeatBookingAppUnsynch.exeSeatBookingAppUnsynch.exe

Page 46: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Thread synchronisation We can synchronise the threads by ensuring that We can synchronise the threads by ensuring that

the the SeatBookings.bookSeat() SeatBookings.bookSeat() method is enclosed method is enclosed by by Monitor.Enter()Monitor.Enter() and and Monitor.Exit()Monitor.Exit() The The SeatBookingsSeatBookings object is then object is then locked locked by the by the

thread which has current accessthread which has current access

SeatBookings object

Booking agent 1

Booking agent 2

locked locked out

Page 47: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Thread synchronisationclass SeatBookings{ private int[] seats; public int totalBooked = 0;

public SeatBookings(int[] s) { seats = s; } public bool isAvailable(int seatno) { return (seats[seatno] == 0); }

public void bookSeat(int seatno) { Monitor.Enter(this); // Lock object if (isAvailable(seatno)) { seats[seatno]++; totalBooked++; Console.WriteLine("Seat " + seatno +

" booked. Total booked= " + totalBooked); } Monitor.Exit(this); }

public bool doubleBooked(int seatno) { return (seats[seatno] > 1); }}

Page 48: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Thread synchronisationSynchronised threads

Booking agent 1 Booking agent 2

check availability

book seat

check availability

book seat

check availability

book seat

Page 49: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Thread synchronisation

Demos\Thread Synch\Demos\Thread Synch\SeatBookingAppSynch.exeSeatBookingAppSynch.exe

Page 50: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Multi-threading and GUI’s

Multiple threads updating GUI components are not Multiple threads updating GUI components are not predictablepredictable GUI components are not GUI components are not thread safethread safe All updates to GUI components should be done in All updates to GUI components should be done in

the the User Interface (UI) ThreadUser Interface (UI) ThreadFor example, writing to a labelFor example, writing to a label

.NET provides an .NET provides an InvokeInvoke method of class method of class Control Control to specify which GUI processing statements will to specify which GUI processing statements will be executed in the be executed in the UIUI thread thread

Page 51: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Multi-threading and GUI’s

Method Method Invoke()Invoke() takes a delegate object as takes a delegate object as an argument which is initialized with the an argument which is initialized with the method that is to be called in the method that is to be called in the UIUI thread thread

We can also include a parameter list for We can also include a parameter list for the initializer method the initializer method

myGUIComponent.Invoke(new myDelegate(myMethod));

myGUIComponent.Invoke(new myDelegate(myMethod),new object[] {arg1,arg2...});

Page 52: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Multi-threading and GUI’s

The following application interacts with a The following application interacts with a GUI to suspend thread execution by GUI to suspend thread execution by clicking on a checkbox controlclicking on a checkbox control

Also the text label is updated to indicate a Also the text label is updated to indicate a suspended thread suspended thread

Page 53: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Multi-threading and GUI’s

Demos\Threads and GUIs\Demos\Threads and GUIs\GUIThreadsForm.exeGUIThreadsForm.exe

Page 54: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

using System;using System.Windows.Forms;using System.Threading;

public partial class GUIForm : Form{ private Counter c1,c2,c3;

public GUIForm() {InitializeComponent();} private void checkBox1_CheckedChanged(object sender, EventArgs e) { if (sender == checkBox1) c1.toggle(); else if (sender == checkBox2) c2.toggle(); else if (sender == checkBox3) c3.toggle(); }

private void GUIForm_Load(object sender, EventArgs e) { c1 = new Counter(label1); Thread thread1 = new Thread(new ThreadStart(c1.count)); thread1.Name = "Thread 1"; thread1.Start();

c2 = new Counter(label2); Thread thread2 = new Thread(new ThreadStart(c2.count)); thread2.Name = "Thread 2"; thread2.Start();

c3 = new Counter(label3); Thread thread3 = new Thread(new ThreadStart(c3.count)); thread3.Name = "Thread 3"; thread3.Start(); }}

Page 55: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

class Counter{ private bool suspended; private Label output; private string threadName; private int currentCount=0;

public Counter(Label l) {output=l; suspended=false;} private delegate void DisplayDelegate(int count);

private void DisplayCount(int count) {output.Text=threadName+ ": " + count;} public void count() { threadName = Thread.CurrentThread.Name; while (true) { Thread.Sleep(300); lock(this) { while (suspended) {Monitor.Wait(this);} } currentCount++; output.Invoke(new DisplayDelegate(DisplayCount),

new object[] {currentCount}); } }

public void toggle() { suspended = !suspended; output.BackColor = suspended ? Color.Red : Color.LightCyan; lock (this) { if (!suspended) Monitor.Pulse(this); } }}

Page 56: Object Oriented Programming Multithreading Dr. Mike Spann m.spann@bham.ac.uk

Summary We have seen how we can create a multi threaded program using We have seen how we can create a multi threaded program using

the the ThreadThread class and the class and the ThreadStartThreadStart ()() delegate delegate We have seen how threads are scheduled and how threads can We have seen how threads are scheduled and how threads can

exist in different states and run with different priority levelsexist in different states and run with different priority levels We have looked at how we can synchronise threads so that they We have looked at how we can synchronise threads so that they

maintain consistency when accessing shared objectsmaintain consistency when accessing shared objects We have looked at the use of the We have looked at the use of the Control.Invoke()Control.Invoke() method for method for

calling methods in the calling methods in the UI UI thread enabling GUI components to be thread enabling GUI components to be updated in a multithreaded programupdated in a multithreaded program

We have only scratched the surface. For an excellent and thorough We have only scratched the surface. For an excellent and thorough description of threading see www.albahari.com/threading description of threading see www.albahari.com/threading