How would I run a Winform application without the Form opening

  • Thread starter Thread starter Cam Evenson
  • Start date Start date
C

Cam Evenson

Guest
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

Continue reading...
 
Back
Top