From f9ede9bc647181a3cdf3fdd90f8cd3ce4da6f3eb Mon Sep 17 00:00:00 2001 From: "gu.martinm@gmail.com" Date: Mon, 26 May 2014 00:40:13 +0200 Subject: [PATCH] Threads and C# Chapter 1: ThreadStart and ParameterizedThreadStart --- Allgemeines/Threads/Threads/Chapter1.cs | 115 +++++++++++++++++++++++++++++ Allgemeines/Threads/Threads/Program.cs | 3 + Allgemeines/Threads/Threads/Threads.csproj | 1 + 3 files changed, 119 insertions(+) create mode 100644 Allgemeines/Threads/Threads/Chapter1.cs diff --git a/Allgemeines/Threads/Threads/Chapter1.cs b/Allgemeines/Threads/Threads/Chapter1.cs new file mode 100644 index 0000000..89b7ff9 --- /dev/null +++ b/Allgemeines/Threads/Threads/Chapter1.cs @@ -0,0 +1,115 @@ +using System; +using System.Threading; + +namespace Threads +{ + /// + /// + /// Chapter 1. + /// Simple Threads + /// + /// Taken from http://msdn.microsoft.com/en-us/library/system.threading.threadstart.aspx + /// Taken from http://msdn.microsoft.com/en-us/library/6x4c42hc%28v=vs.110%29.aspx + /// + /// + public class Chapter1 + { + public static void Test() + { + Console.WriteLine("Chapter 1"); + + + Console.WriteLine("ThreadStart"); + // To start a thread using a static thread procedure, use the + // class name and method name when you create the ThreadStart + // delegate. Beginning in version 2.0 of the .NET Framework, + // it is not necessary to create a delegate explicitly. + // Specify the name of the method in the Thread constructor, + // and the compiler selects the correct delegate. For example: + // + // Thread newThread = new Thread(Work.DoWork); + // + WorkWithThreadStart workWith = new WorkWithThreadStart(); + ThreadStart threadDelegate = new ThreadStart(workWith.DoWork); + Thread newThread = new Thread(threadDelegate); + newThread.Start(); + Thread.Sleep(3000); + // To start a thread using an instance method for the thread + // procedure, use the instance variable and method name when + // you create the ThreadStart delegate. Beginning in version + // 2.0 of the .NET Framework, the explicit delegate is not + // required. + // + workWith = new WorkWithThreadStart(); + workWith.Data = 42; + threadDelegate = new ThreadStart(workWith.DoMoreWork); + newThread = new Thread(threadDelegate); + newThread.Start(); + Thread.Sleep(3000); + + + Console.WriteLine("ParameterizedThreadStart"); + WorkWithParameterizedThreadStart workWithout = new WorkWithParameterizedThreadStart(); + // To start a thread using a shared thread procedure, use + // the class name and method name when you create the + // ParameterizedThreadStart delegate. C# infers the + // appropriate delegate creation syntax: new ParameterizedThreadStart(Work.DoWork) + // + // ParameterizedThreadStart parameterizedThreadDelegate = new ThreadStart(workWithout.DoWork); + // newThread = new Thread(parameterizedThreadDelegate); + newThread = new Thread(workWithout.DoWork); + + // Use the overload of the Start method that has a + // parameter of type Object. You can create an object that + // contains several pieces of data, or you can pass any + // reference type or value type. The following code passes + // the integer value 42. + // + newThread.Start(42); + Thread.Sleep(3000); + + // To start a thread using an instance method for the thread + // procedure, use the instance variable and method name when + // you create the ParameterizedThreadStart delegate. C# infers + // the appropriate delegate creation syntax: + // new ParameterizedThreadStart(w.DoMoreWork) + // + workWithout = new WorkWithParameterizedThreadStart(); + newThread = new Thread(workWithout.DoMoreWork); + + // Pass an object containing data for the thread. + // + newThread.Start("The answer."); + Thread.Sleep(3000); + } + + private class WorkWithThreadStart + { + public void DoWork() + { + Console.WriteLine("Static thread procedure."); + } + + public int Data; + + public void DoMoreWork() + { + Console.WriteLine("Instance thread procedure. Data={0}", Data); + } + } + + private class WorkWithParameterizedThreadStart + { + public void DoWork(object data) + { + Console.WriteLine("Static thread procedure. Data='{0}'", data); + } + + public void DoMoreWork(object data) + { + Console.WriteLine("Instance thread procedure. Data='{0}'", data); + } + } + } +} + diff --git a/Allgemeines/Threads/Threads/Program.cs b/Allgemeines/Threads/Threads/Program.cs index f9fb092..05aa71b 100644 --- a/Allgemeines/Threads/Threads/Program.cs +++ b/Allgemeines/Threads/Threads/Program.cs @@ -18,12 +18,15 @@ namespace Threads { public static void Main(string[] args) { + Chapter1.Test(); Chapter2.Test(); Chapter3.Test(); Chapter4.Test(); Chapter5.Test(); Chapter6 chapter6 = new Chapter6(); var tree = new Threads.Chapter6.Tree(); + // WITH 10 LEVELS MONO STOPS WORKING WITH 806 THREADS!!!! (in my machine with 6GB RAM) + // DUNNO WHAT MAKES THE MONO VIRTUAL MACHINE STOP... :( var levels = 10; chapter6.FillTree(levels, tree, () => 40); diff --git a/Allgemeines/Threads/Threads/Threads.csproj b/Allgemeines/Threads/Threads/Threads.csproj index cd8f300..64ed1a4 100644 --- a/Allgemeines/Threads/Threads/Threads.csproj +++ b/Allgemeines/Threads/Threads/Threads.csproj @@ -44,6 +44,7 @@ See: http://parallelpatterns.codeplex.com/ + \ No newline at end of file -- 2.1.4