Click here to Skip to main content
16,020,114 members
Home / Discussions / C#
   

C#

 
AnswerRe: String to byte array Pin
Mystic_13-Jul-06 13:15
Mystic_13-Jul-06 13:15 
AnswerRe: String to byte array Pin
Jun Du13-Jul-06 14:26
Jun Du13-Jul-06 14:26 
QuestionHow does onActivated works? Pin
User 309585913-Jul-06 12:11
User 309585913-Jul-06 12:11 
AnswerRe: How does onActivated works? Pin
Rob Graham13-Jul-06 12:31
Rob Graham13-Jul-06 12:31 
Questionits regarding Multi-threading Pin
Mystic_13-Jul-06 11:49
Mystic_13-Jul-06 11:49 
AnswerUse the class Pin
Ennis Ray Lynch, Jr.13-Jul-06 11:55
Ennis Ray Lynch, Jr.13-Jul-06 11:55 
GeneralRe: Use the class Pin
Mystic_13-Jul-06 12:27
Mystic_13-Jul-06 12:27 
GeneralRe: Use the class Pin
Judah Gabriel Himango13-Jul-06 12:42
sponsorJudah Gabriel Himango13-Jul-06 12:42 
Are you using C# 2? If so, you can use anonymous delegates to do this:

void Foo()
{
    int val = 5;
    string text = "blah";
    
    // Use an anonymous method to pass the locals to the function.
    ThreadPool.QueueUserWorkItem(delegate
    {
        OnAnotherThread(val, text);
    });
}

void OnAnotherThread(int i, string s)
{
    MessageBox.Show(s);
}


Another way--the way that the C# 2 compiler implements it under the hood--is to create a class that stores the variables you want to pass to the function, then create a function in that class whose signatures matches WaitCallback. Here's an example:

void Foo()
{
    int val = 5;
    string text = "blah";

    HolderClass holder = new Holder(val, text);
    ThreadPool.QueueUserWorkItem(holder.OnAnotherThread);
}

class HolderClass
{
     private int value;
     private string text;

     public HolderClass(int value, string text)
     {
         this.value = value;
         this.text = text;
     }

     public void OnAnotherThread(object state)
     {
          MessageBox.Show(this.text);
     }
}


Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Messianic Instrumentals (with audio)
The apostle Paul, modernly speaking: Epistles of Paul

Judah Himango


GeneralRe: Use the class Pin
Mystic_13-Jul-06 13:03
Mystic_13-Jul-06 13:03 
GeneralRe: Use the class Pin
Mystic_13-Jul-06 13:10
Mystic_13-Jul-06 13:10 
GeneralRe: Use the class Pin
Jun Du13-Jul-06 14:22
Jun Du13-Jul-06 14:22 
GeneralRe: Use the class Pin
Mystic_13-Jul-06 23:19
Mystic_13-Jul-06 23:19 
GeneralRe: Use the class Pin
Jun Du14-Jul-06 3:14
Jun Du14-Jul-06 3:14 
GeneralRe: Use the class Pin
Gavin Roberts14-Jul-06 3:39
Gavin Roberts14-Jul-06 3:39 
GeneralRe: Use the class Pin
Mystic_14-Jul-06 14:36
Mystic_14-Jul-06 14:36 
GeneralRe: Use the class Pin
Mystic_14-Jul-06 14:39
Mystic_14-Jul-06 14:39 
GeneralRe: Use the class Pin
Judah Gabriel Himango14-Jul-06 4:42
sponsorJudah Gabriel Himango14-Jul-06 4:42 
GeneralRe: Use the class Pin
Mystic_14-Jul-06 13:34
Mystic_14-Jul-06 13:34 
QuestionListView Sorting Problem Pin
SteveZWI13-Jul-06 11:44
SteveZWI13-Jul-06 11:44 
AnswerRe: ListView Sorting Problem Pin
Josh Smith13-Jul-06 12:12
Josh Smith13-Jul-06 12:12 
QuestionMulti-Select TreeView? Pin
AngryC13-Jul-06 11:31
AngryC13-Jul-06 11:31 
AnswerRe: Multi-Select TreeView? Pin
Dave Kreskowiak13-Jul-06 14:24
mveDave Kreskowiak13-Jul-06 14:24 
QuestionResponse redirect paramters concatenating Pin
leckey13-Jul-06 10:56
leckey13-Jul-06 10:56 
AnswerRe: Response redirect paramters concatenating Pin
Ennis Ray Lynch, Jr.13-Jul-06 11:02
Ennis Ray Lynch, Jr.13-Jul-06 11:02 
GeneralRe: Response redirect paramters concatenating Pin
leckey13-Jul-06 11:07
leckey13-Jul-06 11:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.