Archives

All posts for the year 2009

VB.NET class for running process/application as background thread:

Public Class Shell
   
   Private Process1 As Process = New Process()
   Sub New()
   
   End Sub
   
   Public ReadOnly Property Active() As Boolean
      Get
         Return Process1.HasExited
      End Get
   End Property
   
   Public Sub Run(ByVal sProcessPath As String, ByVal sArguments As String)
      Process1.StartInfo.FileName = sProcessPath
      Process1.StartInfo.WindowStyle = ProcessWindowStyle.Minimized
      Process1.StartInfo.CreateNoWindow = False
      Process1.StartInfo.UseShellExecute = True
      Process1.EnableRaisingEvents = True
      Process1.StartInfo.Arguments = sArguments
      Try
         'Start Process
         Process1.Start()
         Wait()
      Catch ex As Exception
         MsgBox(ex.Message)
      Finally
         Process1.Close()
      End Try
   End Sub
   
   Private Sub Wait()
      If Not Process1.HasExited Then
         System.Threading.Thread.Sleep(1000)
      End If
   End Sub
End Class

Example for transparent printing txt file:

Dim sproc As New Shell
sproc.Run("notepad", " /p c:\test\example.txt" )

Example for transparent printing pdf  file with FoxitReader :

Dim pdfAppPath as String = String.Empty
Dim progamsfolder As String = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)
If File.Exists(progamsfolder & "\Foxit\FoxitReader.exe") Then pdfAppPath = progamsfolder & "\Foxit\FoxitReader.exe"
   
Dim sproc As New Shell
sproc.Run(pdfAppPath, " /p c:\test\example.pdf")