异步等待多个Telnet服务器 [英] Async Await Multiple Telnet Servers

查看:110
本文介绍了异步等待多个Telnet服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我想如何使用async等待Telnet客户端在C#5.0上创建应用程序。



我的方案是我必须telnet多个服务器同时。另外,我需要它像ping / t函数一样连续检查服务器。



非常感谢.. :)

解决方案
为什么?创建一个单独的线程来处理每个单独的telnet服务器并同步工作。在需要的情况下,在极少数情况下使用线程同步来同步这些线程。



我认为异步API在线程不常见时会流行,或者仅仅因为许多人纯粹了解线程。毕竟,异步操作通常也基于线程,只是在引擎盖下。显式地使用线程更直接,并且提供更稳定的结果,因为1)您具有完全控制权,2)对于同步,您反复使用相同的线程同步技术,因为它们是通用的,而不是特定于应用程序的,3)in相反,异步API是特定于应用程序的,4)异步API只使用更复杂的使用逻辑,这是不太明显的。



-SA


 //假设您已创建CancellationToken,以便取消Telnet监视器。 

列表< task> telnetTasks = new List< task>();

for(var i = 0; i< NumTelnetSessions; I ++)
{
var monitor = new TelnetMonitor(telnetAddresses [i]);
telnetTasks.Add(monitor.Run(cancellationToken));
}

Task.WhenAll(telnetTasks.ToArray());

...
< / task>< / task>



您可以通过取消cancelToken来停止监视器。



TelnetMonitor.Run 是一个异步任务方法,用于监视等待异步发送,接收的循环中的指定服务器和延迟方法以及检查循环中的取消。


Hi guys, any idea on how am I suppose to create an application on C# 5.0 using async await Telnet client.

My scenario is that I have to telnet multiple servers at the same time. Also, i need it to check the servers continuously just like a ping /t function.

Thanks much.. :)

解决方案

Why?! Create a separate thread to work with each separate telnet server and work synchronously. Synchronize those threads using thread synchronization in those rare cases when it is needed.

I think asynchronous APIs get popularity when threads were not a commonplace, or just because many people purely understood threading. After all, asynchronous operations are often also based on threads, only under the hood. Using threads explicitly is way more straightforward and give more stable results, because 1) you have full control, 2) for synchronization, you use the same thread synchronization techniques over and over, as they are universal, not application-specific, 3) in contrast to that asynchronous APIs are application-specific, 4) asynchronous APIs just use more complex logic of their use, which is less obvious.

—SA


// assuming you have created a CancellationToken so you can cancel the Telnet Monitors.

List<task> telnetTasks = new List<task>();

for (var i = 0; i < NumTelnetSessions; I++)
{
    var monitor = new TelnetMonitor(telnetAddresses[i]);
    telnetTasks.Add(monitor.Run(cancellationToken));
}

Task.WhenAll(telnetTasks.ToArray());

...
</task></task>


You can stop the monitors by cancelling the cancellationToken.

TelnetMonitor.Run is an async Task method that monitors the specified server in a loop awaiting async Send, Receive and Delay methods and checking for cancellation in the loop.


这篇关于异步等待多个Telnet服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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