none
How would I run a Winform application without the Form opening RRS feed

  • Question

  • I have a winform application, and for setup purposes I will use it to configure the application.  However, there is a section of code in the application that can run without the winform being visible or minimized. The section of code is on a thread timer and I can activate it from the winform.  But what I would like to do is pass in an Argument (such as "\Auto") and have the timer take over and run the code without the winform. 

    I have tried to pass an argument into the Winform using My.Application.CommandLineArgs.  But I get a nasty error when I do this and nothing loads.  I have seen examples where it was suggested to use a separate class called Main which would process prior to loading the Winform.  But when I have done this I get a console window appearing every time I want to open the application without an Argument. 

    I need a better way of handling this. Ultimately, this application could become a service.  But we are in the Testing phase and it will have to remain as a winform for a while.

    Public Sub FrmHplc_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim oNode As New TreeNode
            Dim udlHelper As New UdlHelper
    
            ' establish the UDL file connection
            Dim connectStr As String = udlHelper.GetConnectionString()
            My.Settings.ConnectionFile = connectStr
            'My.Settings.CurMonth = Month(now)
            'My.Settings.CurYear = Year(now)
            My.Settings.Save()
    
            ' Check if app is to run in Console or winform
            For Each arg As String In My.Application.CommandLineArgs
                Console.WriteLine(arg)
                If arg IsNot Nothing And arg(0) = "/Auto" Then
    
                    Try
                        If File.Exists(StorageData) Then
                            Files = File.ReadAllLines(StorageData).ToList
                        Else
                            File.WriteAllLines(StorageData, Files)
                        End If
                        CheckFiles()
                        With CycleTimr
                            .Interval = 2000
                            .Enabled = True
                        End With
                        LogFileClean()
                    Catch ex As Exception
                        ErrorLogger.WriteToErrorLog("Timer Errors", ex.Message, ex.StackTrace)
                    End Try
                Else
    
                    ' No params passed on the command line, open the usual UI interface
                    Application.EnableVisualStyles()
                    Application.SetCompatibleTextRenderingDefault(False)
                    Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException)
                    Application.Run(New FrmHplc())
    
                End If
            Next
    
            oNode.ImageIndex = 0 ' Closed folder oNode.SelectedImageIndex = 0
    
            oNode.Text = My.Settings.Directory
            TreeView1.Nodes.Add(oNode)
            TreeView1.ExpandAll()
            oNode.Nodes.Add("")
    
            My.Settings.SupportFiles = Application.StartupPath
            My.Settings.Save()
        End Sub

    Friday, November 2, 2018 9:23 PM

Answers

  • Try this then

    Public Class Form1
        '
        ' Your other code goes here
        '
        Protected Overrides Sub SetVisibleCore(ByVal value As Boolean)
    
            If Not IsHandleCreated Then
                CreateHandle()
                value = Not My.Application.Ok
            End If
            MyBase.SetVisibleCore(value)
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Cam Evenson Monday, November 5, 2018 6:36 PM
    • Unmarked as answer by Cam Evenson Monday, November 5, 2018 9:54 PM
    • Marked as answer by Cam Evenson Monday, November 5, 2018 10:25 PM
    Monday, November 5, 2018 5:21 PM
    Moderator

