VB.Net/Windows System/Process

Материал из VB Эксперт
Перейти к: навигация, поиск

Check Existing Duplicate Process

 
Imports System.Threading
Module Module1

    Sub Main()
        Dim MatchingNames As System.Diagnostics.Process()
        Dim TargetName As String
        TargetName = System.Diagnostics.Process.GetCurrentProcess.ProcessName
        MatchingNames = System.Diagnostics.Process.GetProcessesByName(TargetName)
        If (MatchingNames.Length = 1) Then
            Console.WriteLine("Started...")
        Else
            Console.WriteLine("Process already running")
        End If
    End Sub
End Module


Checking for a Previous Instance

  
Module MainModule
    Public Function PrevInstance() As Boolean
        If UBound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0 Then
            Return True
        Else
            Return False
        End If
    End Function
End Module


Get current Running Process

 
Imports System.Threading
Public Class MainClass
   Public Shared Sub Main()
        Dim ProcessList As System.Diagnostics.Process()
        ProcessList = System.Diagnostics.Process.GetProcesses()
        Dim Proc As System.Diagnostics.Process
        For Each Proc In ProcessList
            Console.WriteLine("Name {0} ID {1}", _
              Proc.ProcessName, Proc.Id)
        Next
   End Sub
End Class


Get Previous Instances

  
Imports System.Windows.Forms
Public Class Form1
    Inherits System.Windows.Forms.Form
    Public Sub New()
        MyBase.New()
        InitializeComponent()
    End Sub
    Friend WithEvents Label1 As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.SuspendLayout()
        "
        Me.Label1.Location = New System.Drawing.Point(16, 16)
        Me.Label1.Size = New System.Drawing.Size(296, 23)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "Run the final .EXE version of this application."
        "
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(360, 157)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Label1})
        Me.ResumeLayout(False)
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If PrevInstance() = True Then
            Dim Processes() As Process
            Processes = Diagnostics.Process.GetProcessesByName( Diagnostics.Process.GetCurrentProcess.ProcessName)
            AppActivate(Processes(0).Id)
            Application.Exit()
        End If
    End Sub
    Public Function PrevInstance() As Boolean
        If Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName).Length > 1 Then
            Return True
        Else
            Return False
        End If
    End Function
End Class


Start a Process

 
Imports System
Imports System.Data
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Diagnostics
Imports System.Drawing.Printing
Public Class MainClass
    
    Shared Sub Main()
        
        Dim new_process As New Process
        new_process.StartInfo.FileName = Application.ExecutablePath
        new_process.StartInfo.Verb = "Open"
        new_process.Start()
    End Sub
End Class


Use Process to Start IExplore.exe

 
Imports System.Windows.Forms
Public Class MainClass
   Public Shared Sub Main()
        Try
            System.Diagnostics.Process.Start("IExplore.exe", "www.vbex.ru")
        Catch
            MessageBox.Show("Error launching Internet Explorer")
        End Try
   
   End Sub
End Class