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);
}
}
} 2 2431
"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..
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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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;
|
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...
|
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);...
|
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
|
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()
{
| |
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...
|
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...
|
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.
|
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"
|
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: 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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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...
| |