Synchronous call in Async method?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joshua Coady

    #1

    Synchronous call in Async method?

    I am writing a class that includes several async methods following the
    BeginXxx, EndXxx pattern. Some of these methods will call methods in the
    framework such as Stream.Read or Socket.Send. Do I need to, or is it
    recommended to use the async method calls instead (Stream.BeginRe ad, etc)?

    Example:

    MyMethod()
    {
    //...
    Stream.Read();// I know this method requires parameters
    //
    // Should it be this instead?
    // Stream.BeginRea d();
    // Stream.EndRead( );
    //...
    }

    BeginMyMethod()
    {
    }

    EndMyMethod()
    {
    }

    Thanks,
    Josh


  • Benny Mathew

    #2
    Re: Synchronous call in Async method?

    Hi Joshua,
    That depends on the requirement.
    you would like to do async only where you need the control to come back to
    your code immediately and if the instruction to be executed next is not
    dependant on the completion of the previous instruction.


    Cheers
    Benny

    "Joshua Coady" <josh@coady.u s> wrote in message
    news:eHkTHuKcDH A.3016@tk2msftn gp13.phx.gbl...[color=blue]
    > I am writing a class that includes several async methods following the
    > BeginXxx, EndXxx pattern. Some of these methods will call methods in the
    > framework such as Stream.Read or Socket.Send. Do I need to, or is it
    > recommended to use the async method calls instead (Stream.BeginRe ad, etc)?
    >
    > Example:
    >
    > MyMethod()
    > {
    > //...
    > Stream.Read();// I know this method requires parameters
    > //
    > // Should it be this instead?
    > // Stream.BeginRea d();
    > // Stream.EndRead( );
    > //...
    > }
    >
    > BeginMyMethod()
    > {
    > }
    >
    > EndMyMethod()
    > {
    > }
    >
    > Thanks,
    > Josh
    >
    >[/color]


    Comment

    • Joshua Coady

      #3
      Re: Synchronous call in Async method?

      Does this code look like it will work as expected for asynchronous
      operations? Or, do my calls to stream.Read and socket.Send need to be
      changed to stream.BeginRea d and socket.BeginSen d?



      Thanks



      // AsyncTest.cs



      using System;

      using System.Diagnost ics;

      using System.IO;

      using System.Net;

      using System.Net.Sock ets;

      using System.Text;



      namespace JLCoady

      {

      public class AsyncTest

      {

      #region Fields

      private int dataBufferSize = 512;

      private int totalBytes;

      #endregion



      #region Properties


      //--------------------------------------------------------------------------
      ---------

      public int DataBufferSize

      {

      get { return dataBufferSize; }

      set { dataBufferSize = value; }

      }




      //--------------------------------------------------------------------------
      ---------

      public int TotalBytes

      {

      get { return totalBytes; }

      }

      #endregion



      #region Constructors


      //--------------------------------------------------------------------------
      ---------

      public AsyncTest()

      {

      }

      #endregion



      #region Methods


      //--------------------------------------------------------------------------
      ---------

      private Socket OpenSocket(stri ng hostName, int port)

      {

      if(hostName == null)

      throw new ArgumentNullExc eption("hostNam e");



      Trace.WriteLine (

      string.Format(" {0}:{1}", hostName, port), "OpenSocket ");



      Socket result = null;

      try

      {

      result = new Socket(AddressF amily.InterNetw ork,
      SocketType.Stre am,

      ProtocolType.Tc p);

      result.Connect(

      new IPEndPoint(Dns. Resolve(hostNam e).AddressList[0], port));

      }

      catch(Exception exc)

      {

      CloseSocket(res ult);



      throw new FtpException(st ring.Format(

      "Could not connect to {0} on port {1}.", hostName, port),
      exc);

      }

      return result;

      }




      //--------------------------------------------------------------------------
      ---------

      private void CloseSocket(Soc ket socket)

      {

      Trace.WriteLine ("CloseSocket") ;



      if(socket != null && socket.Connecte d)

      {

      socket.Close();

      socket = null;

      }

      }




      //--------------------------------------------------------------------------
      ---------

      public void Send(string hostName, int port, Stream stream)

      {

      if(hostName == null)

      throw new ArgumentNullExc eption("hostNam e");

      if(stream == null)

      throw new ArgumentNullExc eption("stream" );



      Trace.WriteLine ("Send");



      totalBytes = 0;

      byte[] buffer = new byte[DataBufferSize];

      Socket socket = OpenSocket(host Name, port);

      int bytes = stream.Read(buf fer, 0, buffer.Length);

      while(bytes > 0)

      {

      socket.Send(buf fer, bytes, SocketFlags.Non e);

      totalBytes += bytes;

      bytes = stream.Read(buf fer, 0, buffer.Length);

      }

      CloseSocket(soc ket);

      }




      //--------------------------------------------------------------------------
      ---------

      private delegate void SendCallback(st ring hostName, int port, Stream
      stream);

      private SendCallback send;




      //--------------------------------------------------------------------------
      ---------

      public IAsyncResult BeginSend(strin g hostName, int port, Stream
      stream,

      AsyncCallback callback, object state)

      {

      if(hostName == null)

      throw new ArgumentNullExc eption("hostNam e");

      if(stream == null)

      throw new ArgumentNullExc eption("stream" );



      Trace.WriteLine ("BeginSend" );



      if(send == null)

      send = new SendCallback(th is.Send);



      return send.BeginInvok e(hostName, port, stream, callback, state);

      }




      //--------------------------------------------------------------------------
      ---------

      public void EndSend(IAsyncR esult asyncResult)

      {

      if(asyncResult == null)

      throw new ArgumentNullExc eption("asyncRe sult");



      Trace.WriteLine ("EndSend");



      if(!asyncResult .IsCompleted)

      asyncResult.Asy ncWaitHandle.Wa itOne();



      send.EndInvoke( asyncResult);

      }

      #endregion

      }

      }







      "Joshua Coady" <josh@coady.u s> wrote in message
      news:eHkTHuKcDH A.3016@tk2msftn gp13.phx.gbl...[color=blue]
      > I am writing a class that includes several async methods following the
      > BeginXxx, EndXxx pattern. Some of these methods will call methods in the
      > framework such as Stream.Read or Socket.Send. Do I need to, or is it
      > recommended to use the async method calls instead (Stream.BeginRe ad, etc)?
      >
      > Example:
      >
      > MyMethod()
      > {
      > //...
      > Stream.Read();// I know this method requires parameters
      > //
      > // Should it be this instead?
      > // Stream.BeginRea d();
      > // Stream.EndRead( );
      > //...
      > }
      >
      > BeginMyMethod()
      > {
      > }
      >
      > EndMyMethod()
      > {
      > }
      >
      > Thanks,
      > Josh
      >
      >[/color]


      Comment

      Working...