{"id":929,"date":"2010-05-07T19:14:45","date_gmt":"2010-05-07T19:14:45","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=929"},"modified":"2010-05-08T13:43:04","modified_gmt":"2010-05-08T13:43:04","slug":"threading-running-different-parts-of-the-program-at-once","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2010\/05\/07\/threading-running-different-parts-of-the-program-at-once\/","title":{"rendered":"Threading &#8211; running different parts of the program at &#8220;once&#8221;"},"content":{"rendered":"<p>A thread is when you &#8220;spawn&#8221; off another process (a process is like another program running within itself). <\/p>\n<p>The operating system gives the impression that it is running allot of programs at the same time, but what is happening that allot of processes (programs) have access to the CPU for a limited amount of time, e.g. 10 milliseconds, and then leave the CPU execution stage whilst another process will enter there CPU execution stage (this is the part where the program is actually doing something).<\/p>\n<p>To start with lets first create a method that is run within new thread, here we are just going to output a message and sleep for a bit, loop this over for 10 times.<\/p>\n<pre lang=\"csharp\">\r\n\/\/ the method to create as a threadable method\r\npublic static void RunThisMethod()\r\n{\r\n\tfor (int i =0; i < 10; i++)\r\n\t{\r\n\t\tConsole.WriteLine(\"RunThisMethod number : \"+i.ToString() + \" Sleep for 45\");\r\n\t\tThread.Sleep(45);\r\n\t}\r\n}\r\n<\/pre>\n<p>Now lets create a thread, since a Thread has to be a <a href=\"http:\/\/www.codingfriends.com\/index.php\/2010\/01\/18\/delegates-csharp\/\">delegated<\/a> method but with .net 2 and above the .net environment will pick the correct delegate for you, so this means that you can just pass in the method name to the Thread class and that is it<\/p>\n<pre lang=\"csharp\">\r\n\tThread theThread = new Thread(RunThisMethod);\r\n<\/pre>\n<p>To start the new Thread, it is as easy as start<\/p>\n<pre lang=\"csharp\">\r\n\ttheThread.Start();\r\n<\/pre>\n<p>Here is the full source code, for demoing how the main and the runMyMethod flip between each other (e.g. each process has time in the processor(s) to run)<\/p>\n<pre lang=\"csharp\">\r\nusing System;\r\nusing System.Threading;\r\n\r\nnamespace myThread\r\n{\r\n\tclass MainClass\r\n\t{\r\n\t\tpublic static void Main (string[] args)\r\n\t\t{\r\n\t\t\t\/\/ initialize the RunThisMethod as a thread\r\n\t\t\tThread theThread = new Thread(RunThisMethod);\r\n\t\t\ttheThread.Start();\r\n\t\t\t\r\n\t\t\tfor (int j = 0; j < 5; j++)\r\n\t\t\t{\r\n\t\t\t\tConsole.WriteLine(\"Main method number : \"+j.ToString()+\" Sleep for 100\");\r\n\t\t\t\tThread.Sleep(100);\r\n\t\t\t}\r\n\t\t}\r\n\t\t\r\n\t\t\/\/ the method to create as a threadable method\r\n\t\tpublic static void RunThisMethod()\r\n\t\t{\r\n\t\t\tfor (int i =0; i < 10; i++)\r\n\t\t\t{\r\n\t\t\t\tConsole.WriteLine(\"RunThisMethod number : \"+i.ToString() + \" Sleep for 45\");\r\n\t\t\t\tThread.Sleep(45);\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>and here would be the output<\/p>\n<pre lang=\"bash\">\r\nMain method number : 0 Sleep for 100\r\nRunThisMethod number : 0 Sleep for 45\r\nRunThisMethod number : 1 Sleep for 45\r\nRunThisMethod number : 2 Sleep for 45\r\nMain method number : 1 Sleep for 100\r\nRunThisMethod number : 3 Sleep for 45\r\nRunThisMethod number : 4 Sleep for 45\r\nMain method number : 2 Sleep for 100\r\nRunThisMethod number : 5 Sleep for 45\r\nRunThisMethod number : 6 Sleep for 45\r\nMain method number : 3 Sleep for 100\r\nRunThisMethod number : 7 Sleep for 45\r\nRunThisMethod number : 8 Sleep for 45\r\nMain method number : 4 Sleep for 100\r\nRunThisMethod number : 9 Sleep for 45<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A thread is when you &#8220;spawn&#8221; off another process (a process is like another program running within itself). The operating system gives the impression that it is running allot of programs at the same time, but what is happening that allot of processes (programs) have access to the CPU for a limited amount of time, &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2010\/05\/07\/threading-running-different-parts-of-the-program-at-once\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Threading &#8211; running different parts of the program at &#8220;once&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[200,199,202,201],"class_list":["post-929","post","type-post","status-publish","format-standard","hentry","category-c_sharp","tag-start","tag-thread","tag-thread-sleep","tag-thread-start"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/929","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=929"}],"version-history":[{"count":3,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/929\/revisions"}],"predecessor-version":[{"id":934,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/929\/revisions\/934"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=929"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=929"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=929"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}