Solved

Trying to create new thread when button clicked?

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

758 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now