Solved

c#: ThreadStart method parameters

Posted on 2006-06-20
4
7,826 Views
Last Modified: 2008-01-09
Here is the code I am working with:

public void myMethod(Thread aThread, ??)
{
     ...

     aThread = new Thread(new ThreadStart(??));
     aThread.Start();

     ...
}

public void PrintStuff()
{
     // do stuff
}

I would like to specifiy PrintStuff(...) as the target method, (substituted above by ??), when I call ThreadStart(...) but I do not know what type to use when passing it in as ?? in public myMethod(...).  

Thank you.
0
Comment
Question by:Zaynul
4 Comments
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 50 total points
ID: 16948727
public void myMethod(Thread aThread, ??)
{
     ...

     aThread = new Thread(new ThreadStart(PrintStuff));
     aThread.Start();

     ...
}

public void PrintStuff()
{
     // do stuff
}
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 50 total points
ID: 16951225
Are you actually wanting to pass in "??" as a String, and then be able to make the Thread point to the actual method that is named the same as the String value?
0
 

Author Comment

by:Zaynul
ID: 16951667
Note to Idle_Mind:
 
I tried a few ways passing it in as a string but couldn't figure out how to get to to contain the actual method name. I kept getting an error saying "..... looking for Method name".  If you know how to do it by passing a string thats great. If there is another way, then Im open to that as well. I just need to be able to choose the method dynamically.  The goal of myMethod is to be reused whenever a thread needs to be initiated and started.

Thanks.


Note To AlexFM: what im looking for is a parameter that would be passed in where "??" is, so that you would be able to set the method name in the thread start dynamically.

Thanks.


0
 
LVL 5

Accepted Solution

by:
shtson earned 400 total points
ID: 16952712
Hai2u,

You cannot pass parameters to a thread,

what you should do, if you want to transfer stuff to be used by the thread, is very similar to AlexFM's solution.

Code:

private static int m_ThreadParam = 1000;

public void myMethod(Thread aThread, ??)
{

     m_ThreadParam = 100000;

     aThread = new Thread(new ThreadStart(PrintStuff));
     aThread.Start();

     ...
}

private static void PrintStuff()
{
     for( int i=0; i<m_ThreadParam; i++)
             // do something using the external param xD
}

Cheers,
Shai
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

760 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

21 Experts available now in Live!

Get 1:1 Help Now