C# 在C语言中,如何阻止一个线程直到另一个线程得到服务器响应#
我正在开发一个库,您可以在其中使用以下名称创建服务器:C# 在C语言中,如何阻止一个线程直到另一个线程得到服务器响应#,c#,multithreading,asynchronous,synchronous,C#,Multithreading,Asynchronous,Synchronous,我正在开发一个库,您可以在其中使用以下名称创建服务器: UtilityServer server = new UtilityServer("TestServer1", 3500); UtilityClient client = new UtilityClient("TestClient1"); 您还可以使用以下名称创建客户端: UtilityServer server = new UtilityServer("TestServer1", 3500); UtilityClient clien
UtilityServer server = new UtilityServer("TestServer1", 3500);
UtilityClient client = new UtilityClient("TestClient1");
您还可以使用以下名称创建客户端:
UtilityServer server = new UtilityServer("TestServer1", 3500);
UtilityClient client = new UtilityClient("TestClient1");
服务器使用您选择的端口在本地计算机上创建套接字。服务器运行时,客户端可以通过以下方式连接:
client.Connect(ServerIP, 3500);
当我基于事件进行操作时,一切正常,例如:
client.UserNamesArrived += OnUserNamesArrived;
client.RequestUserNames();
private void OnUserNamesArrived(ReadOnlyCollection<string> userNames)
{
// Do something with userNames
}
public ReadOnlyCollection<string> GetUserNames()
{
// Request userNames from server
// Wait until server sent us userNames
// return userNames
}
// Running on seperate thread, gets messages that server sents to client
ReceiveMessages()
{
while(_isConnected)
{
// Waits until message is received (_strReader.ReadLine())
// Looks what message contains (Can also be other things then userNames)
// Gets collection of userNames when message contains it (Working)
// Triggers event 'UserNamesArrived' with userNames as argument
}
}
sqlCommand.ExecuteNonQuery();
这也会阻止它并等待响应,但如何使用?您可以使用.NET framework的async和Wait属性。您需要将
ReceiveMessages()
方法设置为异步任务,并使运行该方法的主线程ReadOnlyCollection GetUserNames()
等待ReceiveMessages()
完成。。。我想它会起作用的
public async ReadOnlyCollection<string> GetUserNames()
{
// Request userNames from server
List<UserNames> userNameList = await ReceiveMessages();
// return userNames
}
// Running on seperate thread, gets messages that server sents to client
async Task<List<UserNames>> ReceiveMessages()
{
// Looks what message contains
// Gets collection of userNames when requested (Working)
// Triggers event 'UserNamesArrived' with userNames as argument
}
public async ReadOnlyCollection GetUserNames()
{
//从服务器请求用户名
List userNameList=等待接收消息();
//返回用户名
}
//在单独的线程上运行,获取服务器发送给客户端的消息
异步任务接收消息()
{
//查看消息包含的内容
//获取请求时的用户名集合(工作)
//以用户名作为参数触发事件“UserNamesArrived”
}
看看这个例子:考虑的一个实现 我经常使用
AutoResetEvent
进行需要阻塞的跨线程通知
AutoResteEvent允许线程通过以下方式相互通信:
信号。通常,在线程需要独占时使用此类
对资源的访问
由于信号的性质,您经常会听到这种结构称为信号量。WaitHandles是操作系统的一项功能,因此请确保正确处理该句柄
WaitHandle
的显著特点是WaitOne
方法
WaitOne阻塞当前线程,直到当前WaitHandle接收到信号
能否同步查询数据源以检索消息?否,我只能接收来自“ReceiveMessages”的消息。thread.join()可能会有所帮助,它会阻止调用线程,直到线程终止。作为旁注,它通常是*方法上的
,如OnUserNamesArrived
,引发事件而不是处理它们的。你把会议弄错了。你是在图书馆工作还是在图书馆工作?它是第三方库还是你的?“ReceiveMessages”已经在一个单独的线程上运行,我处理在那里收到的所有消息(不仅仅是用户名)。它在while循环中运行。我知道,将ReceiveMessages()方法设置为异步任务,返回用户名列表,并使其在GetUserNames()方法中等待。我更新了我的帖子,向您展示了代码……问题是我不能只调用这个方法,因为我的独立线程已经这样做了,并且得到了消息。另外,它是在一个while循环中。我将'\u resetEvent.WaitOne()'放在我的'GetsUserNames'方法中,将'\u resetEvent.Set()'放在我的'ReceivedMessages'方法中,它工作了@伯恩顿很高兴听到这个消息!