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

C# Character Escape Sequences

Character combinations consisting of a backslash ( \ ) followed by a letter or by a combination of digits are called "escape sequences." To represent a newline character, single quotation mark, or certain other characters in a character constant, you must use escape sequences. An escape sequence is regarded as a single character and is therefore valid as a character constant.

Windows 8 Icons: Segoe UI Symbol

Whatz up guyz... In this post i am going to tell you about "Windows 8 Icons" . If you ever used a windows 8 app, you know that there are so many standard Icons. Actually we do not want to add images for each button, simply we can use the character map for obtain the icons.Go to the character map and select the font as "Segoe UI Symbol" .Then you will see lot of standard symbols in the character map.

SQLite Database for Windows 8 App : Adding existing SQLite DB file

Hi Guys ... This Post is about adding an existing SQLite database file to the Windows Metro App project. As I think "using SQLite" is the best way to create local database for Windows Rt applications.Actually SQLite is very easy to learn but I really think if Microsoft SQL Server CE supports windows rt applications it will more fun. .Isnt it ?. :)