Current location: Hot Scripts Forums » Programming Languages » ASP.NET » Creating Threading in C#


Creating Threading in C#

Reply
  #1 (permalink)  
Old 09-02-09, 02:19 AM
sourav12rav sourav12rav is offline
New Member
 
Join Date: Sep 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Spin Creating Threading in C#

How to create a thread in C#.net as well as multi threading
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 09-13-09, 12:02 PM
digioz's Avatar
digioz digioz is offline
Community VIP
 
Join Date: Oct 2003
Location: Chicago, IL
Posts: 2,167
Thanks: 3
Thanked 8 Times in 8 Posts
Take a look at this MSDN Article on Threading:

Threading Tutorial (C#)

Pete
__________________
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 10-12-09, 06:13 AM
urstop urstop is offline
Newbie Coder
 
Join Date: Dec 2007
Location: London
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
You can use the classes from the System.Threading namespace to create a multithreaded application.
__________________

.NET Software Freelancer UK
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 10-29-09, 07:47 PM
BurkeFour BurkeFour is offline
Newbie Coder
 
Join Date: Oct 2009
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Using the Thread Pool is quick and easy...be careful though, as there are limited numbers of threads per processor.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 10-09-10, 04:22 PM
David Edward David Edward is offline
New Member
 
Join Date: Oct 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Any Windows application must have one or more processes. A Process is structural unit with a memory block and using some set of resources. For each executable, the Windows operating system creates some isolated memory block. This C# .Net Tutorial tries to explain the basics of Multithreading in C# .Net.

Every process must have at least one thread. The first thread is created with a process and is known as primary thread. This Primary Thread is entry point of application. In traditional Windows applications it is the method WinMain() and in console applications it is named main().

Main goal of creating multithreading application is performance improvement. As an example, imagine a situation where in a user starts a long process (e.g. copying), he can?t use a single threaded application and wait for an infinite time for the operation to get completed. But if he uses multi?threading application he can set copying process in the background and interact with application without any problems.



At first, if one wants to create a multi-threaded application an important point to be remembered is, a global variable, which is being accessed by different threads, can try to modify the same variable. This is a generic problem, which is solved using a mechanism called Synchronization of threads. Synchronization is nothing but the process of creating some set of rules to operate data or resources.

The C# .Net language has a powerful namespace which can be used for programming with Threads as well as Thread Synchronization in C# .Net programming. The name of the namespace is Sytem.Threading. The most important class inside this namespace for manipulating the threads is the C# .Net class Thread. It can run other thread in our application process.
Sample program on C# Multithreading - C# Tutorial:

The example it creates an additional C# .Net class Launcher. It has only one method, which output countdown in the console.

Code:
//Sample for C# tutorial on Multithreading using lock

    public void Coundown()
    {

        lock(this)
        {

            for(int i=4;i>=0;i--)
            {

                Console.WriteLine("{0} seconds to start",i);

            }

            Console.WriteLine("GO!!!!!");

        }

    }
There is a new keyword lock inside the above chunk of .Net C# tutorial code. This provides a mechanism for synchronizing the thread operation. It means at the same point of time only one thread can access to this method of created object. Unless the lock is released after completion of the code, the next routine or iteration cannot enter the block.

To understand it more clearly please have a look at the piece of main method?s code:

Code:
    Launcher la = new Launcher();

    Thread firstThread = new Thread(new ThreadStart(la.Coundown));
    Thread secondThread =new Thread(new ThreadStart(la.Coundown));
    Thread thirdThread = new Thread(new ThreadStart(la.Coundown));

    firstThread.Start();
    secondThread.Start();
    thirdThread.Start();
As you see there were created three additional threads. These threads start a method of object that has Launcher type. The above program is a very simple example of using multi-threading in C#. Net. But C# .Net allows us to create more powerful applications with any level of complexity.

At last a few words about attached example. It can be compiled using Microsoft .NET command line. Just type csc filename.cs. After it CSC compiler creates an executable version of your code, since it can be run as standard exe file.

Last edited by digioz; 05-05-11 at 12:37 PM. Reason: Please use code tags
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 10-11-10, 10:46 AM
Yeroon's Avatar
Yeroon Yeroon is offline
Code Master
 
Join Date: Aug 2007
Location: Netherlands, Nijmegen
Posts: 850
Thanks: 2
Thanked 20 Times in 20 Posts
Here you can find a simple sample:

Where can I find sample C# code for simple threading? - C# Frequently Asked Questions - Site Home - MSDN Blogs

Please note that if you want to use this with ASP.net you should confirm that your host allows you to start separate threads. Many only allow the current thread the web app is running in
__________________
Feel free to thank people if they help you by clicking thanks at a post.
=================================
Make it idiot proof and someone will make a better idiot.
=================================
Realise the impotence of proof reading everything you publish
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 11-04-10, 06:22 AM
anilalogix@gmail.com anilalogix@gmail.com is offline
New Member
 
Join Date: Nov 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Multithreading -Learn more

To learn more abt Multithreading in C#(Synchronization primitives,Do’s and Dont’s etc)

Click here :
Multithreading in C#

Your comment is awaiting moderation.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 04-16-11, 07:08 AM
dynamicdreamz1 dynamicdreamz1 is offline
Newbie Coder
 
Join Date: Apr 2011
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
This example demonstrates how to create and start a thread, and shows the interaction between two threads running simultaneously within the same process. Note that you don't have to stop or free the thread. This is done automatically by the .NET Framework common language runtime.

Code:
// StopJoin.cs
using System;
using System.Threading;

public class Alpha
{

   // This method that will be called when the thread is started
   public void Beta()
   {
      while (true)
      {
         Console.WriteLine("Alpha.Beta is running in its own thread.");
      }
   }
};

public class Simple
{
   public static int Main()
   {
      Console.WriteLine("Thread Start/Stop/Join Sample");
      
      Alpha oAlpha = new Alpha();

      // Create the thread object, passing in the Alpha.Beta method
      // via a ThreadStart delegate. This does not start the thread.
      Thread oThread = new Thread(new ThreadStart(oAlpha.Beta));

      // Start the thread
      oThread.Start();

      // Spin for a while waiting for the started thread to become
      // alive:
      while (!oThread.IsAlive);
      
      // Put the Main thread to sleep for 1 millisecond to allow oThread
      // to do some work:
      Thread.Sleep(1);
      
      // Request that oThread be stopped
      oThread.Abort();
      
      // Wait until oThread finishes. Join also has overloads
      // that take a millisecond interval or a TimeSpan object.
      oThread.Join();
      
      Console.WriteLine();
      Console.WriteLine("Alpha.Beta has finished");
      
      try 
      {
         Console.WriteLine("Try to restart the Alpha.Beta thread");
         oThread.Start();
      }
      catch (ThreadStateException) 
      {
         Console.Write("ThreadStateException trying to restart Alpha.Beta. ");
         Console.WriteLine("Expected since aborted threads cannot be restarted.");
      }
      return 0;
   }
}
Output:

Thread Start/Stop/Join Sample
Alpha.Beta is running in its own thread.
Alpha.Beta is running in its own thread.
Alpha.Beta is running in its own thread.
...
...
Alpha.Beta has finished
Try to restart the Alpha.Beta thread
ThreadStateException trying to restart Alpha.Beta. Expected since aborted threads cannot be restarted.

Surat website design company | Surat web design company

Last edited by digioz; 05-05-11 at 12:35 PM. Reason: Please use code tags
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks

Tags
thread, threading


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Multi threading sandy1028 Perl 0 04-06-09 04:58 AM
threading pooling mohit Everything Java 2 04-03-08 08:17 AM


All times are GMT -5. The time now is 12:53 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.