본문 바로가기
C#(CSharp)/WPF

WPF 프로그램 UI 없이 자동으로 종료 되기

by swconsulting 2015. 3. 21.

어떤 경우에 WPF프로그램이 화면이 안나오고 로직을 수행후 바로 종료되어야 하는 경우가 있습니다.


예를 들어 특정 파일 생성 및 기록 또는 레지스트리에 값을 쓰는 경우 등입니다. 


이럴경우에 간단하게 사용하는 방법입니다. Linux 쉘 프로그램과 비슷하다고 생각하면 됩니다.



[App.xaml]

<Application x:Class="WpfApplication1.App"

             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

             >

             <!--StartupUri="MainWindow.xaml">-->

    <Application.Resources>

        

    </Application.Resources>

 

</Application>


[App.xaml.cs]

using System;

using System.Collections.Generic;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Threading.Tasks;

using System.Windows;

 

namespace WpfApplication1

{

    /// <summary>

    /// Interaction logic for App.xaml

    /// </summary>

    public partial class App : Application

    {

        protected override void OnStartup(StartupEventArgs e)

        {           

            base.OnStartup(e);

 

            //My Code here

            //Do something Task

 

            //This Program close(exit) automatically

            Environment.Exit(0);

        }

    }

}

 


출처 : 다년간의 프로그램밍 경험