编写一个多线程的委托消息应用场景
要求有三次以上的消息连接
要写一下设计场景的思路,用文字描述
要源代码
思路:
1、定义一个委托类型,用于封装要在不同线程之间传递的消息。例如:
public delegate void MessageDelegate(string message);
2、在主线程中,定义一个方法来处理消息。例如:
void HandleMessage(string message)
{
Console.WriteLine("Received message: " + message);
}
3、在主线程中,使用委托来将消息传递给其他线程。例如:
MessageDelegate del = new MessageDelegate(HandleMessage);
// Pass the message to another thread
Thread thread = new Thread(() => del("Hello from another thread!"));
thread.Start();
4、在其他线程中,使用委托来调用主线程中的方法。例如:
del("Hello from another thread!");
5、为了支持多个消息之间的连接,我们可以使用一个消息队列来维护所有消息。例如:
using System.Collections.Concurrent;
// Define a concurrent queue to hold the messages
ConcurrentQueue<string> messageQueue = new ConcurrentQueue<string>();
// Add a message to the queue
messageQueue.Enqueue("Hello from another thread!");
// Process the messages in a loop
while (true)
{
// Check if there are any messages in the queue
if (messageQueue.TryDequeue(out string message))
{
// Call the delegate to handle the message
del(message);
}
}
6、为了防止消息队列中的消息过多导致系统负载过高,我们还可以使用线程池来限制最大并发线程数。例如:
using System.Threading;
// Create a thread pool with a maximum of 3 concurrent threads
ThreadPool.SetMaxThreads(3, 3);
// Process the messages in a loop
while (true)
{
// Check if there are any messages in the queue
if (messageQueue.TryDequeue(out string message))
{
// Call the delegate to handle the message using a thread from the thread pool
ThreadPool.QueueUserWorkItem(new WaitCallback(del), message);
}
}
代码示例:
using System;
using System.Collections.Concurrent;
using System.Threading;
namespace DelegateThreadExample
{
public delegate void MessageDelegate(string message);
class Program
{
static void Main(string[] args)
{
// Define a concurrent queue to hold the messages
ConcurrentQueue<string> messageQueue = new ConcurrentQueue<string>();
// Define the method to be called by the delegate
void HandleMessage(string message)
{
Console.WriteLine("Received message: " + message);
}
// Create the delegate
MessageDelegate del = new MessageDelegate(HandleMessage);
// Create a semaphore with a maximum count of 3
Semaphore semaphore = new Semaphore(3, 3);
// Process the messages in a loop
while (true)
{
// Check if there are any messages in the queue
if (messageQueue.TryDequeue(out string message))
{
// Wait for a semaphore slot to become available
semaphore.WaitOne();
// Call the delegate to handle the message using a thread from the thread pool
ThreadPool.QueueUserWorkItem(new WaitCallback(del), message);
// Release the semaphore slot
semaphore.Release();
}
}
}
}
}
c# 委托实现多线程的实例
可以借鉴下
https://blog.csdn.net/u014677855/article/details/82383522
不知道你对于这块知识理解的如何,这里提供的是基本实例,简单易懂【【C# 委托多线程简单应用】】,链接:https://blog.csdn.net/oDJMoney/article/details/126413302
场景:
假设我们有一个远程协助服务的应用,可以接受客户的连接请求,并能通过消息委托的方式帮助客户解决问题。由于客户会有很多,所以我们需要使用多线程来支持多个客户的连接。
思路:
(1)定义一个委托类型,用于封装消息传递的方法。
(2)定义一个消息处理类,用于接收客户的消息并通过委托进行处理。
(3)使用多线程来支持多个客户的连接,每个客户对应一个线程。
(4)在主线程中开启监听器,接受客户的连接请求。
(5)当收到客户的连接请求时,创建一个新的线程来处理该客户的消息。
(6)在新线程中,创建消息处理类的实例,并调用委托进行消息处理。
源代码:
// 定义委托类型
delegate void MessageHandler(string message);
// 消息处理类
class MessageProcessor
{
// 委托实例
private MessageHandler handler;
// 消息队列
private ConcurrentQueue<string> messageQueue;
// 构造函数,用于注册委托
public MessageProcessor(MessageHandler handler)
{
this.handler = handler;
this.messageQueue = new ConcurrentQueue<string>();
}
// 接收客户消息并进行处理
public void ProcessMessage(string message)
{
// 将消息存入消息队列
messageQueue.Enqueue(message);
// 调用委托进行处理
handler(message);
}
// 获取消息队列中的所有消息
public List<string> GetAllMessages()
{
List<string> messages = new List<string>();
while (messageQueue.TryDequeue(out string message))
{
messages.Add(message);
}
return messages;
}
}
// 主线程
static void Main(string[] args)
{
// 开启监听器,接受客户的连接请求
TcpListener listener = new TcpListener(IPAddress.Any, 8080);
listener.Start();
// 创建字典,用于存储每个客户的消息处理类
Dictionary<int, MessageProcessor> processors = new Dictionary<int, MessageProcessor>();
while (true)
{
// 接受客户连接
TcpClient client = listener.AcceptTcpClient();
int clientId = client.GetHashCode();
// 创建新线程来处理客户消息
Thread thread = new Thread(() =>
{
// 创建消息处理类的实例
MessageProcessor processor = new MessageProcessor(HandleMessage);
processors[clientId] = processor;
// 循环接收客户消息并进行处理
while (true)
{
processor.ProcessMessage(client.ReceiveMessage());
}
});
// 启动线程
thread.Start();
}
}
// 消息处理方法
static void HandleMessage(string message)
{
// 处理客户消息
Console.WriteLine("Received message: " + message);
}
直接用 action. 和 func
参考一下https://blog.csdn.net/u014677855/article/details/82383522