Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Trying to create new thread when button clicked?

Posted on 2008-10-03
6
Medium Priority
?
826 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
[X]
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
  • 2
  • 2
6 Comments
 
LVL 13

Accepted Solution

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

Assisted Solution

by:alaran
alaran earned 1000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

715 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