viksnamax
@viksnamax
Aliens is here!

Как получить код завершения программы?

  1. Как получить код завершения приложения?
  2. Как можно аварийно закрыть приложение, что бы результат завершения приложения был не 0 или 1, а какая-то ошибка?
  3. Наверняка есть обработчик, который срабатывает при аварийном закрытии, каким образом он может выполнять код, если приложение закрылось?
  4. Где узнать значение каждого кода?
  • Вопрос задан
  • 384 просмотра
Пригласить эксперта
Ответы на вопрос 2
mindtester
@mindtester Куратор тега C#
http://iczin.su/hexagram_48
помедитировать 1
using System;

namespace ConsoleApp1
{
    class Program
    {
        static int Main(string[] args)
        {
              // по фиг что
              returnt <что то int>;
        }
    }
}
Ответ написан
firedragon
@firedragon
Не джун-мидл-сеньор, а трус-балбес-бывалый.
Вот примерный код будет ловить все, и даст вам 5 секунд что то сделать.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;

namespace TestTrapCtrlC{
    public class Program{
        static bool exitSystem = false;

        #region Trap application termination
        [DllImport("Kernel32")]
        private static extern bool SetConsoleCtrlHandler(EventHandler handler, bool add);

        private delegate bool EventHandler(CtrlType sig);
        static EventHandler _handler;

        enum CtrlType {
         CTRL_C_EVENT = 0,
         CTRL_BREAK_EVENT = 1,
         CTRL_CLOSE_EVENT = 2,
         CTRL_LOGOFF_EVENT = 5,
         CTRL_SHUTDOWN_EVENT = 6
         }

        private static bool Handler(CtrlType sig) {
            Console.WriteLine("Exiting system due to external CTRL-C, or process kill, or shutdown");

            //do your cleanup here
            Thread.Sleep(5000); //simulate some cleanup delay

            Console.WriteLine("Cleanup complete");

            //allow main to run off
             exitSystem = true;

            //shutdown right away so there are no lingering threads
            Environment.Exit(-1);

            return true;
        }
        #endregion

        static void Main(string[] args) {
            // Some biolerplate to react to close window event, CTRL-C, kill, etc
            _handler += new EventHandler(Handler);
            SetConsoleCtrlHandler(_handler, true);

            //start your multi threaded program here
            Program p = new Program();
            p.Start();

            //hold the console so it doesn’t run off the end
            while(!exitSystem) {
                Thread.Sleep(500);
            }
        }

        public void Start() {
            // start a thread and start doing some processing
            Console.WriteLine("Thread started, processing..");
        }
    }
 }
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы