473,405 Members | 2,349 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

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

public Form1()
{
InitializeComponent();
AsyncEvent += new EventHandler(Form1_AsyncEvent);
AsyncEvent.BeginInvoke(this, EventArgs.Empty, new
AsyncCallback(OnAsyncCompleted), AsyncEvent);
CallbackEvent += new TestHandler(Form1_CallbackEvent);
}

private void OnAsyncCompleted(IAsyncResult res)
{
string message = "done";
try
{
((EventHandler)res.AsyncState).EndInvoke(res);
}
catch(Exception ex){message = ex.Message;}
Invoke(CallbackEvent, 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 InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.textBox1.Location = new System.Drawing.Point(0, 0);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(292, 273);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

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

private void Form1_AsyncEvent(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(1000);
}

private void Form1_CallbackEvent(string message)
{
this.textBox1.Text += (message + Environment.NewLine);
}
}
}
Nov 11 '05 #1
2 2404
"Craig Vermeer" <ve******@sentdotcom.nospam> wrote in message
news:%2****************@TK2MSFTNGP15.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******@sentdotcom.nospam> wrote in message
news:%2****************@TK2MSFTNGP15.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
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...
1
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...
11
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...
4
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...
3
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
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...
2
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...
2
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...
3
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
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,...
0
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...

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.