楼主: Lisrelchen
823 6

Multithreading in C# 5.0 Cookbook [推广有奖]

  • 0关注
  • 62粉丝

VIP

已卖:4194份资源

院士

67%

还不是VIP/贵宾

-

TA的文库  其他...

Bayesian NewOccidental

Spatial Data Analysis

东西方数据挖掘

威望
0
论坛币
50288 个
通用积分
83.6906
学术水平
253 点
热心指数
300 点
信用等级
208 点
经验
41518 点
帖子
3256
精华
14
在线时间
766 小时
注册时间
2006-5-4
最后登录
2022-11-6

楼主
Lisrelchen 发表于 2017-1-29 11:44:15 |AI写论文
1论坛币
  1. Multithreaded programming can seem overwhelming but this book clarifies everything through its cookbook approach. Packed with practical tasks, it's the quick and easy way to start delving deep into the power of multithreading in C#.

  2. Overview

  3. Delve deep into the .NET threading infrastructure and use Task Parallel Library for asynchronous programming
  4. Scale out your server applications effectively
  5. Successfully program Windows 8 and Windows Azure asynchronous applications
  6. In Detail

  7. In an age when computer processors are being developed to contain more and more cores, multithreading is a key factor for creating scalable, effective, and responsive applications. If you fail to do it correctly, it can lead to puzzling problems that take a huge amount of time to resolve. Therefore, having a solid understanding of multithreading is a must for the modern application developer.

  8. Multithreading in C# 5.0 Cookbook is an easy-to-understand guide to the most puzzling programming problems. This book will guide you through practical examples dedicated to various aspects of multithreading in C# on Windows and will give you a good basis of practical knowledge which you can then use to program your own scalable and reliable multithreaded applications.

  9. This book guides you through asynchronous and parallel programming from basic examples to practical, real-world solutions to complex problems. You will start from the very beginning, learning what a thread is, and then proceed to learn new concepts based on the information you get from the previous examples.

  10. After describing the basics of threading, you will be able to grasp more advanced concepts like Task Parallel Library and C# asynchronous functions. Then, we move towards parallel programming, starting with basic data structures and gradually progressing to the more advanced patterns. The book concludes with a discussion of the specifics of Windows 8 application programming, giving you a complete understanding of how Windows 8 applications are different and how to program asynchronous applications for Windows 8.

  11. What you will learn from this book

  12. Work with raw threads, synchronize threads, and coordinate their work
  13. Develop your own asynchronous API with Task Parallel Library
  14. Use C# 5.0 asynchronous language features
  15. Scale up your server application with I/O threads
  16. Parallelize your LINQ queries with PLINQ
  17. Use common concurrent collections
  18. Apply different parallel programming patterns
  19. Work with Windows 8 and Windows Azure asynchronous APIs
  20. Use Reactive Extensions to run asynchronous operations and manage their options
复制代码



关键词:everything practical Windows through server

沙发
Lisrelchen 发表于 2017-1-29 11:46:55

Creating a thread in C#

  1. In the Program.cs file add the following using directives:using System;
  2. using System.Threading;
  3. Add the following code snippet below the Main method:static void PrintNumbers()
  4. {
  5.   Console.WriteLine("Starting...");
  6.   for (int i = 1; i < 10; i++)
  7.   {
  8.     Console.WriteLine(i);
  9.   }
  10. }
  11. Add the following code snippet inside the Main method:Thread t = new Thread(PrintNumbers);
  12. t.Start();
  13. PrintNumbers();
  14. Run the program. The output will be something like:
复制代码


藤椅
Lisrelchen 发表于 2017-1-29 11:48:02

