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
Solved

c#: ThreadStart method parameters

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
A short article about a problem I had getting the GPS LocationListener working.
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 …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

808 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