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)) ;
} 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
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)) ; }
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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.
|
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;
...
|
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)
....
|
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.
|
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...
| |
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...
|
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
|
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...
|
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.
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
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
| |
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...
| |