Pages

Advertisement

Wednesday, October 10, 2007

Multithreading Using C#

 

Multithreading has always been a helpful friend of a programmer . Multithreading increases the response time of an application .  Multithreading can be implemented by using namespace system.threading  .

Why Multithreading is required ?

> For this I will give U a simple example : Assume U have a company and U have only one assistance and 0 workers then what will happen the complete workload of company will be on your assistance , the assistance then take much time to do everything one by one , Which will affect U in turn .. your application also runs in the same manner if U have only single thread Your execution and your processing will be very slow which may also hangs your application . So Multithreading distributes specified process to run in another thread  an wont affect the normal processing of your application ..

Lets have a little demo for this .

Demo - 1

 

using System;
using System.Threading;
public class MyThread
{
    public static void Thread1()
    {
        for (int i = 0; i < 10; i++)
        {
            Console.WriteLine("Thread1 {0}", i);
        }
    }
    public static void Thread2()
    {
        for (int i = 0; i < 10; i++)
        {
            Console.WriteLine("Thread2 {0}", i);
        }
    }
}
 
public class MyClass
{
    public static void Main()
    {
        Console.WriteLine("Before start thread");
        Thread tid1 = new Thread(new ThreadStart(MyThread.Thread1));
        Thread tid2 = new Thread(new ThreadStart(MyThread.Thread2));
        tid1.Start();
        tid2.Start();
    }
}


Explanation :



In this Demo u have 2 static methods Thread1 and Thread2 . To make a thread we are making a object class named Thread.



The constructor of this class takes a reference of a ThreadStart class. This constructor
can send two types of exceptions; ArgumentNullException when the parameter is
a null reference or a Security Exception when program does not have permission to
create thread.

 



The parameter of the Thread class is reference to a ThreadStart class.
ThreadStart class points to the method that should be executed first when a
thread is started. The parameter is the name of the function, which is considered as
a thread function. Thread1 is a static function so we give it with the name of class
name without making an object of the class. The thread starts execution with
Start() method of the Thread class. The output of this program is



Before start thread
Thread1 0
Thread1 1
Thread1 2
Thread1 3
Thread1 4
Thread1 5
Thread1 6
Thread1 7
Thread1 8
Thread1 9
Thread2 0
Thread2 1
Thread2 2
Thread2 3
Thread2 4
Thread2 5
Thread2 6
Thread2 7
Thread2 8
Thread2 9



This is how we using multithreading in our application ...



Technorati Tags: , ,

2 comments:

  1. Dear comrads!

    Is it a deal to convert iTunes to mp3? I say NOPE :) Its really puzzling not being able to play iTunes tracks with my $300 iriver!
    Digging for a solution brings certain results. To [url=http://drm-removal.com/features/iTunestoMP3/convert_iTunes_to_MP3.htm]convert iTunes to MP3[/url] I used this tool drm removal (drm-removal.com or something like this, please ask me - will find that web again). I don't care what is drm - what I read, its something dealing with protection and nothing about iTunes.
    But this tool really works for me and it converted quickly my iTuines entire library, including video files!.

    What you your solution? Why iTunes brings this shit? I didn't like ipods at all! They simple stuck and suck!
    What is that drm that I loose converting my itunes to mp3?

    thanks in advance!

    ReplyDelete
  2. Just to share an expirience:
    I found a way to grab Youtube movies. Some of are sites where you put a link from YouTube and it gives you file with strange extension and small size. Some are really complicated.
    My way was to use moviesherlock (moviesherlock.com or sm like this - please ask me - will rememeber). Really simple utility, that just works, and only 1 button in software.
    [url=http://download.cnet.com/3001-2140_4-75065920.html?spi=68a87b0bad1ea20e12285c5005469cde]I downloaded it from here: http://download.cnet.com/3001-2140_4-75065920.html?spi=68a87b0bad1ea20e12285c5005469cde[/url].
    I downloaded even HD movies!

    But it looks too easy, has no player, but just works. It downloaded ~12 concurrent movies for me and actually slows down my system. Its possible to do search in [url=http://moviesherlock.com]moviesherlock[/url] and search results differs from what you see in YouTube. Thats strange.
    Who knows - Tube search are same for same keyword? I likes cat (Simons cat / dog).
    Why doesn't youtube allows to download its movies without external tools?!! It actually sucks!

    Thanks in advance!

    ReplyDelete