简体   繁体   English

如何在C#控制台应用程序中禁用用户输入?

[英]How to disable user input in C# Console Application?

I am building a console application which runs on timer, it is a scheduler SMS sending application the methods are included in main method. 我正在构建一个运行在计时器上的控制台应用程序,它是一个调度程序SMS发送应用程序,方法包含在main方法中。 How can i make sure that user can't input any character so that the application continues until the machine stops. 如何确保用户无法输入任何字符,以便应用程序继续运行直到机器停止。 Together i want to view the Information regarding the execution of application. 我想一起查看有关应用程序执行的信息。 I have disabled the close button too. 我也禁用了关闭按钮。 The source code is as follows: 源代码如下:

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();
    }

}

I'd suggest building a Windows Service for this purpose, you can ensure it will start (if desired) on machine startup and will run in the background. 我建议为此目的构建Windows服务,您可以确保它将在计算机启动时启动(如果需要)并将在后台运行。 You can log information to log files, the Event log etc. to ensure everything is working correctly. 您可以将信息记录到日志文件,事件日志等,以确保一切正常。

Visual Studio has templates for services that make is very easy to build one quickly. Visual Studio具有可轻松快速构建服务的模板。

There are two solutions of this problem. 这个问题有两个解决方案。

  1. When you want something to run continuously you should create windows service. 当你想要连续运行时,你应该创建Windows服务。
  2. if you want to change your existing code then try following code. 如果您想更改现有代码,请尝试以下代码。 I have added while loop. 我添加了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(); } } 

Reference this answer https://stackoverflow.com/a/32532767/6611487 参考这个答案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("");
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM