user5136052
user5136052

Reputation:

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

}

Upvotes: 1

Views: 2909

Answers (3)

Kundan Bhati
Kundan Bhati

Reputation: 505

There are two solutions of this problem.

  1. When you want something to run continuously you should create windows service.
  2. if you want to change your existing code then try following code. I have added while loop.

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

Upvotes: 1

boop_the_snoot
boop_the_snoot

Reputation: 3257

Reference this answer 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("");
 }
}

Upvotes: 0

Terry Lennox
Terry Lennox

Reputation: 30715

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. 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.

Upvotes: 2

Related Questions