Pausing a thread

  1. Pausing a thread

  2. This recipe will show you how to make a thread wait for some time without wasting operating system resources.

  3. Getting ready
  4. To work through this recipe, you will need Visual Studio 2012. There are no other prerequisites. The source code for this recipe can be found at BookSamples\Chapter1\Recipe2.

  5. How to do it...
  6. To understand how to make a thread wait without wasting operating system resource, perform the following steps:

  7. Start Visual Studio 2012. Create a new C# Console Application project.
  8. In the Program.cs file add the following using directives:
  9. using System;
  10. using System.Threading;
  11. Add the following code snippet below the Main method:
  12. static void PrintNumbers()
  13. {
  14.   Console.WriteLine("Starting...");
  15.   for (int i = 1; i < 10; i++)
  16.   {
  17.     Console.WriteLine(i);
  18.   }
  19. }
  20. static void PrintNumbersWithDelay()
  21. {
  22.   Console.WriteLine("Starting...");
  23.   for (int i = 1; i < 10; i++)
  24.   {
  25.     Thread.Sleep(TimeSpan.FromSeconds(2));
  26.     Console.WriteLine(i);
  27.   }
  28. }
  29. Add the following code snippet inside the Main method:
  30. Thread t = new Thread(PrintNumbersWithDelay);
  31. t.Start();
  32. PrintNumbers();
  33. Run the program.
复制代码

板凳
Lisrelchen 发表于 2017-1-29 11:51:04
Making a thread wait
  1. Making a thread wait

  2. This recipe will show you how a program can wait for some computation in another thread to complete to use its result later in the code. It is not enough to use Thread.Sleep because we don't know the exact time the computation will take.

  3. Getting ready

  4. To work through this recipe, you will need Visual Studio 2012. There are no other prerequisites. The source code for this recipe can be found at BookSamples\Chapter1\Recipe3.

  5. How to do it...

  6. To understand how a program can wait for some computation in another thread to complete to use its result later, perform the following steps:

  7. Start Visual Studio 2012. Create a new C# Console Application project.
  8. In the Program.cs file, add the following using directives:
  9. using System;
  10. using System.Threading;
  11. Add the following code snippet below the Main method:
  12. static void PrintNumbersWithDelay()
  13. {
  14.   Console.WriteLine("Starting...");
  15.   for (int i = 1; i < 10; i++)
  16.   {
  17.     Thread.Sleep(TimeSpan.FromSeconds(2));
  18.     Console.WriteLine(i);
  19.   }
  20. }
  21. Add the following code snippet inside the Main method:
  22. Console.WriteLine("Starting...");
  23. Thread t = new Thread(PrintNumbersWithDelay);
  24. t.Start();
  25. t.Join();
  26. Console.WriteLine("Thread completed");
  27. Run the program.
复制代码

报纸
Lisrelchen 发表于 2017-1-29 11:52:23
Aborting a thread
  1. Aborting a thread

  2. In this recipe, we will describe how to abort another thread's execution.

  3. Getting ready

  4. To work through this recipe, you will need Visual Studio 2012. There are no other prerequisites. The source code for this recipe can be found at BookSamples\Chapter1\Recipe4.

  5. How to do it...

  6. To understand how to abort another thread's execution, perform the following steps:

  7. Start Visual Studio 2012. Create a new C# Console Application project.
  8. In the Program.cs file, add the following using directives:
  9. using System;
  10. using System.Threading;
  11. Add the following code snippet below the Main method:
  12. static void PrintNumbersWithDelay()
  13. {
  14.   Console.WriteLine("Starting...");
  15.   for (int i = 1; i < 10; i++)
  16.   {
  17.     Thread.Sleep(TimeSpan.FromSeconds(2));
  18.     Console.WriteLine(i);
  19.   }
  20. }
  21. Add the following code snippet inside the Main method:
  22. Console.WriteLine("Starting program...");
  23. Thread t = new Thread(PrintNumbersWithDelay);
  24. t.Start();
  25. Thread.Sleep(TimeSpan.FromSeconds(6));
  26. t.Abort();
  27. Console.WriteLine("A thread has been aborted");
  28. Thread t = new Thread(PrintNumbers);
  29. t.Start();
  30. PrintNumbers();
  31. Run the program.
复制代码

