473,791 Members | 2,899 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Invoke but no call of the event

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(Loa dFileList));
loadThread.Star t();


public void OnLoadFailed(ob ject sender, EventArgs e)
{
this.m_StatusTe xtBox.Text = "Connection to server failed
!!!\r\nAborting ...";
System.Threadin g.Thread.Sleep( 2000);
this.DialogResu lt = DialogResult.Ca ncel;
this.Close();
}

public void OnFinished(obje ct sender, EventArgs e)
{
this.m_StatusTe xtBox.Text = "Success... ";
System.Threadin g.Thread.Sleep( 2000);
this.Close();
}

private void LoadFileList()
{
WebRequest objRequest =
System.Net.Http WebRequest.Crea te("http://deepthought:777 8/WM/files.xml");
objRequest.Time out = 1000;
try
{
// Timeout exception
WebResponse objResponse = objRequest.GetR esponse();
/*
do something with it if success
*/
} catch
{
this.Invoke(new EventHandler(th is.OnLoadFailed ));
return;
}

this.Invoke(new EventHandler(th is.OnFinished)) ;
}
Nov 16 '05 #1
3 1910
Alexander <jo*******@gmx. de> wrote:
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.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
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
"Alexander" <jo*******@gmx. de> wrote in message
news:84******** *************** ***@posting.goo gle.com...
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(Loa dFileList));
loadThread.Star t();


public void OnLoadFailed(ob ject sender, EventArgs e)
{
this.m_StatusTe xtBox.Text = "Connection to server failed
!!!\r\nAborting ...";
System.Threadin g.Thread.Sleep( 2000);
this.DialogResu lt = DialogResult.Ca ncel;
this.Close();
}

public void OnFinished(obje ct sender, EventArgs e)
{
this.m_StatusTe xtBox.Text = "Success... ";
System.Threadin g.Thread.Sleep( 2000);
this.Close();
}

private void LoadFileList()
{
WebRequest objRequest =
System.Net.Http WebRequest.Crea te("http://deepthought:777 8/WM/files.xml"); objRequest.Time out = 1000;
try
{
// Timeout exception
WebResponse objResponse = objRequest.GetR esponse();
/*
do something with it if success
*/
} catch
{
this.Invoke(new EventHandler(th is.OnLoadFailed ));
return;
}

this.Invoke(new EventHandler(th is.OnFinished)) ;
}

Nov 16 '05 #3
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.ShowDialo g();
}
}

class Form2 : Form
{
public Form2()
{
// start thread as described above
}
}
I already discovered in the compactframewor k 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.
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4871
by: boxim | last post by:
hi all, I'm having a few problems whereby my application is hanging when using the Invoke method of a form's control. Basically, when a user clicks a button on the form, it calls a remote function, which in turn fires an event that is caught by the form. The form then need to add a value passed in the event to a form object, however, when using the Invoke method, it just hangs.
3
8883
by: David Logan | last post by:
I have an application using sockets, and it uses the asynchronous method for receiving (and others, but receiving is the basic problem.) In short, I want: class someClass: Form { mySocketClass sss; ...
3
1967
by: Zürcher See | last post by:
I'm using the following structure for my programm, and I never had problems. Now by the invoke of the MyObject_End eventhandler I get the following error: System.Windows.FormsSpecified cast is not valid. at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object args, Boolean synchronous) at System.Windows.Forms.Control.Invoke(Delegate method, Object args) ....
2
3009
by: rawCoder | last post by:
Hi I am having this InvalidOperationException with message Cannot call Invoke or InvokeAsync on a control until the window handle has been created This is raised when i try to invoke a method with arguments I need to do this as the method is being called Asynchronously from elsewhere using delegate's BeginInvoke method. The form is well created/initialized yet the messages states otherwise.
6
31868
by: Peter Rilling | last post by:
Okay, I have the main thread which does all the work. This main thread spawns a worker thread that just periodically poles the environment looking for a certain condition. This second thread is very small, and only is responsible for raising a flag when the environment changes. What I want is for the main thread to raise an event when the change has been flagged. I other words, I want the main thread to raise the event, not this small...
7
2132
by: Jeff Stewart | last post by:
I need a thread to run a subroutine which updates my main form's progress bar. I've properly marshaled all UI updates to the main UI thread, and after the main thread starts the worker thread, it waits for the worker thread to complete by means of a while t.isAlive, sleep(0) mechanism. But when my worker thread calls my UpdateProgressBar routine, which calls Me.Invoke, the invoke call blocks forever. But I can't figure out why the main...
15
62100
by: Oleg Subachev | last post by:
I need to programmatically invoke from other class Click event of the Button on my Form. Button.OnClick method is protected, not public. How to perform this ? Oleg Subachev
18
4614
by: Zytan | last post by:
I want the same function to be run whether you press Enter or double click the listbox. It seems really verbose to write both handlers to both events everytime, even if they both call the same function, or one calls the other. Isn't there an event for 'ListBox selection selected' that is automatically called by ALL the standard GUI ways of doing it? C'mon, this is C#, we aren't supposed to be programming the GUI. There has to be such...
2
4427
by: =?Utf-8?B?a2VubmV0aG1Abm9zcGFtLm5vc3BhbQ==?= | last post by:
vs2005, c# Trying to understand why one way works but the other doesnt. I have not tried to create a simpler mdi/child/showdialog app for posting purposes (I think even this would not be so small or simple). I am hoping the description will generate some ideas I can check out. PROBLEM: ----------------- Switching to UI thread using Invoke(), everything seems good.
0
9669
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9515
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10426
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10207
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9993
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9029
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7537
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4109
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2913
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.