Threading in C#

Post on 19-May-2015

3,656 views 8 download

Tags:

description

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

transcript

Multithreading in C#

Medhat Dawoud

www.medhatdawoud.comMedhatDawoud@gmail.com

Agenda

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

Introduction

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

What is Multitasking ?

Operating Systems do this for us

How ?(Stories to Clear)

What is Multitasking ?

A Little Demo for two Methods Without Multitasking

What is Multitasking ?

So, What is a Thread ?

And

How it works ?

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 .

Multithreading in C#

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

* A built in namespace in .NET framework

Thread Class Functionality

This class has very important

Static members *

* Static members is that you call directly without objects

Thread (Static Members)

Static Properties• Name• Priorty• CurrentThread

• IsAlive• ThreadState• IsBackground

Thread (Static Members)

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

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

Thread (Static Members)

Show Demo*

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

Problems that will face us

Non-technical Example

The Television between you and your sister

Problems that will face us

A Technical Example

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).

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.

Give a solution ??

Some one say :

Stop sharing device

It sounds very funny

The perfect solution is

Synchronization

The locking mechanism

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 .

Another problem

• One of the problems that Threads cause is

Deadlock*

* It means that one thread might wait infinite period.

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 ?

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

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.

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 .

So, what is the solution ?

The only Way is

To be very Careful when

using multiple locks .

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

Thanks