Solved

Trying to create new thread when button clicked?

Posted on 2008-10-03
6
819 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

770 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