Re: Asynchronous Threading Issue

From: Jon Skeet [C# MVP] (skeet_at_pobox.com)
Date: 01/19/05


Date: Wed, 19 Jan 2005 21:49:16 -0000

Sam Loveridge <sloveridge@adelaidebank.com.au> wrote:
> Silly me. Should have triple checked the sample code. I didn't call
> method2.BeginInvoke and as a result it doesn't rally highlight the problem
> I'm having at all.
>
> The threading issue I'm trying to work through revolves around the
> System.Net.Sockets.Socket.BeginConnect method. My understanding of async
> callbacks is that if I use a callback function it will execute on a
> different thread to the main thread. However, if I use a callback function
> in the BeginConnect method and note the thread it would appear to be on the
> main thread. This might not be the actual problem, but after that call
> everything in my app goes haywire.

How are you detecting whether or not you're on "the main thread"?

> I want to call BeginConnect so that I try to connect without holding up
> execution on the main thread, but if there's an error connecting I want to
> retry automatically for x number of times. On success I want to then raise a
> public event to notify success. I can get my code to perform the connection
> attempt, and retries and after actually connecting it executes the public
> "Connected" event once (traced to confirm), but when I trace the handler of
> the event I see it has actually executed the number of times I tried to
> connect.
>
> Anyone else seen this? I use a similar form of code to the example I
> attached earlier (which seems to perform correctly after fixing the bug :))
> which leads me to suspect the way BeginConnect operates ???
>
> Suggestions? Examples of using BeginConnect and retries with callbacks
> instead of loops?

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. (I know it'll be similar code to what you posted
before, but it's good to see the updated version.)

-- 
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Relevant Pages

  • Re: Asynchronous Threading Issue
    ... callbacks is that if I use a callback function it will execute on a ... if I use a callback function ... I want to call BeginConnect so that I try to connect without holding up ... but if there's an error connecting I want to ...
    (microsoft.public.dotnet.general)
  • Re: All Quantifier
    ... The working code: ... :- dynamic done/1. ... % black_edge and so black_conn connecting two processes ... % Now A shall also be required in order to execute C: ...
    (comp.lang.prolog)
  • Re: No results from query in some contexts
    ... maybe even read permission on that table as well. ... ensure you are connecting and executing that proc as you expect. ... > If I execute this sproc from SQL Server Management studio connected to the ... > If I open up the table in visual studio 2005 I see the records. ...
    (microsoft.public.sqlserver.security)
  • Re: TcpClient BeginRead not spawning new thread
    ... I assume that BeginRead can actually execute the callback function ... within the BeginRead statement, and on same thread? ... In general, you can only assume that asynchronous operations will not block indefinitely, which is the main reason for using them. ... you can always change your delegate to execute its code on a thread pool thread instead ) but that's not a very elegant solution. ...
    (microsoft.public.dotnet.framework)
  • Ado and lock timeout error: is it a bug or its my fault?
    ... I'm experiencing a strange problem in Delphi 7 connecting to SqlServer ... using Ado components: ... At that point execute your program and, ...
    (borland.public.delphi.database.ado)

Quantcast