Invoking event from thread. Why is it passing "This"?

C

Claire

Visual Studio 2003

I have a thread running a small timeout. Rather than defining my own
delegate I decided to be lazy and use a standard EventHandler delegate
When OnReadMessageTimeout is called, "sender" appears to points to the form
that started the thread rather than Thread.CurrentThread.Name.
What is going wrong please?


_ReadTimeoutThread = new Thread(new ThreadStart(OnReadMessageLoop));
_ReadTimeoutThread.IsBackground = true;
_ReadTimeoutThread.Name = "_ReadTimeoutThread";
_ReadTimeoutThread.Start();

private void OnReadMessageLoop()
{
Thread.Sleep(1000);
Invoke(new EventHandler(OnReadMessageTimeout), new
object[]{Thread.CurrentThread.Name ,null});
}

private void OnReadMessageTimeout(object sender, System.EventArgs f)
{
!!! sender is owning form and not the string "_ReadTimeoutThread"
string Name = (string)sender;
if (Name != "")
{
foobar
 
J

Jon Skeet [C# MVP]

Visual Studio 2003

I have a thread running a small timeout. Rather than defining my own
delegate I decided to be lazy and use a standard EventHandler delegate
When OnReadMessageTimeout is called, "sender" appears to points to the form
that started the thread rather than Thread.CurrentThread.Name.
What is going wrong please?
From the docs for Control.Invoke:

<quote>
The delegate can be an instance of EventHandler, in which case the
sender parameter will contain this control, and the event parameter
will contain EventArgs.Empty.
</quote>

Jon
 
C

Claire

thanks Jon :)

<quote>
The delegate can be an instance of EventHandler, in which case the
sender parameter will contain this control, and the event parameter
will contain EventArgs.Empty.
</quote>

Jon
 
B

Ben Voigt [C++ MVP]

Jon Skeet said:
<quote>
The delegate can be an instance of EventHandler, in which case the
sender parameter will contain this control, and the event parameter
will contain EventArgs.Empty.
</quote>

Well that's clearly broken. It should only apply to the one-argument
version of Invoke.
 
J

Jon Skeet [C# MVP]

Ben Voigt said:
Well that's clearly broken. It should only apply to the one-argument
version of Invoke.

Agreed - it does seem extremely odd to take parameters and then ignore
them.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top