{"id":414,"date":"2010-06-29T18:44:32","date_gmt":"2010-06-29T13:14:32","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=414"},"modified":"2010-06-29T18:44:32","modified_gmt":"2010-06-29T13:14:32","slug":"background-and-foreground-thread-in-c","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/microsoft\/csharp\/background-and-foreground-thread-in-c\/","title":{"rendered":"Background and foreground thread in c#"},"content":{"rendered":"<li><em><strong>Foreground threads<\/strong><\/em> have the ability to prevent the current application from terminating. The CLR will not shut down an application (which is to say, unload the hosting AppDomain) until all foreground threads have ended.<\/li>\n<li><em><strong>Background threads<\/strong><\/em> (sometimes called\u00a0<em>daemon threads<\/em>) are viewed by the CLR as expendable paths of execution that can be ignored at any point in time (even if they are currently laboring over some unit of work). Thus, if all foreground threads have terminated, any and all background threads are automatically killed when the application domain unloads.<\/li>\n<p>It is important to note that foreground and background threads are\u00a0<em>not<\/em> synonymous with primary and worker threads. By default, every thread you create via the\u00a0<code><strong>Thread.Start()<\/strong><\/code> method is automatically a\u00a0<em>foreground<\/em> thread. Again, this means that the AppDomain will not unload until all threads of execution have completed their units of work. In most cases, this is exactly the behavior you require.<\/p>\n<p>Example:<\/p>\n<figure id=\"attachment_415\" aria-describedby=\"caption-attachment-415\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/06\/ThreadDemo.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-415\" title=\"Threading in CSharp\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/06\/ThreadDemo-300x93.png?resize=300%2C93&#038;ssl=1\" alt=\"Thread Foreground Background\" width=\"300\" height=\"93\" \/><\/a><figcaption id=\"caption-attachment-415\" class=\"wp-caption-text\">Threading in CSharp<\/figcaption><\/figure>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n\nusing System;\nusing System.Collections.Generic;\nusing System.Text;\nusing System.Threading;\nnamespace TestThread\n{\nclass Program\n{\nstatic void Main(string&#x5B;] args)\n{\nProgram obj = new Program();\nobj.printInfo();\n\/\/Create object of parameterized thread, so that you can pass parameter to any thread\nParameterizedThreadStart pst = new ParameterizedThreadStart(obj.function1);\n\/\/Bydefault threads are Foreground Thread\nThread t1 = new Thread(pst);\n\/\/Set Thread Name\nt1.Name = &quot;Thread1&quot;;\n\/\/Passs Parameter to thread\nt1.Start(true);\n\/\/Create object of ThreadStart, it does not have any parameter\nThreadStart ts = new ThreadStart(obj.function2);\nThread t2 = new Thread(ts);\nt2.Name = &quot;Thread2&quot;;\n\/\/Make it Background Thread\nt2.IsBackground = true;\n\/\/Run the thread\nt2.Start();\nConsole.ReadKey();\n}\npublic void function1(object val)\n{\nfor (int i = 0; i &lt; 5; i++)\n{\nConsole.WriteLine(&quot;This is parameterized function1 by {0} and Value passed is {1} &quot;, Thread.CurrentThread.Name, val);\n}\n}\npublic void function2()\n{\nfor (int i = 0; i &lt; 5; i++)\n{\nConsole.WriteLine(&quot;This is function2 by : &quot; + Thread.CurrentThread.Name);\n}\n}\npublic void printInfo()\n{\nSystem.Text.StringBuilder sb = new StringBuilder();\nsb.Append(&quot;*******************************************************n&quot;);\nsb.Append(&quot;            read Demo by ShivaSoft                     n&quot;);\nsb.Append(&quot;*******************************************************n&quot;);\nSystem.Console.WriteLine(sb);\n}\n}\n}\n\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Example of Threading in C#. Includes Foreground and Background Threading.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[22],"tags":[198],"class_list":["post-414","post","type-post","status-publish","format-standard","hentry","category-csharp","tag-thread"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":1816,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/daemon-thread-java\/","url_meta":{"origin":414,"position":0},"title":"Daemon Thread","author":"Jitendra","date":"March 24, 2011","format":false,"excerpt":"Example and Explanation of Daemon thread in JAVA","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":757,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/anonymous-classimplements-interface-methods-of-threads\/","url_meta":{"origin":414,"position":1},"title":"Create Thread using Anonymous class and Interface","author":"Jitendra","date":"August 16, 2010","format":false,"excerpt":"Demonstration of creating Thread by Anonymous class and Interface","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":724,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/simple-threading-techniques\/","url_meta":{"origin":414,"position":2},"title":"Thread, Life Cycle of Thread","author":"Jitendra","date":"August 7, 2010","format":false,"excerpt":"What is Thread? Life cycle of thread. Difference in start() and run() method of Thread. By Default how many thread runs?","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":748,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/producer-consumer-problem\/","url_meta":{"origin":414,"position":3},"title":"Thread Synchronization","author":"Jitendra","date":"August 7, 2010","format":false,"excerpt":"Describes Synchronization, Producer and Consumer Problem, wait(), notify(), InterThread Communication","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1825,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/java-thread-timertask\/","url_meta":{"origin":414,"position":4},"title":"Java Thread &#8211; Executor framework, Timer and TimerTask","author":"Jitendra","date":"March 24, 2011","format":false,"excerpt":"Tutorial and example of Executor framework, Timer and TimerTask over Thread","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"Animation of ProgressBar in Swing Using TimerTask","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/03\/Animation-of-ProgressBar-Swing-Using-TimerTask.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":1430,"url":"https:\/\/www.jitendrazaa.com\/blog\/others\/tips\/cross-thread-operation-not-valid-error-solved-in-c\/","url_meta":{"origin":414,"position":5},"title":"Cross-thread operation not valid &#8211; error solved in C#","author":"Jitendra","date":"December 25, 2010","format":false,"excerpt":"Cross-thread operation not valid - error solved in C#","rel":"","context":"In &quot;c#&quot;","block_context":{"text":"c#","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/microsoft\/csharp\/"},"img":{"alt_text":"ThreadCommunication","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/12\/ThreadCommunication.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/414","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=414"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/414\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=414"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=414"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=414"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}