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

  • Thread starter Thread starter Claire
  • Start date Start date
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
 
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
 
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
 
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.
 
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.
 
Back
Top