Re: Asynchronous Threading Issue

From: Sam Loveridge (sloveridge_at_adelaidebank.com.au)
Date: 01/19/05


Date: Thu, 20 Jan 2005 08:01:31 +1030

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.

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?

Sam.

"Kevin Yu [MSFT]" <v-kevy@online.microsoft.com> wrote in message
news:3w5HlWg$EHA.2504@cpmsftngxa10.phx.gbl...
> Hi Sam,
>
> First of all, I would like to confirm my understanding of your issue. From
> your description, I understand that there is something wrong with the
> multi-thread app, in which Test2 never get called. If there is any
> misunderstanding, please feel free to let me know.
>
> Based on the code you have provided, it seems there is a mistake in the
> Test2CallBack. In your code, you called method.BeginInvoke to start the
> thread. However, it has to be method2, because method's thread function
has
> been set to Test1. The following is the modified code. HTH.
>
> Console.WriteLine(String.Format("{0}: {1}: Calling Test2
> asynchronously", DateTime.Now.ToLongTimeString(),
> AppDomain.GetCurrentThreadId))
>
> Dim method2 As New Test2Delegate(AddressOf Test2)
> Dim result As IAsyncResult = method2.BeginInvoke(New
> AsyncCallback(AddressOf Test2Callback), method)
>
> Kevin Yu
> =======
> "This posting is provided "AS IS" with no warranties, and confers no
> rights."
>



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 ... > execution on the main thread, but if there's an error connecting I want to ... > which leads me to suspect the way BeginConnect operates ??? ...
    (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)