C# と VB.NET の質問掲示板

わんくま同盟

ASP.NET、C++/CLI、Java 何でもどうぞ

C# と VB.NET の入門サイト


(過去ログ 108 を表示中)
■64170 / )  Re[11]: AppDomainのアンロードタイミング
□投稿者/ SS (5回)-(2012/11/09(Fri) 17:00:01)
とっちゃん さん

"o" の件、分かりました。 ありがとうございます。
まさか第二引数だとは…、やはり理解出来てませんでしたね。お恥ずかしい。


ソースですが…、長くてすいません。

Public Class DLLCommon
    'イベント
    ' フォームのクローズ
    Public Delegate Sub DllFrmClose()
    ' DLLの終了
    Public Delegate Sub DllEnd(ByVal dmin As Integer)


    'インターフェース
    Public Interface IDll
        ReadOnly Property SyncContxt As System.Threading.SynchronizationContext
        Event MyEnd As DLLCommon.DllEnd
        Sub Start(ByVal PG As PGInfo)
    End Interface
End Class


'呼び出しDLL (HogeDLLプロジェクト) DLLCommon参照
Public Class HogeDLL
    Inherits MarshalByRefObject
    Implements DLLCommon.IDll

    'SynchronizationContextプロパティ
    Private _SynchronizationContext As System.Threading.SynchronizationContext = Nothing

    Public ReadOnly Property SyncContext As System.Threading.SynchronizationContext _
        Implements DLLCommon.IDll.SyncContxt
        Get
            Return Me._SynchronizationContext
        End Get
    End Property

    'メソッド
    Public Sub Start() Implements DLLCommon.IDll.Start
        Me._SynchronizationContext = System.Threading.SynchronizationContext.Current
→→    ※ ここで右辺の SynchronizationContext.Current が Nothing でした。

        'Formクラスのインスタンスを作成する
        Dim frm As New Form1()
        AddHandler frm.MyClose, AddressOf FrmClose

        'モードレスフォームとして表示する
        frm.Show()

    End Sub

    'FormClosedイベント 取得
    Public Sub FrmClose()
        RaiseEvent MyEnd(AppDomain.CurrentDomain.Id)
    End Sub

    'DLL終了イベント定義
    Public Event MyEnd As DLLCommon.DllEnd Implements DLLCommon.IDll.MyEnd
End Class


'DLLを呼ぶ側 (HogeExeプロジェクト) DLLCommon参照
Public Class HogeExe
    'AppDomain 保持用クラス
    Private Class domainTemp
        Public _AppDomain As AppDomain
        Public _SynchroContxt As System.Threading.SynchronizationContext
    End Class
    Private _AppDomainTemp As domainTemp = Nothing

    '実処理
    Private Sub DllExecute()
        Try
            Dim apd As AppDomain = AppDomain.CreateDomain(HogeDLL)
            Dim dllI As DLLCommon.IDll _
                = CType(apd.CreateInstanceAndUnwrap("HogeDLL", "HogeDLL.HogeDLL"), DLLCommon.IDll)
            AddHandler dllI.MyEnd, AddressOf Dll_End

            dllI.Start()

            If Me._AppDomainTemp Is Nothing Then Me._AppDomainTemp = New domainTemp()
            Me._AppDomainTemp._AppDomain = apd
            Me._AppDomainTemp._SynchroContxt = dllI.SyncContxt
        Catch

        End Try
    End Sub

    'DLL終了イベント
    Private Sub Dll_End(ByVal dmin As Integer)
        Try
            'AppDomain.Unload(Me._AppDomainTemp._AppDomain)
            Me._AppDomainTemp._SynchroContxt.Post(Sub(o) AppDomain.Unload(Me._AppDomainTemp._AppDomain), Nothing)
        Catch ex As Exception

        End Try
    End Sub
End Class

返信 編集キー/


管理者用

- Child Tree -