ThreadStart Delegate

  • Thread starter Thread starter Patrick J. Quick
  • Start date Start date
P

Patrick J. Quick

Please forgive if this question has already been posted.

Why is the ThreadStart Delegate sealed?
Also, is there a way to pass a parameter (or parameters) to a thread proc?
Using CreateThread(...), we could pass a void pointer to the thread proc.


Any ideas or work arounds are greatly appreciated.


Thanks

Pat
 
Patrick J. Quick said:
Please forgive if this question has already been posted.

Why is the ThreadStart Delegate sealed?

Delegates are always sealed, aren't they? How would you expect to
derive from them?
Also, is there a way to pass a parameter (or parameters) to a thread proc?
Using CreateThread(...), we could pass a void pointer to the thread proc.

Any ideas or work arounds are greatly appreciated.

See http://www.pobox.com/~skeet/csharp/threadstart.html
 
Thanks. That is not really the issue though.
The issue is how to pass parameters to a Thread Proc.
 
Hi Patrick

It probably sealed for similar reasons to what I stated in the following
post:
Lets say you define your own delegate, you would basically need to rewrite
the Thread class to use it.

Maybe this will be possible with Generics. To toughs playing with beta of C#
2.0 confirm if this is true?

Gary



Patrick J. Quick said:
Thanks. That is not really the issue though.
The issue is how to pass parameters to a Thread Proc.
 
Thanks for the info.
I was reviewing Java's implementation (I am not a Java developer, though)
and Java allows you to derive from Thread.
I am curious why Microsoft did not allow the same or similar functionality.
Additionally, I am curious why Microsoft did not provide a mechanism similar
to CreateThread(...) where you can pass a reference (void pointer in the
case of CreateThread(...)) to the Thread Proc. I am using a couple of hacks
to get around this but can not rationalize the omission. Guess there was
just so much to do to get .Net out the door. :-)


Gary van der Merwe said:
Hi Patrick

It probably sealed for similar reasons to what I stated in the following
post:
Lets say you define your own delegate, you would basically need to rewrite
the Thread class to use it.

Maybe this will be possible with Generics. To toughs playing with beta of C#
2.0 confirm if this is true?

Gary
 
Patrick J. Quick said:
I was reviewing Java's implementation (I am not a Java developer, though)
and Java allows you to derive from Thread.
I am curious why Microsoft did not allow the same or similar functionality.

I suspect Sun wouldn't if they started again. Most people in Java who
subclass Thread don't need to at all - they should be using a Runnable
instead, mostly. I can't immediately think of any good reasons to
subclass Thread which aren't basically to do with naming.
Additionally, I am curious why Microsoft did not provide a mechanism similar
to CreateThread(...) where you can pass a reference (void pointer in the
case of CreateThread(...)) to the Thread Proc. I am using a couple of hacks
to get around this but can not rationalize the omission. Guess there was
just so much to do to get .Net out the door. :-)

I don't know much about ThreadProc, but as far as I can see it's pretty
much the equivalent of ThreadStart. What benefit are you missing?
 
Just the ability to pass a parameter (or parameters) to the thread proc
(thread start method).

WIN32 API:
HANDLE CreateThread(
LPSECURITY_ATTRIBUTES lpThreadAttributes,
SIZE_T dwStackSize,
LPTHREAD_START_ROUTINE lpStartAddress,
LPVOID lpParameter,
DWORD dwCreationFlags,
LPDWORD lpThreadId
);
DWORD WINAPI ThreadProc(LPVOID lpParameter);
C#:public delegate void ThreadStart();public Thread(ThreadStart start);C#
Sample Code Follows:tWorker = new System.Threading.Thread(tsWorker);tsWorker
= new System.Threading.ThreadStart(this.CreateOpusPackageWorker);C++
(Unmanaged) Sample Code Follows:hWorkerThread = CreateThread(NULL, 5120,
(LPTHREAD_START_ROUTINE)pThreadStartFunction, pdwThreadData,
CREATE_SUSPENDED, pdwThreadID);DWORD WINAPI ThreadProc(PVOID pv){
//Note: The thread ends when this function returns DWORD dwRet = 0;
CSocket* pSocket = (CSocket*)pv;... return dwRet;}"Jon Skeet [C# MVP]"
 
Patrick J. Quick said:
Just the ability to pass a parameter (or parameters) to the thread proc
(thread start method).

Well the equivalent is easy to mock up:

delegate void ThreadStartWithParameter (object state);

public class ThreadStarter
{
object state;
ThreadStartWithParameter method;

public ThreadStarter(ThreadStartWithParameter method, object state)
{
this.method = method;
this.state = state;
}

public void ThreadStartEntry()
{
method (state);
}
}

You'd then use:

ThreadStarter starter = new ThreadStarter
(new ThreadStartWithParameter(MyMethod), "some state");
new Thread (new ThreadStart(starter.ThreadStartEntry)).Start();

(You may well wish to use shorter names.)
 
Thanks for the info and code.
That works fine.

I am still curious why Microsoft did not include this functionality in the
framework.
 
Back
Top