1

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

}
  • 2
    Why should typed text halt the application ? – Marged Jul 13 '17 at 06:04
  • use Console.ReadKey(true) – boop_the_snoot Jul 13 '17 at 06:06
  • Instead of `Console.ReadLine();` why not use a `while(true)` ? – Suraj S Jul 13 '17 at 06:09
  • @Marged I want to make sure that the Console. Writeline message stay active on the Console Display, sometime the user may press Enter or any characters, which results interrupt the execution of the application. –  Jul 13 '17 at 06:09
  • 1
    Your approach is incorrect, it's not a good practice to attempt creating an "unstoppable" application in this way. For example, you tried to disable Close button, but Alt+F4 still closes the window. Consider developing windows service and save your output to the log file. – vasek Jul 13 '17 at 06:09
  • Reference this answer https://stackoverflow.com/a/32532767. I hope this might help you. – boop_the_snoot Jul 13 '17 at 06:09
  • 1
    Why a console app and not a Windows service or planned task? – Guillaume Jul 13 '17 at 06:10
  • @SurajS It works. Thanks –  Jul 13 '17 at 06:35
  • @Guillaume I will prepare upgrade this to Windows Service. Currently this is as per client's requirement. –  Jul 13 '17 at 08:55

3 Answers3

2

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.

Terry Lennox
  • 29,471
  • 5
  • 28
  • 40
1

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();
            }
    
        }
    
Kundan Bhati
  • 485
  • 1
  • 9
  • 19
0

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("");
 }
}
boop_the_snoot
  • 3,209
  • 4
  • 33
  • 44