Skip to main content

Multithreading C# : Part 02

Using a Background threads

Foreground thread :
You can use foreground threads to keep the application alive. When non of foreground threads are running, CLR (common language runtime) will shut down your application.  Background threads are then terminated.









using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
 
namespace Multithreading
{
    public static class BackgroundThread
    {
        // Using a background thread 
 
        //Thread method
        public static void ExampleThreadMethod()
        {
            for (int x = 0; x < 10; x++)
            {
                Console.WriteLine("ExampleThreadMethod: {0}", x);
 
                //Thread.Sleep(1000)? 
                //Sleep one second
                Thread.Sleep(1000);
            }
        }
 
        //Main method
        public static void Main(string[] args)
        {
            Thread thread = new Thread(new ThreadStart(ExampleThreadMethod));
            //Define thread is as background thread
            thread.IsBackground = true;
            thread.Start();
 
        }
    }
}
if you run this application you will see that the application exists immediately. That is because we define the new thread as "Background thread". Output will be nothing visual.
//Define thread is as Background thread
thread.IsBackground = true;
 
But if we define the "thread" method as foreground thread then it will make application alive and you can have a visual output. 
//Define thread is as Foreground thread
thread.IsBackground = false;
 
Output will be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Debug and see how program executes , You will understand why I did make it sleep the thread every second.
 
Thank You :)

Comments

Popular posts from this blog

CsharpForBeginers 02 - Showing MessageBoxes

Hi guyz ... This is the second part of MessageBoxes Tutorial. In this tutorial we are going to talking about how to create Yes/No message Box. MessageBoxButtons Go to the UI and double click on the Button.Then remove the all codes within the curly braces. Add following codes within the braces Code: MessageBox .Show( "Will you really remove the item?" , "Warning" , MessageBoxButtons .YesNo);

Imagine Cup 2014

Hi guys .. Get ready for imagine cup 2014 In NewZealand .In the next few days imagine cup 2013 finals will happen in Russia. So It is time to wear your thinking hat.You have one year to develop your marvelous ideas.  The Microsoft Imagine Cup is the world’s most prestigious student technology competition, bringing together student innovators from all over the world. If you have a great idea for a new app, bring it to life through Imagine Cup. Over the past 10 years, more than 1.65 million students from 190 countries have participated in the Imagine Cup. 

Visual Studio 2013 New Editor Features

In Visual Studio 2013, we have introduced new features that boost productivity and save time when working inside the Editor. Some of these are new features and some are the most popular extensions from  Productivity Power Tools . These features are a result of the feedback you gave us through  User Voice  requests, forum posts and Connect bugs. The MVP community also helped us pick some of these experiences. Our primary focus for the Editor in this version is to keep the developer in context as much as possible. This blog post describes capabilities that bring information to your fingertips and allow you to do as much as possible without leaving your place in code.