Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

c#: ThreadStart method parameters

Posted on 2006-06-20
4
Medium Priority
?
7,842 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
[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
4 Comments
 
LVL 48

Assisted Solution

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

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

     ...
}

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

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 150 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 1200 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
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…
Simple Linear Regression

636 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