Как вызвать из метода в первом потоке, метод который во втором потоке?
Код:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;
using System.Threading;
namespace сервер_типа
{
class Program
{
public delegate void Mess();
public static event Mess message;
private static void CheckThread()
{
Console.WriteLine("привет из чек тред: " + Thread.CurrentThread.ManagedThreadId);
}
private static byte[] buff = new byte[1024];
private static Socket client;
static void Main(string[] args)
{
message += new Mess(CheckThread);
Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
listener.Bind(new IPEndPoint(IPAddress.Any, 9095));
listener.Listen(10);
client = listener.Accept();
Console.WriteLine("подключился чел");
Console.WriteLine("привет из main: " + Thread.CurrentThread.ManagedThreadId);
client.BeginReceive(buff, 0, buff.Length, 0, new AsyncCallback(ReceiveCallback), client);
Console.Read();
}
private static void SendCallback(IAsyncResult ar)
{
Socket sck = (Socket)ar.AsyncState;
int i = sck.EndSend(ar);
client.BeginReceive(buff, 0, buff.Length, 0, new AsyncCallback(ReceiveCallback), client);
}
private static void ReceiveCallback(IAsyncResult ar)
{
try
{
Socket sck = (Socket)ar.AsyncState;
int i = sck.EndReceive(ar);
Console.WriteLine("получили " + Encoding.UTF8.GetString(buff, 0, i));
sck.BeginSend(buff, 0, i, 0, new AsyncCallback(SendCallback), sck);
Console.WriteLine("отправили " + Encoding.UTF8.GetString(buff, 0, i));
Console.WriteLine("привет из ReceiveCallback: " + Thread.CurrentThread.ManagedThreadId);
message();
}
catch (Exception ex)
{
client.Close();
Console.WriteLine("Клиент отключился");
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;
using System.Threading;
namespace сервер_типа
{
class Program
{
public delegate void Mess();
public static event Mess message;
private static void CheckThread()
{
Console.WriteLine("привет из чек тред: " + Thread.CurrentThread.ManagedThreadId);
}
private static byte[] buff = new byte[1024];
private static Socket client;
static void Main(string[] args)
{
message += new Mess(CheckThread);
Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
listener.Bind(new IPEndPoint(IPAddress.Any, 9095));
listener.Listen(10);
client = listener.Accept();
Console.WriteLine("подключился чел");
Console.WriteLine("привет из main: " + Thread.CurrentThread.ManagedThreadId);
client.BeginReceive(buff, 0, buff.Length, 0, new AsyncCallback(ReceiveCallback), client);
Console.Read();
}
private static void SendCallback(IAsyncResult ar)
{
Socket sck = (Socket)ar.AsyncState;
int i = sck.EndSend(ar);
client.BeginReceive(buff, 0, buff.Length, 0, new AsyncCallback(ReceiveCallback), client);
}
private static void ReceiveCallback(IAsyncResult ar)
{
try
{
Socket sck = (Socket)ar.AsyncState;
int i = sck.EndReceive(ar);
Console.WriteLine("получили " + Encoding.UTF8.GetString(buff, 0, i));
sck.BeginSend(buff, 0, i, 0, new AsyncCallback(SendCallback), sck);
Console.WriteLine("отправили " + Encoding.UTF8.GetString(buff, 0, i));
Console.WriteLine("привет из ReceiveCallback: " + Thread.CurrentThread.ManagedThreadId);
message();
}
catch (Exception ex)
{
client.Close();
Console.WriteLine("Клиент отключился");
}
}
}
}