如和当我输入Ctrl+c时停止我的循环? 我编了一个ping软件,现在只能一直循环ping下去,如何实现一直检测输入,当我输入Ctrl+c或者别的什么时停止循环,显示结果。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这样看你的程序是什么类型的,如果是WinForm的,可以处理窗体的消息,如果控制能项目可以读取Console.ReadLine的值。 应该是个多线程的程序,程序运行后另起一个线程循环ping,当主线程中输入Ctrl+c时结束循环ping线程,也是新手,不知说的对不对, 在你的循环里设置一个信号量,通过控制信号量来控制是否开始停止,给你个实例代码。 public sealed class MultiThreadWorker { bool _threadSwitch = false; bool _pauseSwitch = false; ManualResetEvent _resetEvent = new ManualResetEvent(false); Thread[] _workers = null; readonly object _locker = new object(); int _intervalMillisconds = 0; public delegate bool ThreadWorkingEentHander(object sender, EventArgs e); public event ThreadWorkingEentHander OnThreadWorking; readonly EventArgs args = new EventArgs(); private bool RaiseThreadWorking() { if (OnThreadWorking != null) { return OnThreadWorking(this, args); } return false; } public MultiThreadWorker(Func<object,EventArgs,bool> eventHandler) { this.OnThreadWorking += new ThreadWorkingEentHander(eventHandler); } public void Start(int threadCount,int intervalMillisconds) { _intervalMillisconds = intervalMillisconds; lock(_locker) { if (_workers == null) { _threadSwitch = true; _workers = new Thread[threadCount]; for(int i = 0;i < _workers.Length; i ++) { _workers[i] = new Thread(Run); _workers[i].IsBackground = true; _workers[i].Name = i.ToString(); _workers[i].Start(); } } } } void Run() { while (_threadSwitch) { int timestamp = 0; try { timestamp = System.Environment.TickCount; if (!RaiseThreadWorking()) { break; } if (_pauseSwitch) { _resetEvent.WaitOne(); } } catch (System.Exception ex) { LogManager.WriteError(ex); } int elapsedTickCount = Environment.TickCount - timestamp; int sleepInterval = _intervalMillisconds - elapsedTickCount; sleepInterval = sleepInterval > 0 ? sleepInterval : 0; Thread.Sleep(sleepInterval); } } public void Continue() { lock (_locker) { if (!_pauseSwitch) { return; } _pauseSwitch = false; _resetEvent.Set(); } } public void Pause() { lock (_locker) { if (_pauseSwitch) { return; } _resetEvent.Reset(); _pauseSwitch = true; } } public void Stop() { lock (_locker) { if (_workers == null) { return; } _threadSwitch = false; _pauseSwitch = false; _resetEvent.Set(); for (int i = 0; i < _workers.Length; i++) { if (_workers[i].IsAlive) { try { _workers[i].Abort(); } catch { } } } _workers = null; } } public static void RuntimeWatch(Action action,ref int totalTime) {#if DEBUG int timeStamp = Environment.TickCount;#endif action();#if DEBUG timeStamp = Environment.TickCount - timeStamp; LogManager.WriteDebug("Runtime:" + timeStamp.ToString()); Interlocked.Add(ref totalTime, timeStamp);#endif } }你要终止调用Pause方法就可以了但你要知道怎样界面能接受Ctrl的命令是console还是winform? 那就用我这个类就可以了,把这个类作为一个静态变量用Console.ReadKey()来读取你输入的字符如果是ctl,就调用静态变量的Pause()方法就行了,如果继续就调用Continue就ok。 using System;using System.Collections.Generic;using System.Text;namespace ConsoleApplication2{ class Program { static void Main(string[] args) { while (true) { if (Console.KeyAvailable) { ConsoleKeyInfo input = System.Console.ReadKey(false); if (input.Modifiers == ConsoleModifiers.Control && input.Key == ConsoleKey.C) break; } Console.WriteLine("test"); System.Threading.Thread.Sleep(1000); } } }} 我觉得定义一个全局变量(bool isgo = true),如果isgo==true就循环,在按ctrl+C时触发事件把isgo赋值成false ,循环退出。只是怎么想的,没去写代码。只是给点建议。 class Program { static MultiThreadWorker _worker = new MultiThreadWorker(Test); static void Main(string[] args) { _worker.Start(1,1); while (true) { ConsoleKeyInfo input = System.Console.ReadKey(false); if (Console.KeyAvailable) { if (input.Modifiers == ConsoleModifiers.Control && input.Key == ConsoleKey.C){_worker.Stop(); break;} } if(input.Char.Equals('p')){ _worker.Pause();}if(input.Char.Equals('g')){_work.Continue();} } } static bool Test(object obj,EventArgs e){//send ping cmd} } 关于水晶报表问题 鼠标右击问题 -- 急问一个关于网页重定向的问题 C#-- 两台服务器一台北京一台上海,数据如何完全同步? 为什么这样转换就是十六进制 关于XPATH 查询问题 做象棋游戏 棋子载入用什么控件好picturebox数组?imagelist?还是listview啊? 求助:c#窗体应用程序如何响应系统服务的启动或控制请求 c# c\s结构 menuitem隐藏显示菜单项的问题 C# 开发SIP 客户端发送短信 求一个正则表达式... 如何给运行中的程序传参
也是新手,不知说的对不对,
public sealed class MultiThreadWorker
{
bool _threadSwitch = false;
bool _pauseSwitch = false;
ManualResetEvent _resetEvent = new ManualResetEvent(false);
Thread[] _workers = null;
readonly object _locker = new object();
int _intervalMillisconds = 0; public delegate bool ThreadWorkingEentHander(object sender, EventArgs e); public event ThreadWorkingEentHander OnThreadWorking;
readonly EventArgs args = new EventArgs(); private bool RaiseThreadWorking()
{
if (OnThreadWorking != null)
{
return OnThreadWorking(this, args);
}
return false;
} public MultiThreadWorker(Func<object,EventArgs,bool> eventHandler)
{
this.OnThreadWorking += new ThreadWorkingEentHander(eventHandler);
}
public void Start(int threadCount,int intervalMillisconds)
{
_intervalMillisconds = intervalMillisconds;
lock(_locker)
{
if (_workers == null)
{
_threadSwitch = true;
_workers = new Thread[threadCount];
for(int i = 0;i < _workers.Length; i ++)
{
_workers[i] = new Thread(Run);
_workers[i].IsBackground = true;
_workers[i].Name = i.ToString();
_workers[i].Start();
}
}
} } void Run()
{
while (_threadSwitch)
{
int timestamp = 0;
try
{
timestamp = System.Environment.TickCount;
if (!RaiseThreadWorking())
{
break;
}
if (_pauseSwitch)
{
_resetEvent.WaitOne();
}
}
catch (System.Exception ex)
{
LogManager.WriteError(ex);
}
int elapsedTickCount = Environment.TickCount - timestamp;
int sleepInterval = _intervalMillisconds - elapsedTickCount;
sleepInterval = sleepInterval > 0 ? sleepInterval : 0;
Thread.Sleep(sleepInterval);
}
} public void Continue()
{
lock (_locker)
{
if (!_pauseSwitch)
{
return;
}
_pauseSwitch = false;
_resetEvent.Set();
}
} public void Pause()
{
lock (_locker)
{
if (_pauseSwitch)
{
return;
}
_resetEvent.Reset();
_pauseSwitch = true;
}
} public void Stop()
{
lock (_locker)
{
if (_workers == null)
{
return;
}
_threadSwitch = false;
_pauseSwitch = false;
_resetEvent.Set();
for (int i = 0; i < _workers.Length; i++)
{
if (_workers[i].IsAlive)
{
try
{
_workers[i].Abort();
}
catch
{
}
}
}
_workers = null;
}
} public static void RuntimeWatch(Action action,ref int totalTime)
{
#if DEBUG
int timeStamp = Environment.TickCount;
#endif
action();
#if DEBUG
timeStamp = Environment.TickCount - timeStamp;
LogManager.WriteDebug("Runtime:" + timeStamp.ToString());
Interlocked.Add(ref totalTime, timeStamp);
#endif
}
}你要终止调用Pause方法就可以了但你要知道怎样界面能接受Ctrl的命令
是console还是winform?
如果是ctl,就调用静态变量的Pause()方法就行了,如果继续就调用Continue就ok。
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
while (true)
{
if (Console.KeyAvailable)
{
ConsoleKeyInfo input = System.Console.ReadKey(false);
if (input.Modifiers == ConsoleModifiers.Control && input.Key == ConsoleKey.C)
break;
}
Console.WriteLine("test");
System.Threading.Thread.Sleep(1000);
}
}
}
}
定义一个全局变量(bool isgo = true),如果isgo==true就循环,
在按ctrl+C时触发事件把isgo赋值成false ,循环退出。只是怎么想的,没去写代码。只是给点建议。
{
static MultiThreadWorker _worker = new MultiThreadWorker(Test);
static void Main(string[] args)
{
_worker.Start(1,1);
while (true)
{
ConsoleKeyInfo input = System.Console.ReadKey(false);
if (Console.KeyAvailable)
{ if (input.Modifiers == ConsoleModifiers.Control && input.Key == ConsoleKey.C)
{
_worker.Stop();
break;
}
}
if(input.Char.Equals('p'))
{
_worker.Pause();
}
if(input.Char.Equals('g'))
{
_work.Continue();
}
}
}
static bool Test(object obj,EventArgs e)
{
//send ping cmd
}
}