SlideShare a Scribd company logo
1 of 28
Multithreading in C# MedhatDawoud MedhatDawoud@gmail.com www.medhatdawoud.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 WithoutMultitasking
What is Multitasking ? So,  Whatis a Thread? And Howit 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
Thread (Static Members) Static Methods
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 TechnicalExample
Remember that  in our case (Multithreading ) the shared device is theCPU*  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 B Thread 2 lock object B then lock object A So, What will happen ?
Deadlock With An Example Thread 1 will lock objectA Thread 2 willlockobjectB 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 Carefulwhen  usingmultiple locks .
Summary To day we know : ,[object Object]
What’s Multithreading.
Problems face us and how to solve them.

More Related Content

What's hot

Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread Synchronization
Benj Del Mundo
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
Raghu nath
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Java
parag
 

What's hot (20)

Presentation1
Presentation1Presentation1
Presentation1
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
 
Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread Synchronization
 
Java exception
Java exception Java exception
Java exception
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
 
Java 8 date & time api
Java 8 date & time apiJava 8 date & time api
Java 8 date & time api
 
Jsp ppt
Jsp pptJsp ppt
Jsp ppt
 
Developing an ASP.NET Web Application
Developing an ASP.NET Web ApplicationDeveloping an ASP.NET Web Application
Developing an ASP.NET Web Application
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Threads in JAVA
Threads in JAVAThreads in JAVA
Threads in JAVA
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Multithreading In Java
Multithreading In JavaMultithreading In Java
Multithreading In Java
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
 
JavaFX Overview
JavaFX OverviewJavaFX Overview
JavaFX Overview
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Chapitre 6 traitement des exceptions
Chapitre 6  traitement des exceptionsChapitre 6  traitement des exceptions
Chapitre 6 traitement des exceptions
 
Threads And Synchronization in C#
Threads And Synchronization in C#Threads And Synchronization in C#
Threads And Synchronization in C#
 
Packages in java
Packages in javaPackages in java
Packages in java
 
jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...
jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...
jQuery Tutorial For Beginners | Developing User Interface (UI) Using jQuery |...
 

Viewers also liked

Threads c sharp
Threads c sharpThreads c sharp
Threads c sharp
Deivaa
 
Multi threading design pattern
Multi threading design patternMulti threading design pattern
Multi threading design pattern
Yan Wang
 
[Winform] Visual studio C# 2012 upload ftp
[Winform] Visual studio C# 2012 upload ftp[Winform] Visual studio C# 2012 upload ftp
[Winform] Visual studio C# 2012 upload ftp
Junifar hidayat
 

Viewers also liked (20)

Threading in c#
Threading in c#Threading in c#
Threading in c#
 
Threading
ThreadingThreading
Threading
 
multi threading
multi threadingmulti threading
multi threading
 
Threads c sharp
Threads c sharpThreads c sharp
Threads c sharp
 
C# Advanced L04-Threading
C# Advanced L04-ThreadingC# Advanced L04-Threading
C# Advanced L04-Threading
 
Intro To .Net Threads
Intro To .Net ThreadsIntro To .Net Threads
Intro To .Net Threads
 
Threads c sharp
Threads c sharpThreads c sharp
Threads c sharp
 
Thread
ThreadThread
Thread
 
Multi threading design pattern
Multi threading design patternMulti threading design pattern
Multi threading design pattern
 
Asynchronous Programming with C#
Asynchronous Programming with C#Asynchronous Programming with C#
Asynchronous Programming with C#
 
Delegates and events
Delegates and events   Delegates and events
Delegates and events
 
Real time web
Real time webReal time web
Real time web
 
Concurrency
ConcurrencyConcurrency
Concurrency
 
Triggers en SQL Server
Triggers en SQL ServerTriggers en SQL Server
Triggers en SQL Server
 
Backgroundworker
Backgroundworker Backgroundworker
Backgroundworker
 
[Winform] Visual studio C# 2012 upload ftp
[Winform] Visual studio C# 2012 upload ftp[Winform] Visual studio C# 2012 upload ftp
[Winform] Visual studio C# 2012 upload ftp
 
Generics C#
Generics C#Generics C#
Generics C#
 
[Winform] Visual studio C# 2012 Setting themes with telerik themes library
[Winform] Visual studio C# 2012 Setting themes with telerik themes library[Winform] Visual studio C# 2012 Setting themes with telerik themes library
[Winform] Visual studio C# 2012 Setting themes with telerik themes library
 
C# 5 deep drive into asynchronous programming
C# 5 deep drive into asynchronous programmingC# 5 deep drive into asynchronous programming
C# 5 deep drive into asynchronous programming
 
Multithreading Design Patterns
Multithreading Design PatternsMultithreading Design Patterns
Multithreading Design Patterns
 

Similar to Threading in C#

Similar to Threading in C# (20)

Java Threads
Java ThreadsJava Threads
Java Threads
 
C# interview
C# interviewC# interview
C# interview
 
C# interview-questions
C# interview-questionsC# interview-questions
C# interview-questions
 
25 java tough interview questions
25 java tough interview questions25 java tough interview questions
25 java tough interview questions
 
Lecture #2 threading, networking & permissions final version #2
Lecture #2  threading, networking & permissions final version #2Lecture #2  threading, networking & permissions final version #2
Lecture #2 threading, networking & permissions final version #2
 
1.17 Thread in java.pptx
1.17 Thread in java.pptx1.17 Thread in java.pptx
1.17 Thread in java.pptx
 
All of Javascript
All of JavascriptAll of Javascript
All of Javascript
 
java_threads.ppt
java_threads.pptjava_threads.ppt
java_threads.ppt
 
