Threading in C#

28
Multithreading in C# Medhat Dawoud www.medhatdawoud.com [email protected]

description

this presentation was shown in 25/12/2010 at perfect training center

Transcript of Threading in C#

Page 1: Threading in C#

Multithreading in C#

Medhat Dawoud

[email protected]

Page 2: Threading in C#

Agenda

• Introduction.• What’s Multithreading ?• Threads types ?• Threading in C# ?• Thread Class functionality.• Problems and solutions .

Page 3: Threading in C#

Introduction

• Do you like to be professional ?• What is the target of this session ?• A word by the history.• Before multithreading.

Page 4: Threading in C#

What is Multitasking ?

Operating Systems do this for us

How ?(Stories to Clear)

Page 5: Threading in C#

What is Multitasking ?

A Little Demo for two Methods Without Multitasking

Page 6: Threading in C#

What is Multitasking ?

So, What is a Thread ?

And

How it works ?

Page 7: Threading in C#

Threads types

• Foreground VS background .• The Threads is by default foreground .• But you can make it background by

assigning a simple property (IsBackground) to true .

Page 8: Threading in C#

Multithreading in C#

• C# support Multitasking strongly.• Multithreading == Multitasking.• using System.Threading* .• Using Thread class.

* A built in namespace in .NET framework

Page 9: Threading in C#

Thread Class Functionality

This class has very important

Static members *

* Static members is that you call directly without objects

Page 10: Threading in C#

Thread (Static Members)

Static Properties• Name• Priorty• CurrentThread

• IsAlive• ThreadState• IsBackground

Page 11: Threading in C#

Thread (Static Members)

Static Methods• Sleep()• Start()• Abort()

• Suspend()• Resume()• Join()

Page 12: Threading in C#

Thread (Static Members)

Show Demo*

* A simple program Written in C# to Clear all ideas of Thread Class

Page 13: Threading in C#

Problems that will face us

Non-technical Example

The Television between you and your sister

Page 14: Threading in C#

Problems that will face us

A Technical Example

Page 15: Threading in C#

Remember that

in our case (Multithreading ) the shared device is the

CPU* which cannot serve more than one request in

one single moment.So, how it help us in making multitasking

applications???

* Central Processing Unit (Processor).

Page 16: Threading in C#

Warning !!

Don’t use Multiple threads except it is required not to make the CPU busy with

transitions between threads instead of serving the instructions of the threads itself.

Page 17: Threading in C#

Give a solution ??

Some one say :

Stop sharing device

It sounds very funny

Page 18: Threading in C#

The perfect solution is

Synchronization

The locking mechanism

Page 19: Threading in C#

What is The locking mechanism?

• The locking mechanism is how to lock the use of an object temporarily while one thread is using it.

• Use the C# keyword Lock .• Simple Demo .

Page 20: Threading in C#

Another problem

• One of the problems that Threads cause is

Deadlock*

* It means that one thread might wait infinite period.

Page 21: Threading in C#

Deadlock With An Example

Thread 1 lock object A then lock object BThread 2 lock object B then lock object A

So, What will happen ?

Page 22: Threading in C#

Deadlock With An Example

Thread 1 will lock object A Thread 2 will lock object B

Then , thread1 waiting for object B to be released from thread2 that waiting for object A from thread1

Page 23: Threading in C#

Deadlock With An Example

So, the program will deadlock at this fragment of code because non of both threads will leave the

object until it got the other and end working.

Page 24: Threading in C#

Deadlock ?

• Also exist in database servers and operating systems.

• This kind of bugs is Very difficult to be detected because it give no error message in runtime and has no exception handling .

• Just the program is stop responding .

Page 25: Threading in C#

So, what is the solution ?

The only Way is

To be very Careful when

using multiple locks .

Page 26: Threading in C#

Summary• To day we know :• What before Multithreading.• What’s Multithreading.• Problems face us and how to solve them.• Synchronization and locking mechanism.

Page 27: Threading in C#
Page 28: Threading in C#

Thanks