Threading in C#


Published on

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

Published in: Education
1 Comment
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Threading in C#

  1. 1. Multithreading in C#<br />MedhatDawoud<br /><br /><br />
  2. 2. Agenda<br />Introduction.<br />What’s Multithreading ?<br />Threads types ?<br />Threading in C# ?<br />Thread Class functionality.<br />Problems and solutions .<br />
  3. 3. Introduction<br />Do you like to be professional ?<br />What is the target of this session ?<br />A word by the history.<br />Before multithreading.<br />
  4. 4. What is Multitasking ?<br />Operating Systems do this for us <br />How?<br />(Stories to Clear)<br />
  5. 5. What is Multitasking ?<br />A Little Demo for two Methods<br />WithoutMultitasking <br />
  6. 6. What is Multitasking ?<br />So, <br />Whatis a Thread?<br />And<br />Howit works ?<br />
  7. 7. Threads types<br />Foreground VS background .<br />The Threads is by default foreground .<br />But you can make it background by assigning a simple property (IsBackground) to true .<br />
  8. 8. Multithreading in C#<br />C# support Multitasking strongly.<br />Multithreading == Multitasking.<br />using System.Threading* .<br />Using Thread class.<br />* A built in namespace in .NET framework<br />
  9. 9. Thread Class Functionality<br />This class has very important <br />Static members *<br />* Static members is that you call directly without objects<br />
  10. 10. Thread (Static Members)<br />Static Properties<br />
  11. 11. Thread (Static Members)<br />Static Methods<br />
  12. 12. Thread (Static Members)<br />Show Demo*<br />* A simple program Written in C# to Clear all ideas of Thread Class<br />
  13. 13. Problems that will face us<br />Non-technical Example<br />The Television <br />between you and your sister  <br />
  14. 14. Problems that will face us<br />A TechnicalExample <br />
  15. 15. Remember that <br />in our case (Multithreading ) the shared device is theCPU*<br /> which cannot serve more than one request in one single moment.<br />So, how it help us in making multitasking applications???<br />* Central Processing Unit (Processor).<br />
  16. 16. Warning !!<br />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.<br />
  17. 17. Give a solution ??<br />Some one say :<br />Stop sharing device <br />It sounds very funny<br /><br />
  18. 18. The perfect solution is<br />Synchronization<br />The locking mechanism<br />
  19. 19. What is The locking mechanism?<br />The locking mechanism is how to lock the use of an object temporarily while one thread is using it.<br />Use the C# keyword Lock .<br />Simple Demo .<br />
  20. 20. Another problem <br />One of the problems that Threads cause is <br />Deadlock*<br />* It means that one thread might wait infinite period.<br />
  21. 21. Deadlock With An Example<br />Thread 1 lock object A then lock object B<br />Thread 2 lock object B then lock object A<br />So, What will happen ?<br />
  22. 22. Deadlock With An Example<br />Thread 1 will lock objectA<br />Thread 2 willlockobjectB<br />Then , thread1 waiting for object B to be released from thread2 that waiting for object A from thread1<br />
  23. 23. Deadlock With An Example<br />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.<br />
  24. 24. Deadlock ?<br />Also exist in database servers and operating systems.<br />This kind of bugs is Very difficult to be detected because it give no error message in runtime and has no exception handling .<br />Just the program is stop responding .<br />
  25. 25. So, what is the solution ?<br />The only Way is <br />To be very Carefulwhen <br />usingmultiple locks .<br />
  26. 26. Summary<br />To day we know :<br /><ul><li>What before Multithreading.
  27. 27. What’s Multithreading.
  28. 28. Problems face us and how to solve them.
  29. 29. Synchronization and locking mechanism.</li></li></ul><li>
  30. 30. Thanks<br />