Threads and C#
authorgu.martinm@gmail.com <gu.martinm@gmail.com>
Sun, 25 May 2014 22:40:13 +0000 (00:40 +0200)
committergu.martinm@gmail.com <gu.martinm@gmail.com>
Sun, 25 May 2014 22:40:13 +0000 (00:40 +0200)
Chapter 1: ThreadStart and ParameterizedThreadStart

Allgemeines/Threads/Threads/Chapter1.cs [new file with mode: 0644]
Allgemeines/Threads/Threads/Program.cs
Allgemeines/Threads/Threads/Threads.csproj

diff --git a/Allgemeines/Threads/Threads/Chapter1.cs b/Allgemeines/Threads/Threads/Chapter1.cs
new file mode 100644 (file)
index 0000000..89b7ff9
--- /dev/null
@@ -0,0 +1,115 @@
+using System;
+using System.Threading;
+
+namespace Threads
+{
+    /// <summary>
+    /// 
+    /// 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
+    /// 
+    /// </summary>
+    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);
+            }
+        }
+    }
+}
+
index f9fb092..05aa71b 100644 (file)
@@ -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<int>();
+            // 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);
 
index cd8f300..64ed1a4 100644 (file)
@@ -44,6 +44,7 @@ See: http://parallelpatterns.codeplex.com/</Description>
     <Compile Include="Chapter4.cs" />
     <Compile Include="Chapter5.cs" />
     <Compile Include="Chapter6.cs" />
+    <Compile Include="Chapter1.cs" />
   </ItemGroup>
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
 </Project>
\ No newline at end of file