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));

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: , ,


Anonymous said...

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=]convert iTunes to MP3[/url] I used this tool drm removal ( 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!

Anonymous said...

topic53, [url=]order klonopin no prescription[/url], ;-( ywt, order klonopin no prescription *sorry*, [url=]purchase xanax 2mg[/url], :-) mbp, xanax 2mg *sorry*, [url=]order adipex online[/url], ;-( xts, order adipex online *sorry*, [url=]cheap alprazolam[/url], :-( qip, alprazolam no prescription :-), [url=]buy ambien without prescription[/url], :-( mwr, buy ambien no prescription ;-(

Anonymous said...

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 ( or sm like this - please ask me - will rememeber). Really simple utility, that just works, and only 1 button in software.
[url=]I downloaded it from here:[/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=]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!