Question Opening a nre Process from a Windows Forms .dll

kawark

New member
Joined
Nov 5, 2009
Messages
4
Location
Little Rock, AR
Programming Experience
5-10
I have a Winforms applicaton compiled as a Dynamic Link Library instead of an .exe


I pass the path of the dll to a console applicaton, Load the dll an assembly, do all the necessary steps to run the application and all works.

The Problem is that I can close my winforms application but my "Loader application" still shows up as running. You have to go to Taskmanager and close it.

Here is the console app that I pass the path to the dll to, create and run the application.

Can someone tell me what I need to do to get the console starter app to close either independently of the app it starts? Or how to close the console app when then started application is closed?


VB.NET:
Imports System
Imports System.Windows.Forms
Imports System.Diagnostics
Imports System.IO
Imports System.Reflection
Imports System.Xml

Module Module1

    Sub Main()
        Main(Environment.GetCommandLineArgs())
    End Sub

    Private Sub Main(ByVal args() As String)
        Dim dllpath As String = ""
        Try
            dllpath = args(1).ToString()
        Catch ex As Exception
            'write to error log
            Exit Sub
        End Try

        Dim fi As FileInfo = New FileInfo(dllpath)
        Dim appname As String = fi.Name.Substring(0, fi.Name.Length() - fi.Extension.Length)
        Try
            Dim asm As Assembly = Assembly.LoadFrom(dllpath)
            Dim clsname As String = appname & ".clsConsole"
            Dim type As Type = asm.GetType(clsname)
            Dim instance As Object = Activator.CreateInstance(type)

            Dim mi As MethodInfo = type.GetMethod("CallFormWithParms")
            mi.Invoke(instance, New Object() {My.Settings.AppConfigFilesPath})

            Application.Run()

        Catch ex As Exception
            Dim str1 As String = ex.Message
        End Try


    End Sub

End Module
 
You should read the help topics for the three Run method versions: Application.Run Method (System.Windows.Forms)
Ideally you should use the Run(Form) version, you can use Activator to create the form instance from the dll and pass it the parameters, then Run(it). Otherwise the help topics will tell you the other options you have to close the message loop manually is to call Application.Exit or .ExitThread from the form.
 
Thanks JohnH. :D


In my class clsConsole I changed the Sub to a Function that returns a form.
From there I changed my calling code to get a form and use the Application.Run(form) and it is working now. Thanks.

here's the modified code for others.
VB.NET:
Imports System
Imports System.Windows.Forms
Imports System.Diagnostics
Imports System.IO
Imports System.Reflection
Imports System.Xml

Module Module1

    Sub Main()
        Main(Environment.GetCommandLineArgs())
    End Sub

    Private Sub Main(ByVal args() As String)
        Dim dllpath As String = ""
        Try
            dllpath = args(1).ToString()
        Catch ex As Exception
            'write to error log
            Exit Sub
        End Try

        Dim fi As FileInfo = New FileInfo(dllpath)
        Dim appname As String = fi.Name.Substring(0, fi.Name.Length() - fi.Extension.Length)
        Try
            Dim asm As Assembly = Assembly.LoadFrom(dllpath)
            Dim clsname As String = appname & ".clsConsole"
            Dim type As Type = asm.GetType(clsname)
            Dim instance As Object = Activator.CreateInstance(type)

              Dim mi As MethodInfo = type.GetMethod("CreateFormWithParms")
            Dim f As Form = mi.Invoke(instance, New Object() {My.Settings.AppConfigFilesPath})

            Application.Run(f)

        Catch ex As Exception
            Dim str1 As String = ex.Message
        End Try


    End Sub

End Module


Here is my class module in the dll I'm loading:

clsConsole:

VB.NET:
Imports TestApp1
Public Class clsConsole

   
    Public Function CreateFormWithParms(ByVal parmpath As String) As Windows.Forms.Form
        Dim frm As New Form1

        If parmpath IsNot Nothing Then
            frm.AppConfigPath = parmpath
        End If

        Return frm

    End Function

End Class
 
Back
Top