473,785 Members | 2,830 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

AsyncCallback called twice on Exception

Hi All,

I noticed an interesting issue the other day, and thought someone here
might be able to shed some light on things.

I have a situation where I'm using asynchronous delegates, and an
exception is getting thrown from within the AsyncCallback on the
delegate. For some reason, this causes the AsyncCallback delegate to be
executed twice. Now, I understand how to stop the situation from
happening. However, what I didn't know was why things behaved the way
they did.

Below is a 'short but complete' code sample that demonstrates the behavior.

Thanks,
Craig

using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Data;

namespace AyncDelegateIss ueDemo
{
public class Form1 : System.Windows. Forms.Form
{
private System.Windows. Forms.TextBox textBox1;
private delegate void TestHandler(str ing message);
private event TestHandler CallbackEvent;
private event EventHandler AsyncEvent;

public Form1()
{
InitializeCompo nent();
AsyncEvent += new EventHandler(Fo rm1_AsyncEvent) ;
AsyncEvent.Begi nInvoke(this, EventArgs.Empty , new
AsyncCallback(O nAsyncCompleted ), AsyncEvent);
CallbackEvent += new TestHandler(For m1_CallbackEven t);
}

private void OnAsyncComplete d(IAsyncResult res)
{
string message = "done";
try
{
((EventHandler) res.AsyncState) .EndInvoke(res) ;
}
catch(Exception ex){message = ex.Message;}
Invoke(Callback Event, new object[]{message});
throw new Exception("test ");
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.textBox1 = new System.Windows. Forms.TextBox() ;
this.SuspendLay out();
//
// textBox1
//
this.textBox1.D ock = System.Windows. Forms.DockStyle .Fill;
this.textBox1.L ocation = new System.Drawing. Point(0, 0);
this.textBox1.M ultiline = true;
this.textBox1.N ame = "textBox1";
this.textBox1.S ize = new System.Drawing. Size(292, 273);
this.textBox1.T abIndex = 0;
this.textBox1.T ext = "";
//
// Form1
//
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(292, 273);
this.Controls.A dd(this.textBox 1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayo ut(false);

}
#endregion

[STAThread]
static void Main()
{
Application.Run (new Form1());
}

private void Form1_AsyncEven t(object sender, EventArgs e)
{
System.Threadin g.Thread.Sleep( 1000);
}

private void Form1_CallbackE vent(string message)
{
this.textBox1.T ext += (message + Environment.New Line);
}
}
}
Nov 11 '05 #1
2 2430
"Craig Vermeer" <ve******@sentd otcom.nospam> wrote in message
news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
Hi All,

I noticed an interesting issue the other day, and thought someone here
might be able to shed some light on things.


Coincidentally, the first item on the list here.....

Backwards Breaking Changes from version 1.1 to 2.0
"If a callback function of an async delegate throws an exception, the
callback will be invoked twice."
http://www.gotdotnet.com/team/change...0/default.aspx
--
Ken Halter - MS-MVP-VB (visiting from VB6 world) - http://www.vbsight.com
Please keep all discussions in the groups..
Nov 11 '05 #2
Ah. So it IS a bug :)

Thanks!

Ken Halter wrote:
"Craig Vermeer" <ve******@sentd otcom.nospam> wrote in message
news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
Hi All,

I noticed an interesting issue the other day, and thought someone here
might be able to shed some light on things.


Coincidentally, the first item on the list here.....

Backwards Breaking Changes from version 1.1 to 2.0
"If a callback function of an async delegate throws an exception, the
callback will be invoked twice."
http://www.gotdotnet.com/team/change...0/default.aspx

Nov 11 '05 #3

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

Similar topics

6
3696
by: Robert | last post by:
Hello all... In my code below, the Notify Constructor and Destructor is getting called twice and it appears that a new Notify object is created on the 2nd call. The 2nd call is caused by this line below: pNotify = new EMAILnotify; that lives in the Notification Constructor. One theory is that the Notify base class is not completely constructed prior to using it in the Notification Constructor code: pNotify = new EMAILnotify;
1
5224
by: Jim P. | last post by:
I'm having trouble returning an object from an AsyncCallback called inside a threaded infinite loop. I'm working on a Peer2Peer app that uses an AsyncCallback to rerieve the data from the remote peer. I have no problem connecting the peers and streaming Network Streams. When the incoming data is finished recieving, I act upon it. This works great as long as all of the code is inside my form. I want to build the networking code into a...
11
3788
by: Doug Thews | last post by:
I've been working on some samples that use BeginInvoke/EndInvoke. In one example, I call BeginInvoke and pass it an AsyncCallback function pointer. I was messing around with ReaderWriterLocks and noticed that if I did this, it worked (please ignore the lack of try ... catch blocks, because I cut down the code to be more brief - I have a try...catch surround the AcquireWriterLock method): ---------- myLock.AcquireWriterLock(1000);...
4
22891
by: LP | last post by:
Hello! I am still transitioning from VB.NET to C#. I undertand the basic concepts of Delegates, more so of Events and somewhat understand AsyncCallback methods. But I need some clarification on when to use one over another? If anyone could provide any additional info, your comments, best practices, any good articles, specific examples, etc. Thank you
3
9419
by: Lenn | last post by:
Hello, I have the following example of AsyncCallback from a C# book which I wanted to implement in my project: //Class with AsyncDelegate public class AsyncProcess { public AsyncProcess() {
2
2130
by: Jared | last post by:
Hello all, I have a web service that I am trying to retrieve data from asynchronously. According to the MSDN documentation (ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/vbcon/html/vbtskCallingWebServiceAsynchronously.htm) I should be able to call a webservice asynchronously using a delegate function. The callback seems to work, but, when I assign the value to an existing datagrid the program hangs. I don't quite understand, what do I...
2
486
by: Craig Vermeer | last post by:
Hi All, I noticed an interesting issue the other day, and thought someone here might be able to shed some light on things. I have a situation where I'm using asynchronous delegates, and an exception is getting thrown from within the AsyncCallback on the delegate. For some reason, this causes the AsyncCallback delegate to be executed twice. Now, I understand how to stop the situation from happening. However, what I didn't know was...
2
4914
by: Steven Blair | last post by:
Hi, I am using an AsyncCallback to receive a TcpClient on incoming connections. The code below works fine until I start the application and no connection is received within the time limit. I could easily try / catch the code causing the problem but I would like to understand why this problem is happening. I was unable to find any field on the TcpListener that would indicate the Listenign socket was stopped.
3
1860
by: Artie | last post by:
Hi, We have an app which uses an AsyncCallback method to handle the return from a COM call. In this callback, if we try to do anything with a WebBrowser control, we get the following exception: "InvalidCastException was unhandled Specified cast is not valid"
0
9645
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
10330
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...
1
10093
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9952
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...
1
7500
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...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
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
2880
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.