This post has 2 Replies | 2 Followers

Top 100 Contributor
Posts 4
phuc Posted: 04-17-2009 8:50 AM

Mình viết 2 ứng dụng: 1 ứng dụng bằng WPF, 1 ứng dụng bằng Winform.

Dùng Winform thông qua COM để remote WPF và có lỗi khi start Application (WPF) lần thứ 2. Exception như sau:

System.InvalidOperationException: Cannot create more than one System.Windows.Application instance in the same AppDomain.

   at System.Windows.Application..ctor()

   at WpfApplication1.Application..ctor()

   at WpfApplication1.Application.Main()

   at ActiveX.ComClass.Start() in C:\Documents and Settings\ntphuc\My Documents\Visual Studio 2008\Projects\COM\ActiveX\ComClass.vb:line 22

------------------------------------------------------------------------------------

Trong WPF mình có thêm add thêm project có tên là ActiveX chứ 1 class dạng COM tên là ComClass.vb như sau:

<ComClass(ComClass.ClassId, ComClass.InterfaceId, ComClass.EventsId)> _
Public Class ComClass

#Region "COM GUIDs"
    ' These  GUIDs provide the COM identity for this class
    ' and its COM interfaces. If you change them, existing
    ' clients will no longer be able to access the class.
    Public Const ClassId As String = "247a3a9e-3e77-4129-812b-47dee3d96abc"
    Public Const InterfaceId As String = "1e028a7a-9e45-4ef2-b3c2-9a6736fbbbdc"
    Public Const EventsId As String = "fc46970c-e032-47d2-81fc-8d7eb9023465"
#End Region

    ' A creatable COM class must have a Public Sub New()
    ' with no parameters, otherwise, the class will not be
    ' registered in the COM registry and cannot be created
    ' via CreateObject.
    Public Sub New()
        MyBase.New()
    End Sub
    Public Sub Start()
        Try
            WpfApplication1.Application.Main()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
    Public Sub StopTB()
        WpfApplication1.tbBoot.Shutdown()
    End Sub
End Class

-------------------------------------------------------------------------------------------------------------------------------------------------------

ứng dụng Winform add reference ActiveX projects.

Winform có 2 button Start và Stop. code như sau:

Public Class Form1

    Dim obj As ActiveX.ComClass
    Private _isFirstStart As Boolean = False
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        obj = New ActiveX.ComClass
        obj.Start()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        obj.StopTB()
        obj = Nothing
    End Sub

End Class

----------------------------------------------------

Vấn đề là ko thể start lần 2 ứng dụng WPF bằng Winform (  WpfApplication1.Application.Main()).

Bạn nào biết chỉ dùm mình

Phúc

  • Filed under:
Top 25 Contributor
Posts 24

Nếu có thể bạn nên dùng System.Diagnostics, ở đó bạn có class Process cho phép bạn chạy 1 application khác.

Top 100 Contributor
Posts 4

cam on ban, do la cach tot de chay 2 process, nhung neu nhu the minh khong the su dung ung dung winform de dieu khien ung dung WPF dc

Page 1 of 1 (3 items) | RSS