Solved

c#: ThreadStart method parameters

Posted on 2006-06-20
4
7,834 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Specific format 21 198
object oriented design (python) and documentation tools 2 42
Help Required 3 97
Query Syntax 17 36
This is about my first experience with programming Arduino.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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…

778 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