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

Start button confirmed to make a comeback in Windows 8.1

We first  heard rumors  about a possible comeback of the Start menu button in Windows 8.1 last week, but now sources speaking to  The Verge  have confirmed that this will indeed be the case, only it’s probably not what most detractors were hoping for. The newly reintroduced button will reportedly sit on the traditional bottom left corner, and will look near-identical to the existing Windows flag used in the Charm bar, but clicking on it will simply bring up the tile-based Start screen rather than the old Start menu. Read Full Article

Load an Addin into DraftSight

These days i am developing two plugins for Autocad 2012 &  DraftSight 2013.so when i was developing the plugin i got in to big trouble.Normally in the Autocad , we can load the .dll file simply typing the command "NETLOAD" but in the Draftsight i could not able to do that. It said  " D:\c \projects\c projects\DSAddinCSharp1\ DSAddinCSharp1\bin\Debug\ DSAddinCSharp1.dll is not a valid add-in "

Barcode Reader for Motorola MC3000 Device (Windows Mobile 6)

You can simply click the button and read the barcode. Create the User interface as you wish and add following codes to the scan button and Scanning.cs class.