Java Class 6 | Java Class 6 |Threads in Java| Applets | Swing GUI | JDBC | Ac...
Java Class 6 | Java Class 6 |Threads in Java| Applets | Swing GUI | JDBC | Ac...Java Class 6 | Java Class 6 |Threads in Java| Applets | Swing GUI | JDBC | Ac...
Java Class 6 | Java Class 6 |Threads in Java| Applets | Swing GUI | JDBC | Ac...
 
Mario Fusco - Comparing different concurrency models on the JVM | Codemotion ...
Mario Fusco - Comparing different concurrency models on the JVM | Codemotion ...Mario Fusco - Comparing different concurrency models on the JVM | Codemotion ...
Mario Fusco - Comparing different concurrency models on the JVM | Codemotion ...
 
C# classes
C#   classesC#   classes
C# classes
 
Comparing different concurrency models on the JVM
Comparing different concurrency models on the JVMComparing different concurrency models on the JVM
Comparing different concurrency models on the JVM
 
Basic java part_ii
Basic java part_iiBasic java part_ii
Basic java part_ii
 
Multithreading and concurrency in android
Multithreading and concurrency in androidMultithreading and concurrency in android
Multithreading and concurrency in android
 
Jump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design PatternJump start to OOP, OOAD, and Design Pattern
Jump start to OOP, OOAD, and Design Pattern
 
Jump Start To Ooad And Design Patterns
Jump Start To Ooad And Design PatternsJump Start To Ooad And Design Patterns
Jump Start To Ooad And Design Patterns
 
All of javascript
All of javascriptAll of javascript
All of javascript
 
GoF Design patterns I: Introduction + Structural Patterns
GoF Design patterns I:   Introduction + Structural PatternsGoF Design patterns I:   Introduction + Structural Patterns
GoF Design patterns I: Introduction + Structural Patterns
 
Multithreading 101
Multithreading 101Multithreading 101
Multithreading 101
 
Unusual C# - OOP
Unusual C# - OOPUnusual C# - OOP
Unusual C# - OOP
 

More from Medhat Dawoud (11)

Using Git and BitBucket
Using Git and BitBucketUsing Git and BitBucket
Using Git and BitBucket
 
Introduction to linux
Introduction to linuxIntroduction to linux
Introduction to linux
 
Select your career
Select your careerSelect your career
Select your career
 
Intro to t sql – 3rd session
Intro to t sql – 3rd sessionIntro to t sql – 3rd session
Intro to t sql – 3rd session
 
Intro to T-SQL - 1st session
Intro to T-SQL - 1st sessionIntro to T-SQL - 1st session
Intro to T-SQL - 1st session
 
Intro to T-SQL – 2nd session
Intro to T-SQL – 2nd sessionIntro to T-SQL – 2nd session
Intro to T-SQL – 2nd session
 
Mesh cloud (road to mongoDB)
Mesh cloud (road to mongoDB)Mesh cloud (road to mongoDB)
Mesh cloud (road to mongoDB)
 
Before start
Before startBefore start
Before start
 
DevMix Startup
DevMix StartupDevMix Startup
DevMix Startup
 
How to python
How to pythonHow to python
How to python
 
Program threats
Program threatsProgram threats
Program threats
 

Recently uploaded

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
SoniaTolstoy
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
fonyou31
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 

Recently uploaded (20)

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 

Threading in C#

  • 1. Multithreading in C# MedhatDawoud MedhatDawoud@gmail.com www.medhatdawoud.com
  • 2. Agenda Introduction. What’s Multithreading ? Threads types ? Threading in C# ? Thread Class functionality. Problems and solutions .
  • 3. Introduction Do you like to be professional ? What is the target of this session ? A word by the history. Before multithreading.
  • 4. What is Multitasking ? Operating Systems do this for us How? (Stories to Clear)
  • 5. What is Multitasking ? A Little Demo for two Methods WithoutMultitasking
  • 6. What is Multitasking ? So, Whatis a Thread? And Howit works ?
  • 7. 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 .
  • 8. Multithreading in C# C# support Multitasking strongly. Multithreading == Multitasking. using System.Threading* . Using Thread class. * A built in namespace in .NET framework
  • 9. Thread Class Functionality This class has very important Static members * * Static members is that you call directly without objects
  • 10. Thread (Static Members) Static Properties
  • 11. Thread (Static Members) Static Methods
  • 12. Thread (Static Members) Show Demo* * A simple program Written in C# to Clear all ideas of Thread Class
  • 13. Problems that will face us Non-technical Example The Television between you and your sister 
  • 14. Problems that will face us A TechnicalExample
  • 15. Remember that in our case (Multithreading ) the shared device is theCPU* which cannot serve more than one request in one single moment. So, how it help us in making multitasking applications??? * Central Processing Unit (Processor).
  • 16. 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.
  • 17. Give a solution ?? Some one say : Stop sharing device It sounds very funny 
  • 18. The perfect solution is Synchronization The locking mechanism
  • 19. 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 .
  • 20. Another problem One of the problems that Threads cause is Deadlock* * It means that one thread might wait infinite period.
  • 21. Deadlock With An Example Thread 1 lock object A then lock object B Thread 2 lock object B then lock object A So, What will happen ?
  • 22. Deadlock With An Example Thread 1 will lock objectA Thread 2 willlockobjectB Then , thread1 waiting for object B to be released from thread2 that waiting for object A from thread1
  • 23. 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.
  • 24. 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 .
  • 25. So, what is the solution ? The only Way is To be very Carefulwhen usingmultiple locks .
  • 26.
  • 28. Problems face us and how to solve them.
  • 29.