在线程中发送消息 [英] Send message in a thread

查看:114
本文介绍了在线程中发送消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在从线程中的TCP连接读取信息,并在构建JSON消息后将其发送到REST服务器。

将消息发送到我的应用程序需要时间很慢

在线程中发送JSON消息的最佳方法是什么?

我不知道每次发送消息时是否可以创建新帖子...



我是什么尝试过:



  private   void  PluginReceiveClass_OnMessageReceived(Model.Message e)
{
foreach (插件插件 in loadedPlugins_)
{
if (plugin.PluginSendClass!= null
{
if (plugin.IsSendStarted)
{
plugin.PluginSendClass。 SendMessage消息(E);
}
}
}
}

解决方案

尝试使用任务

 ... 
Task.Factory.StartNew(()= > plugin.PluginSendClass.SendMessage(e));
...


这是解决方案1之外的另一种解决方案。

用户 ThreadPool 如下所示。

 ThreadPool.QueueUserWorkItem(o = >  plugin.PluginSendClass.SendMessage(E)); 


Hi,

I'm reading the information from a TCP connection in a thread and after I'm building a JSON message to send it to a REST server.
It takes time to send the message to my application is slow
What's the best way to send the JSON message in a thread ?
I don't know if I can create a new thread each time I send a message...

What I have tried:

private void PluginReceiveClass_OnMessageReceived(Model.Message e)
       {
           foreach (Plugin plugin in loadedPlugins_)
           {
               if (plugin.PluginSendClass != null)
               {
                   if (plugin.IsSendStarted)
                   {
                       plugin.PluginSendClass.SendMessage(e);
                   }
               }
           }
       }

解决方案

Try using Task :

...
Task.Factory.StartNew( () => plugin.PluginSendClass.SendMessage(e) );
...


This is another solution in addition to Solution 1.
User ThreadPool as shown below.

ThreadPool.QueueUserWorkItem(o => plugin.PluginSendClass.SendMessage(e));


这篇关于在线程中发送消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