Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Trying to create new thread when button clicked?

Posted on 2008-10-03
6
820 Views
Last Modified: 2011-10-03
Experts,

I'm trying to run a method in a new thread, once a button is clicked.  Here's my code:

        private void button1_Click(object sender, EventArgs e)
        {
            Thread newThread = new Thread(new ThreadStart(RunLoop(0)));
            newThread.Start();      
        }

I get the error, "Method name expected".

Please help!
0
Comment
Question by:mhouldridge
  • 2
  • 2
6 Comments
 
LVL 13

Accepted Solution

by:
joechina earned 250 total points
ID: 22634874
0
 
LVL 4

Assisted Solution

by:alaran
alaran earned 250 total points
ID: 22634893
ThreadStart is a delegate, so you need to just pass the name of the method you want invoked. The method must match the signature og the delegate.

This means that you must do like so:
Thread newThread = new Thread(new ThreadStart(RunLoop));
newThread.Start();

Note that the ThreadStart delegate returns void and has no parameters.

To pass parameters you need to pass a ParameterizedThreadStart, which returns void and takes one parameter of type object:
Thread newThread = new Thread(new ParameterizedThreadStart(RunLoop));
newThread.Start(myParameterObject);  

Your threaded method should then look like so:
public static void RunLoop(object parameters) {}

The object you pass to the newThread.Start() method will be the one you get in your RunLoop method.
0
 
LVL 13

Expert Comment

by:joechina
ID: 22634895
private void button1_Click(object sender, EventArgs e)
        {
            Thread newThread = new Thread(new ParameterizedThreadStart(RunLoop));
            newThread.Start(0);      
        }
0
 
LVL 4

Expert Comment

by:alaran
ID: 22865171
As both joechina and myself gave the same answer at pretty much the same time, I think it would be fair to split the points.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question