views:

36

answers:

0

I have a problem with a solution that I'm trying to develope. This is my scenario: I have a VB6 application and I would call from this application some WPF windows. I've used the Interop Form Library to define a WinForm like a bridge from VB6 to WPF. The Interop WinForm exposes the methods to start and shutdown the wpf application.

To do that, in the WPF Application I've defined an helper like this:

Public Class StartApplicationHelper

    Public Shared Property IsReady As Boolean = False
    Public Shared Event NotifyEvent As ValueEnterEventHandler

    Public Shared Sub Start()
        If System.Windows.Application.Current Is Nothing Then
            Try
                Dim myApp As Application = New Application
                myApp.ShutdownMode = ShutdownMode.OnExplicitShutdown
                myApp.InitializeComponent()
                IsReady= True
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End If
    End Sub

    Public Shared Sub Shutdown()
        If System.Windows.Application.Current IsNot Nothing Then
            System.Windows.Application.Current.Shutdown()
            IsReady = False
        End If
    End Sub

    Public Shared Sub DispatchEvent(ByVal eve As String)
        If IsReady Then EventDispatcherService.DispatchEvent(eve, New EventDispatcherDataChildWin(String.Empty, Nothing, Nothing, False))
    End Sub

    Public Shared Sub DispatchResult(ByVal res As Object)
        RaiseEvent NotifyEvent(Nothing, New ValueEnterEventArgs(res))
    End Sub

End Class

the method DispatchEvent manage the execute of specific event like the opening of an application window.

For example, on winform I've wrote this statements:

 MyWpfApp.StartApplicationHelper.Start()
 Do While MyWpfApp.StartApplicationHelper.IsReady = False       
     System.Windows.Forms.Application.DoEvents()
 Loop
 MyWpfApp.StartApplicationHelper.DispatchEvent("OpenWin1")

in this way I can define an InteropFormMethod to open wpf window from VB6 across the Interop WinForm.

This solution seems work but I have a problem with a particular use case where the wpf application are stopped (shutdown) and then restarted (start). This is the displayed error message: "Cannot create more than one System.Windows.Application instance in the same AppDomain".

I'm trying to modify my helper to manage this case but I still have not found a solution. I would clean the AppDomain to restart wpf application. How can I do? Can you help me?