passing data to thread

  • Thread starter Thread starter Bern
  • Start date Start date
B

Bern

How to pass a data to a newly created thread in the following?

--------------------------------------------------------------------

Thread newthread = new Thread (new ThreadStart (Method));

newthread.Start ();

void Method (){

.... some code. ...
}
 
class ThreadController
{
private int i;
private string s;
public ThreadController(int i, string s)
{
this.s = s;
this.i = i;
}
public void ThreadProc()
{
// use i and s
}
}

ThreadController tc = new ThreadController(42, "Hello World");
Thread t = new Thread(new ThreadStart(tc.ThreadProc);
t.Start();

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<[email protected]>

How to pass a data to a newly created thread in the following?

--------------------------------------------------------------------

Thread newthread = new Thread (new ThreadStart (Method));

newthread.Start ();

void Method (){

.... some code. ...
}




---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.782 / Virus Database: 528 - Release Date: 22/10/2004



[microsoft.public.dotnet.languages.csharp]
 
Hi Bern,
How to pass a data to a newly created thread in the following?

--------------------------------------------------------------------

Thread newthread = new Thread (new ThreadStart (Method));

newthread.Start ();

void Method (){

.... some code. ...
}

I'm using a wrapper that supports a delegate with a generic
state object argument.


ContextThread newthread = new ContextThread(
new ContextThreadStart (Method), someObject);

void Method(object state) {
SomeObject c = state as SomeObject;
...
}

bye
Rob


using System;
using System.Threading;

namespace ACME {

public delegate void ContextThreadStart(object state);

public class ContextThread {

public Thread Thread {
get {
return thread;
}
}
readonly Thread thread;
readonly ContextThreadStart start;
readonly object state;

public ContextThread(ContextThreadStart start, object state) {
this.start = start;
this.state = state;
this.thread = new Thread(new ThreadStart(ThreadProc));
}

public void Start() {
thread.Start();
}

void ThreadProc() {
start(state);
}
}

}
 
Hi Bern,

I'd like to know if this issue has been resolved yet. Is there anything
that I can help. I'm still monitoring on it. If you have any questions,
please feel free to post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Back
Top