A
Anibal Acosta
One complete day a lost finding the problem, Now I know what is, but I
don't know why?
This is my simple source code. The first method call the BeginConnect()
using the same windows form thread, and the second method call the
BeginConnect() using another thread
You will think... "Why you want to execute the BeginConnect using
another thread" But believe me that a really need
I really need your help
AA
private void btnSync_Click(object sender, System.EventArgs e)
{
//When click this button, the BeginConnect method is called
//using the same thread, using this way the socket tries to
//connect and the print in the console (false)
BeginConnect();
}
private void btnAsync_Click(object sender, System.EventArgs e)
{
//When click this button, the BeginConnect method is called
//using other thread, using this way the socket tries to
//connect but print (true) in the console
System.Threading.Thread myThread = new System.Threading.Thread(new
System.Threading.ThreadStart(BeginConnect));
myThread.Start();
}
internal void BeginConnect()
{
Socket mSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
IPEndPoint mEndPoint = new IPEndPoint(IPAddress.Parse("10.0.0.0"), 8978);
mSocket.BeginConnect(mEndPoint, new AsyncCallback(ConnectCallBack),
mSocket);
}
private void ConnectCallBack(IAsyncResult ar)
{
Socket mSocket = (ar.AsyncState as Socket);
Console.WriteLine(mSocket.Connected);
}
don't know why?
This is my simple source code. The first method call the BeginConnect()
using the same windows form thread, and the second method call the
BeginConnect() using another thread
You will think... "Why you want to execute the BeginConnect using
another thread" But believe me that a really need
I really need your help
AA
private void btnSync_Click(object sender, System.EventArgs e)
{
//When click this button, the BeginConnect method is called
//using the same thread, using this way the socket tries to
//connect and the print in the console (false)
BeginConnect();
}
private void btnAsync_Click(object sender, System.EventArgs e)
{
//When click this button, the BeginConnect method is called
//using other thread, using this way the socket tries to
//connect but print (true) in the console
System.Threading.Thread myThread = new System.Threading.Thread(new
System.Threading.ThreadStart(BeginConnect));
myThread.Start();
}
internal void BeginConnect()
{
Socket mSocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
IPEndPoint mEndPoint = new IPEndPoint(IPAddress.Parse("10.0.0.0"), 8978);
mSocket.BeginConnect(mEndPoint, new AsyncCallback(ConnectCallBack),
mSocket);
}
private void ConnectCallBack(IAsyncResult ar)
{
Socket mSocket = (ar.AsyncState as Socket);
Console.WriteLine(mSocket.Connected);
}