我有以下代码为WCF控制台托管应用程序提供自动刷新功能。如何防止以下代码中出现StackOverflow错误?
当Console.ReadKey接受无效字符时,它将重新启动ReadKey方法。如果用户在该代码上足够长的时间对键盘进行了篡改,它将进入StackOverflowException。
有没有人有办法重写这段代码,使它不会导致堆栈吹?
[STAThread]
static void Main(string[] args)
{
bool restart = true;
while(restart)
{
using (var myWcfHost = new MyWcfHost())
{
myWcfHost.start();
Console.WriteLine("Press Enter to quit or Ctrl+R to restart");
restart = WaitForRestart();
}
}
}
private static bool WaitForRestart()
{
// clear users input
Console.CursorLeft = 0;
Console.Write(' ');
Console.CursorLeft = 0;
// read users input
var key = Console.ReadKey();
if ((key.Modifiers & ConsoleModifiers.Control) != 0
&& key.Key == ConsoleKey.R)
{
// refersh the settings
ConfigurationManager.RefreshSection("appSettings");
return true;
}
if (key.Key == ConsoleKey.Enter || key.Key == ConsoleKey.Escape)
{
return false;
}
return WaitForRestart();
}
我一直在盯着这永远,最后决定发布,我发布后我想到了这个权利。谢谢,这工作。 – bendewey 2009-04-27 01:15:15