Solved

c#: ThreadStart method parameters

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

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Modify a small python script 19 122
What does != "" mean in programming 8 134
Back-end Database connect string being set someplace where I can't find it. 10 49
Create .bat File 16 73
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
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…

733 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