Solved

Trying to create new thread when button clicked?

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

617 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