我正在构建一个运行在计时器上的控制台应用程序,它是一个调度程序SMS发送应用程序,方法包含在main方法中。如何确保用户无法输入任何字符,以便应用程序继续运行直到机器停止。我想一起查看有关应用程序执行的信息。我也禁用了关闭按钮。源代码如下:
public class Program
{
private const int MF_BYCOMMAND = 0x00000000;
public const int SC_CLOSE = 0xF060;
[DllImport("user32.dll")]
public static extern int DeleteMenu(IntPtr hMenu, int nPosition, int wFlags);
[DllImport("user32.dll")]
private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
[DllImport("kernel32.dll", ExactSpelling = true)]
private static extern IntPtr GetConsoleWindow();
static void Main(string[] args)
{
DeleteMenu(GetSystemMenu(GetConsoleWindow(), false), SC_CLOSE, MF_BYCOMMAND);
Timer t = new Timer(TimerCallback, null, 0, 5000);
Console.ReadLine();
}
private static void TimerCallback(Object o)
{
Console.WriteLine("SMS Banking Schedule: " + DateTime.Now);
DLLSendSMS dllSendSMS = new DLLSendSMS();
dllSendSMS.GetMessages(null);
GC.Collect();
}
}
答案 0 :(得分:2)
我建议为此目的构建Windows服务,您可以确保它将在计算机启动时启动(如果需要)并将在后台运行。您可以将信息记录到日志文件,事件日志等,以确保一切正常。
Visual Studio具有可轻松快速构建服务的模板。
答案 1 :(得分:1)
这个问题有两个解决方案。
如果您想更改现有代码,请尝试使用以下代码。我添加了while循环。
public class Program
{
private const int MF_BYCOMMAND = 0x00000000;
public const int SC_CLOSE = 0xF060;
[DllImport("user32.dll")]
public static extern int DeleteMenu(IntPtr hMenu, int nPosition, int wFlags);
[DllImport("user32.dll")]
private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
[DllImport("kernel32.dll", ExactSpelling = true)]
private static extern IntPtr GetConsoleWindow();
static void Main(string[] args)
{
DeleteMenu(GetSystemMenu(GetConsoleWindow(), false), SC_CLOSE, MF_BYCOMMAND);
Timer t = new Timer(TimerCallback, null, 0, 5000);
//Removed readline and added while loop (infinite)
while(true)
{
}
}
private static void TimerCallback(Object o)
{
Console.WriteLine("SMS Banking Schedule: " + DateTime.Now);
DLLSendSMS dllSendSMS = new DLLSendSMS();
dllSendSMS.GetMessages(null);
GC.Collect();
}
}
答案 2 :(得分:0)
参考此答案https://stackoverflow.com/a/32532767/6611487
class Writer
{
public void WriteLine(string myText)
{
for (int i = 0; i < myText.Length; i++)
{
if (Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Enter)
{
Console.Write(myText.Substring(i, myText.Length - i));
break;
}
Console.Write(myText[i]);
System.Threading.Thread.Sleep(pauseTime);
}
Console.WriteLine("");
}
}