Click here to Skip to main content
15,890,609 members
Articles / Programming Languages / C#
Tip/Trick

How to run method in separate thread

Rate me:
Please Sign up or sign in to vote.
3.50/5 (3 votes)
25 Mar 2011CPOL 38.5K   10   2
Multithreading

Here is the main method which can run a method in a separate thread:


C#
private static void RunMethodInSeparateThread(Action action)
{
    var thread = new Thread(new ThreadStart(action));
    thread.Start();
}

Here is how to use this method:


C#
private static void Method1()
{
    //Method1 implementation
}

private static void Method2()
{
    //Method2 implementation
}

static void Main(string[] args)
{
    RunMethodInSeparateThread(Method1);
    RunMethodInSeparateThread(Method2);
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Belarus Belarus
Software developer with over 3 years of extensive experience in analysis, design and development. Recently, most interested in refactoring and design patterns applied to .NET Framework.

Core technologies I am using: OOP, OOD, DDD, TDD, N-tier applications, enterprise development.

Certificates:
Brainbench: .NET Framework 3.5 Fundamentals, Data Modeling Concepts, Web Design Concepts, C#

Microsoft: Exam 70-526: TS: Microsoft .NET Framework 2.0 - Windows-Based Client Development

INTUIT.RU: Development of Web-application ASP. NET Using Visual Studio. NET, Web applications in ASP.NET, Microsoft .NET Framework Distributed Applications Development.

Specialties
NET Framework : 2.0, 3.5, 4.0
Languages and technologies: C#, ASP.NET MVC 2, ASP.NET MVC 3, WCF, ASP.NET 4.0, Web Services, ADO.NET, LINQ, Entity Framework, NHibernate 3.0, JavaScript, HTML, CSS, XML, Ajax
RDBMS : SQL Server 2005, 2008, 2008 R2, MS Access.
Reporting: MS SQL Reporting Services, Crystal Reports

Comments and Discussions

 
Questionparameters Pin
Armando Martínez González25-Apr-12 18:08
professionalArmando Martínez González25-Apr-12 18:08 
GeneralReason for my vote of 4 Good reading Pin
Mohammed Uvace4-Apr-11 21:04
Mohammed Uvace4-Apr-11 21:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.