Click here to Skip to main content
15,886,689 members
Articles / Programming Languages / C#

How to run method in separate thread

Rate me:
Please Sign up or sign in to vote.
4.93/5 (14 votes)
24 Mar 2011CPOL 37.2K   8  
Or in the new framework 4.0 use parallel tasks?private void MethodStarter(){ Task myFirstTask = Task.Factory.StartNew(Method1); Task mySecondTask = Task.Factory.StartNew(Method1);}private void Method1(){}private void Method2(){}Sascha Barber wrote this[^]...

Alternatives

Members may post updates or alternatives to this current article in order to show different approaches or add new features.

Please Sign up or sign in to vote.
24 Mar 2011J a a n s
Much easier to go for async invoke.new MethodInvoker(MyMethod).BeginInvoke(null, null);We can use the suitable overload in case of parameters/callback methods.Note: MethodInvoker is available inside the System.Windows.Forms name space. You can create your own delegates also.
Please Sign up or sign in to vote.
25 Mar 2011Pavel Yermalovich 3 alternatives  
Multithreading
Please Sign up or sign in to vote.
24 Mar 2011Nick Reshetinsky
You may actualy skip delegate types when assigning a method to execute: private void DoLongOperation( ) { // long processing } Thread thr = new Thread(DoLongOperation); thr.Start( );

License

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


Written By
Architect http://4dotnet.nl
Netherlands Netherlands
I'm developing software for over two decades. I'm working as a cloud solution architect and a team lead at 4DotNet in The Netherlands.

In my current position, I love to help customers with their journey to the cloud. I like to create highly performant software and to help team members to a higher level of software development.

My focus is on the Microsoft development stack, mainly C# and the Microsoft Azure Cloud. I also have a strong affinity with Angular. As a result of my contributions to the community, I received the Microsoft MVP Award for Microsoft Azure.

Comments and Discussions