Synchronous call in Async method?

  • Thread starter Thread starter Joshua Coady
  • Start date Start date
J

Joshua Coady

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.BeginRead, etc)?

Example:

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

BeginMyMethod()
{
}

EndMyMethod()
{
}

Thanks,
Josh
 
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
 
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.BeginRead and socket.BeginSend?



Thanks



// AsyncTest.cs



using System;

using System.Diagnostics;

using System.IO;

using System.Net;

using System.Net.Sockets;

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(string hostName, int port)

{

if(hostName == null)

throw new ArgumentNullException("hostName");



Trace.WriteLine(

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



Socket result = null;

try

{

result = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,

ProtocolType.Tcp);

result.Connect(

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

}

catch(Exception exc)

{

CloseSocket(result);



throw new FtpException(string.Format(

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

}

return result;

}




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

private void CloseSocket(Socket socket)

{

Trace.WriteLine("CloseSocket");



if(socket != null && socket.Connected)

{

socket.Close();

socket = null;

}

}




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

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

{

if(hostName == null)

throw new ArgumentNullException("hostName");

if(stream == null)

throw new ArgumentNullException("stream");



Trace.WriteLine("Send");



totalBytes = 0;

byte[] buffer = new byte[DataBufferSize];

Socket socket = OpenSocket(hostName, port);

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

while(bytes > 0)

{

socket.Send(buffer, bytes, SocketFlags.None);

totalBytes += bytes;

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

}

CloseSocket(socket);

}




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

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

private SendCallback send;




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

public IAsyncResult BeginSend(string hostName, int port, Stream
stream,

AsyncCallback callback, object state)

{

if(hostName == null)

throw new ArgumentNullException("hostName");

if(stream == null)

throw new ArgumentNullException("stream");



Trace.WriteLine("BeginSend");



if(send == null)

send = new SendCallback(this.Send);



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

}




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

public void EndSend(IAsyncResult asyncResult)

{

if(asyncResult == null)

throw new ArgumentNullException("asyncResult");



Trace.WriteLine("EndSend");



if(!asyncResult.IsCompleted)

asyncResult.AsyncWaitHandle.WaitOne();



send.EndInvoke(asyncResult);

}

#endregion

}

}
 
Back
Top