All replies

  • Hello,

    1. Open the project's property window
    2. Click "View Application Events" button.
    3. Middle combo box select "MyApplication events"
    4. Right combo box select StartUp
    5. Write code similar to below.
    Namespace My
        Partial Friend Class MyApplication
            Private _ok As Boolean
            Public ReadOnly Property Ok() As Boolean
                Get
                    Return _ok
                End Get
            End Property
    
            Private Sub MyApplication_Startup(sender As Object, e As StartupEventArgs) Handles Me.Startup
                _ok = e.CommandLine.Contains("/Auto")
            End Sub
        End Class
    End Namespace

    Form load

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        If My.Application.Ok Then
            ' do something
        Else
            ' do something else
        End If
    End Sub

    To test, open the project's property window to the Debug tab, enter as follows and alter the code above to do something.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Friday, November 2, 2018 10:31 PM
    Moderator
  • I have tried this in the past and was unsuccessful with hiding the winform even thou the flag was seen as being true.  So I do not know how to get the form hidden.  I tried putting a hide but the main form still appears.

    Public Sub FrmHplc_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim oNode As New TreeNode
            Dim udlHelper As New UdlHelper
    
            ' establish the UDL file connection
            Dim connectStr As String = udlHelper.GetConnectionString()
            My.Settings.ConnectionFile = connectStr
            'My.Settings.CurMonth = Month(now)
            'My.Settings.CurYear = Year(now)
            My.Settings.Save()
    
            ' Check if app is to run in Console or winform
    
            If My.Application.Ok Then
    
                    Try
                        Me.Hide()
                        If File.Exists(StorageData) Then
                            Files = File.ReadAllLines(StorageData).ToList
                        Else
                            File.WriteAllLines(StorageData, Files)
                        End If
                        CheckFiles()
                        With CycleTimr
                            .Interval = 2000
                            .Enabled = True
                        End With
                        LogFileClean()
                    Catch ex As Exception
                        ErrorLogger.WriteToErrorLog("Timer Errors", ex.Message, ex.StackTrace)
                    End Try
                Else
    
                    oNode.ImageIndex = 0 ' Closed folder oNode.SelectedImageIndex = 0
    
                    oNode.Text = My.Settings.Directory
                    TreeView1.Nodes.Add(oNode)
                    TreeView1.ExpandAll()
                    oNode.Nodes.Add("")
    
                    My.Settings.SupportFiles = Application.StartupPath
                    My.Settings.Save()
    
                    ' No params passed on the command line, open the usual UI interface
                    Application.EnableVisualStyles()
                    'Application.SetCompatibleTextRenderingDefault(False)
                    'Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException)
                    Me.Show()
                    'Application.Run(New FrmHplc())
    
                End If
            
    
            
        End Sub

    Monday, November 5, 2018 5:05 PM
  • Try this then

    Public Class Form1
        '
        ' Your other code goes here
        '
        Protected Overrides Sub SetVisibleCore(ByVal value As Boolean)
    
            If Not IsHandleCreated Then
                CreateHandle()
                value = Not My.Application.Ok
            End If
            MyBase.SetVisibleCore(value)
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Cam Evenson Monday, November 5, 2018 6:36 PM
    • Unmarked as answer by Cam Evenson Monday, November 5, 2018 9:54 PM
    • Marked as answer by Cam Evenson Monday, November 5, 2018 10:25 PM
    Monday, November 5, 2018 5:21 PM
    Moderator
  • Not sure if this will help, but if you have code that isn't dependent on an instance of the form you might be able to accomplish what you want in the constructor.


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it."

    - from former MSDN User JohnWein

    SerialPort Info

    Multics - An OS ahead of its time.

    Monday, November 5, 2018 6:07 PM
  • I think that did it.  Looks like the application is running but without the Winform window showing.  Now on to assuring the timing function is actually performing the code.
    Monday, November 5, 2018 6:35 PM
  • I spoke to soon, when I add the SetVisibleCore code to the application yes the form does not load but neither does any of the code in the other classes.  So is there a way to hide the form. 
    Monday, November 5, 2018 9:56 PM
  • I spoke to soon, when I add the SetVisibleCore code to the application yes the form does not load but neither does any of the code in the other classes.  So is there a way to hide the form. 

    Not sure what to say other than you need to rethink code flow. Any code that needs to execute without the form should not reside in the form.

    Dummy example

    Namespace My
        Partial Friend Class MyApplication
            Private _ok As Boolean
            Public ReadOnly Property Ok() As Boolean
                Get
                    Return _ok
                End Get
            End Property
    
            Private Sub MyApplication_Startup(sender As Object, e As StartupEventArgs) Handles Me.Startup
                _ok = e.CommandLine.Contains("/Auto")
                If _ok Then
                    Dim ops = New InvisibleOperations
                    ops.DoSomeWork()
                    Environment.Exit(0)
                End If
            End Sub
        End Class
    End Namespace

    Dummy class

    Public Class InvisibleOperations
        Public Sub DoSomeWork()
            ' do something
        End Sub
    End Class
    

    Run the debugger and the code (for me) works as expected with /Auto and without /Auto.



    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, November 5, 2018 10:17 PM
    Moderator
  • I found a workaround.  I determine if the main form is activated.  Then I pass in the Application.Ok condition.  If it is true then I minimize the form and turn off that is shows in the taskbar. And poof, no form but can still run the code in it.  And it looks like the memory footprint is much smaller as well.


    Monday, November 5, 2018 10:27 PM
  • I found a workaround.  I determine if the main form is activated.  Then I pass in the Application.Ok condition.  If it is true then I minimize the form and turn off that is shows in the taskbar. And poof, no form but can still run the code in it.  And it looks like the memory footprint is much smaller as well.


    Good to hear this is working for you :-)

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, November 5, 2018 10:36 PM
    Moderator