Created
April 25, 2017 07:50
-
-
Save dron247/047c9159d478597d7123e958e4849b1d to your computer and use it in GitHub Desktop.
Simple example of the Application object like those in windows forms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Usage: | |
| public class Program { | |
| static void Main(string[] args) { | |
| var app = Application.Initialize(args); | |
| app.Start(); | |
| //THIS LINE IS UNREACHABLE DURING APP EXECUTION | |
| } | |
| } | |
| */ | |
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| using System.Threading; | |
| using System.Threading.Tasks; | |
| using System.Diagnostics; | |
| namespace Example { | |
| public class Application { | |
| private static Application instance = null; | |
| private static readonly object padlock = new object(); | |
| private static bool IsStarted = false; | |
| string[] CommandLineArgs; | |
| private static AutoResetEvent exitWaiter = new AutoResetEvent(false); | |
| /// <summary> | |
| /// Событие вызываемое, когда завершение разрешено | |
| /// </summary> | |
| public event EventHandler ApplicationExit; | |
| /// <summary> | |
| /// Доступ к объекту приложения | |
| /// </summary> | |
| public static Application Instance { | |
| get { | |
| lock (padlock) { | |
| return instance; | |
| } | |
| } | |
| } | |
| private Application(string[] args) { CommandLineArgs = args; } | |
| /// <summary> | |
| /// Конструктор объекта приложения | |
| /// </summary> | |
| /// <returns>pointer on application obj</returns> | |
| public static Application Initialize(string[] args) { | |
| lock (padlock) { | |
| if (instance == null) | |
| instance = new Application(args); | |
| return instance; | |
| } | |
| } | |
| /// <summary> | |
| /// Запускает приложение | |
| /// </summary> | |
| public void Start() {//Пуск приложения в работу с защитой от дурака | |
| bool isInitNeeded = false; | |
| lock (padlock) { | |
| if (!IsStarted) {//Проверяем запущено ли приложение | |
| IsStarted = true; | |
| isInitNeeded = true; | |
| } | |
| } | |
| if (isInitNeeded) { | |
| InitializeComponent();//Вызываем метод инициализации модулей | |
| exitWaiter.WaitOne();//Запрещаем приложению завершаться | |
| } | |
| } | |
| /// <summary> | |
| /// Посылает сигнал подсистемам о завершении приложения и разрешает ему закрыться | |
| /// </summary> | |
| public static void Exit() { | |
| Instance.OnApplicationExit();//Запускаем событие выхода из приложения | |
| exitWaiter.Set();//Разрешаем приложению завершиться | |
| } | |
| private void OnApplicationExit() { | |
| ApplicationExit?.Invoke(this, EventArgs.Empty); | |
| } | |
| #endregion | |
| /// <summary> | |
| /// В этом методе инициализируются подсистемы приложения | |
| /// </summary> | |
| private void InitializeComponent() { | |
| //по хорошему этот метод должен быть делегирован снаружи | |
| // тут запускаются все подсистемы приложения | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment