SIGN UP MEMBER LOGIN:    
ARTICLE

Writing Your First Multithreading Application

Posted by Mahesh Chand Articles | Multithreading in C# February 14, 2001
If you have never written a multithreading application in C# or .NET, this application might be helpful for you.
Reader Level:
Download Files:
 

If you have never written a multithreading application in C# or .NET, this application might be helpful for you. In this application, second thread writes data to the console. The Thread class encapsulate the thread functionality in .NET.

You create a new secondary thread by using Thread class constructor. The constructor of the Thread class takes one argument and that is the function you want to execute in secondary thread. This function might be from another class or this class itself. Once you created an instance, you set the priority of the thread by calling Priority method and then call the Start method to start the thread.

You use Abort method to kill a thread. Make sure thread is alive before you kill it.

Another way to create a secondary thread is use ThreadStart. See my Tutorial: Multithreading for Beginners for more details.

public class mt1
{
public static void PrintCurDateTime()
{
Console.WriteLine( " Secondary Thread" );
Console.WriteLine( DateTime.Now.ToLongTimeString() );
}
public static int Main(String[] args)
{
Console.WriteLine("Main Thread \n");
try
{
Thread secThread =
new Thread(new ThreadStart( PrintCurDateTime ) );
secThread.Priority = ThreadPriority.Highest;
secThread.Start();
if ( secThread.IsAlive)
{
secThread.Abort();
}
}
catch (Exception e)
{
Console.WriteLine( e.ToString());
}
return 0;

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Gauge for SharePoint
Become a Sponsor