地板
Lisrelchen 发表于 2017-1-29 11:54:08
Determining a thread state
  1. Determining a thread state

  2. This recipe will describe possible states a thread could have. It is useful to get information about whether a thread is started yet or whether it is in a blocked state. Please note that because a thread runs independently, its state could be changed at any time.

  3. Getting ready

  4. To work through this recipe, you will need Visual Studio 2012. There are no other prerequisites. The source code for this recipe can be found at BookSamples\Chapter1\Recipe5.

  5. How to do it...

  6. To understand how to determine a thread state and acquire useful information about it, perform the following steps:

  7. Start Visual Studio 2012. Create a new C# Console Application project.
  8. In the Program.cs file, add the following using directives:
  9. using System;
  10. using System.Threading;
  11. Add the following code snippet below the Main method:
  12. static void DoNothing()
  13. {
  14.   Thread.Sleep(TimeSpan.FromSeconds(2));
  15. }

  16. static void PrintNumbersWithStatus()
  17. {
  18.   Console.WriteLine("Starting...");
  19.   Console.WriteLine(Thread.CurrentThread
  20.   .ThreadState.ToString());
  21.   for (int i = 1; i < 10; i++)
  22.   {
  23.     Thread.Sleep(TimeSpan.FromSeconds(2));
  24.     Console.WriteLine(i);
  25.   }
  26. }
  27. Add the following code snippet inside the Main method:
  28. Console.WriteLine("Starting program...");
  29. Thread t = new Thread(PrintNumbersWithStatus);
  30. Thread t2 = new Thread(DoNothing);
  31. Console.WriteLine(t.ThreadState.ToString());
  32. t2.Start();
  33. t.Start();
  34. for (int i = 1; i < 30; i++)
  35. {
  36.   Console.WriteLine(t.ThreadState.ToString());
  37. }
  38. Thread.Sleep(TimeSpan.FromSeconds(6));
  39. t.Abort();
  40. Console.WriteLine("A thread has been aborted");
  41. Console.WriteLine(t.ThreadState.ToString());
  42. Console.WriteLine(t2.ThreadState.ToString());
  43. Run the program.
复制代码

7
Lisrelchen 发表于 2017-1-29 11:54:58
Thread priority
  1. Thread priority

  2. This recipe will describe the different possible options for thread priority. Setting a thread priority determines how much CPU time a thread will be given.

  3. Getting ready

  4. To work through this recipe, you will need Visual Studio 2012. There are no other prerequisites. The source code for this recipe can be found at BookSamples\Chapter1\Recipe6.

  5. How to do it...

  6. To understand the workings of thread priority, perform the following steps:

  7. Start Visual Studio 2012. Create a new C# Console Application project.
  8. In the Program.cs file, add the following using directives:
  9. using System;
  10. using System.Diagnostics;
  11. using System.Threading;
  12. Add the following code snippet below the Main method:
  13. static void RunThreads()
  14. {
  15.   var sample = new ThreadSample();

  16.   var threadOne = new Thread(sample.CountNumbers);
  17.   threadOne.Name = "ThreadOne";
  18.   var threadTwo = new Thread(sample.CountNumbers);
  19.   threadTwo.Name = "ThreadTwo";

  20.   threadOne.Priority = ThreadPriority.Highest;
  21.   threadTwo.Priority = ThreadPriority.Lowest;
  22.   threadOne.Start();
  23.   threadTwo.Start();

  24.   Thread.Sleep(TimeSpan.FromSeconds(2));
  25.   sample.Stop();
  26. }
  27. class ThreadSample
  28. {
  29.   private bool _isStopped = false;
  30.   public void Stop()
  31.   {
  32.     _isStopped = true;
  33.   }

  34.   public void CountNumbers()
  35.   {
  36.     long counter = 0;

  37.     while (!_isStopped)
  38.     {
  39.       counter++;
  40.     }

  41.     Console.WriteLine("{0} with {1,11} priority " +"has a count = {2,13}", Thread.CurrentThread.Name, Thread.CurrentThread.Priority,counter.ToString("N0"));
  42.   }
  43. }
  44. Add the following code snippet inside the Main method:
  45. Console.WriteLine("Current thread priority: {0}", Thread.CurrentThread.Priority);
  46. Console.WriteLine("Running on all cores available");
  47. RunThreads();
  48. Thread.Sleep(TimeSpan.FromSeconds(2));
  49. Console.WriteLine("Running on a single core");
  50. Process.GetCurrentProcess().ProcessorAffinity = new IntPtr(1);
  51. RunThreads();
  52. Run the program.
复制代码

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2026-1-22 20:33