Invoke but no call of the event

  • Thread starter Thread starter Alexander
  • Start date Start date
A

Alexander

I have a dialog that loads a file from a web server, this is done in
an own thread. On finish or failure the thread is supposed to raise an
event notifying the dialog. If the WebRequest now raises a timeout
exception the invoke of the event is done, but the event is never
executed. For testing purposes I exchanged the exception once by a
simple divison by zero which works perfectly, the event is fired and
executed. I debugged the thread to make sure the exception really
reaches the catch block and invokes the event. But I do not understand
why the event function is not called...
I tried the same code on the compact framework on which it is supposed
to run and on the normal framework, but the result is identical.


In the example LoadFileList is the thread function started from
another function of the same class:
Thread loadThread = new Thread(new ThreadStart(LoadFileList));
loadThread.Start();




public void OnLoadFailed(object sender, EventArgs e)
{
this.m_StatusTextBox.Text = "Connection to server failed
!!!\r\nAborting...";
System.Threading.Thread.Sleep(2000);
this.DialogResult = DialogResult.Cancel;
this.Close();
}

public void OnFinished(object sender, EventArgs e)
{
this.m_StatusTextBox.Text = "Success...";
System.Threading.Thread.Sleep(2000);
this.Close();
}

private void LoadFileList()
{
WebRequest objRequest =
System.Net.HttpWebRequest.Create("http://deepthought:7778/WM/files.xml");
objRequest.Timeout = 1000;
try
{
// Timeout exception
WebResponse objResponse = objRequest.GetResponse();
/*
do something with it if success
*/
} catch
{
this.Invoke(new EventHandler(this.OnLoadFailed));
return;
}

this.Invoke(new EventHandler(this.OnFinished));
}
 
Alexander said:
I have a dialog that loads a file from a web server, this is done in
an own thread. On finish or failure the thread is supposed to raise an
event notifying the dialog. If the WebRequest now raises a timeout
exception the invoke of the event is done, but the event is never
executed. For testing purposes I exchanged the exception once by a
simple divison by zero which works perfectly, the event is fired and
executed. I debugged the thread to make sure the exception really
reaches the catch block and invokes the event. But I do not understand
why the event function is not called...
I tried the same code on the compact framework on which it is supposed
to run and on the normal framework, but the result is identical.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
From what I see, the use of Invoke() is correct. However, you should
understand that you will never see your message displayed in the status
textbox. That's because, in your OnLoadFailed and OnFinished functions you
set the status box's text and then sleep the current thread (which is the UI
thread for the form). That means no Paint events can get processed. When the
Sleep is done, you close the form. Thus, what I would have expected the
behavior to be is that, after either a success or failure, your dialog would
freeze for 2 seconds and then close without ever displaying the status
message.

Ken
 
Hi, the description of the problem was not fully correct - my fault
due to too fast testing. The program works exactly like this on the
normal framework the timeout took much longer than I expected though,
but it does not work on the compact framework.

Also I discovered, that the problem is not the kind of exception or
the webrequest but the dialog itself.

The whole program as a shorted version looks like this:

class Form1 : Form
{
public Form1()
{
// do something
Form2 preLoadDialog = new Form2();
form2.ShowDialog();
}
}

class Form2 : Form
{
public Form2()
{
// start thread as described above
}
}


I already discovered in the compactframework newsgroup, that there are
several issues with the compact framework with calling a form in the
constructor of a form, with showing a form as a dialog and with
message processing if a dialog is called as modal. The program as
described above blocks at the synchronized Invoke call which never
return, because the form, which should have an own message loop, just
queues all messages. The messages will be processed all at once if the
dialog is hidden or closed - so useless if the invoke itself should
close the dialog.
From what I have heard this and some other issues might already have
been resolved in SP1, but the emulator is not fixed, so I will try
this on a real device tomorrow. If it works, I still might have to
find a workaround because it is kind of impossible to make all
testings on the real device.
 
Back
Top