アプリケーションの終了条件を定義する

注意:
この文書は以前「.NETでいきまっしょい!」で公開していたものですが、公開以降メンテナンスされていません。 今や古い情報となった内容が記載されている場合があるのでご注意ください。
ApplicationContextクラスを継承することで、アプリケーションの終了条件を定義する事ができる。 アプリケーションを終了する場合には、ExitThread()メソッドを呼び出す。 また、このクラスのインスタンスを用いてアプリケーションを開始することにより、ウィンドウを持たないアプリケーションでもメッセージループを持つことができる。
この例ではApplicationContextを継承したCalculatingApplicationというクラスを作成し、アプリケーションを起動している。 また、計算が終了した時点でクラス内からExitThread()メソッドを呼び出している。 これが呼び出された時点でアプリケーションが終了する。
VB.NET
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
Public Class CalculatingApplication

    Inherits System.Windows.Forms.ApplicationContext

    Public Sub New()

        MyBase.New()

    End Sub

    Public Sub Start()

        ' 計算を行うスレッドを起動する
        Dim thread As New System.Threading.Thread(AddressOf Calculate)

        thread.Start()

    End Sub

    Private Sub Calculate()

        Console.WriteLine("計算を開始します。")

        Dim count As Integer = 0

        Do

            count += 1

            System.Threading.Thread.Sleep(50)

        Loop Until (100 < count)

        ' スレッドのメッセージループを終了する
        MyClass.ExitThread()

        Console.WriteLine("計算が終了しました。")

    End Sub

    Protected Overrides Sub ExitThreadCore()

        Console.WriteLine("スレッドのメッセージループを終了します。")

        MyBase.ExitThreadCore()

    End Sub

End Class

Public Class MainClass

    Public Shared Sub Main()

        Dim app As New CalculatingApplication

        ' 計算を開始する
        app.Start()

        Console.WriteLine("現在のスレッドでメッセージループが開始されます。")

        ' CalculatingApplicationクラスのインスタンスを使用してアプリケーションを起動する
        System.Windows.Forms.Application.Run(app)

        Console.WriteLine("スレッドのメッセージループは終了しました。")

    End Sub

End Class
C#
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
public class CalculatingApplication : System.Windows.Forms.ApplicationContext
{
    public CalculatingApplication() : base() {}

    public void Start()
    {
        // 計算を行うスレッドを起動する
        System.Threading.Thread thread = new System.Threading.Thread( new System.Threading.ThreadStart( Calculate ) );

        thread.Start();
    }

    private void Calculate()
    {
        Console.WriteLine( "計算を開始します。" );

        int count = 0;

        do
        {
            count += 1;

            System.Threading.Thread.Sleep( 50 );

        } while ( !( 100 < count ) );

        // スレッドのメッセージループを終了する
        this.ExitThread();

        Console.WriteLine( "計算が終了しました。" );
    }

    protected override void ExitThreadCore()
    {
        Console.WriteLine( "スレッドのメッセージループを終了します。" );

        base.ExitThreadCore();
    }
}

public class MainClass
{
    public static void Main()
    {
        CalculatingApplication app = new CalculatingApplication();

        // 計算を開始する
        app.Start();

        Console.WriteLine( "現在のスレッドでメッセージループが開始されます。" );

        // CalculatingApplicationクラスのインスタンスを使用してアプリケーションを起動する
        System.Windows.Forms.Application.Run( app );

        Console.WriteLine( "スレッドのメッセージループは終了しました。" );
    }
}
出力結果
現在のスレッドでメッセージループが開始されます。
計算を開始します。
スレッドのメッセージループを終了します。
スレッドのメッセージループは終了しました。
計算が終了しました。
Press any key to continue