Visualizzazione dei risultati da 1 a 9 su 9
  1. #1
    Utente di HTML.it L'avatar di x69asterix
    Registrato dal
    Jan 2005
    Messaggi
    1,303

    [VB.NET]Terminare processo

    dovrei fare terminare un processo del task manager da codice, ma non riesco a trovare niente in merito, mi date una mano.

  2. #2
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,466
    Come mai questo processo non si chiude da solo ?

    Terminare un processo non e' mai una buona operazione ... che poi si faccia da un programma, e' anche peggio.
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  3. #3
    Utente di HTML.it L'avatar di x69asterix
    Registrato dal
    Jan 2005
    Messaggi
    1,303
    non si chiude perchè da una applicazione chiamo un'altra applicazione console "non mia".

  4. #4
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,466
    E allora? Questa seconda applicazione avra' un modo di terminare "normale" ...

    In ogni caso, leggi

    http://www.vb-helper.com/howto_net_t...e_process.html
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  5. #5
    Utente di HTML.it L'avatar di x69asterix
    Registrato dal
    Jan 2005
    Messaggi
    1,303
    allora ho provato ad usare:
    codice:
    Public Class Form1
        Inherits System.Windows.Forms.Form
    
    #Region " Windows Form Designer generated code "
    
        Public Sub New()
            MyBase.New()
    
            'This call is required by the Windows Form Designer.
            InitializeComponent()
    
            'Add any initialization after the InitializeComponent() call
    
        End Sub
    
        'Form overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub
    
        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer
    
        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        Friend WithEvents btnTerminate As System.Windows.Forms.Button
        Friend WithEvents btnSendCloseMessage As System.Windows.Forms.Button
        Friend WithEvents txtTargetTitle As System.Windows.Forms.TextBox
        Friend WithEvents label1 As System.Windows.Forms.Label
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Me.btnTerminate = New System.Windows.Forms.Button
            Me.btnSendCloseMessage = New System.Windows.Forms.Button
            Me.txtTargetTitle = New System.Windows.Forms.TextBox
            Me.label1 = New System.Windows.Forms.Label
            Me.SuspendLayout()
            '
            'btnTerminate
            '
            Me.btnTerminate.Location = New System.Drawing.Point(128, 48)
            Me.btnTerminate.Name = "btnTerminate"
            Me.btnTerminate.Size = New System.Drawing.Size(72, 32)
            Me.btnTerminate.TabIndex = 7
            Me.btnTerminate.Text = "Terminate"
            '
            'btnSendCloseMessage
            '
            Me.btnSendCloseMessage.Location = New System.Drawing.Point(24, 48)
            Me.btnSendCloseMessage.Name = "btnSendCloseMessage"
            Me.btnSendCloseMessage.Size = New System.Drawing.Size(72, 32)
            Me.btnSendCloseMessage.TabIndex = 6
            Me.btnSendCloseMessage.Text = "Send Close Message"
            '
            'txtTargetTitle
            '
            Me.txtTargetTitle.Location = New System.Drawing.Point(80, 8)
            Me.txtTargetTitle.Name = "txtTargetTitle"
            Me.txtTargetTitle.Size = New System.Drawing.Size(120, 20)
            Me.txtTargetTitle.TabIndex = 5
            Me.txtTargetTitle.Text = "Target"
            '
            'label1
            '
            Me.label1.Location = New System.Drawing.Point(8, 8)
            Me.label1.Name = "label1"
            Me.label1.Size = New System.Drawing.Size(64, 16)
            Me.label1.TabIndex = 4
            Me.label1.Text = "Target Title"
            '
            'Form1
            '
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(208, 85)
            Me.Controls.Add(Me.btnTerminate)
            Me.Controls.Add(Me.btnSendCloseMessage)
            Me.Controls.Add(Me.txtTargetTitle)
            Me.Controls.Add(Me.label1)
            Me.Name = "Form1"
            Me.Text = "Form1"
            Me.ResumeLayout(False)
    
        End Sub
    
    #End Region
    
        Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
        Private Declare Function GetWindowThreadProcessId Lib "user32" Alias "GetWindowThreadProcessId" (ByVal hwnd As Integer, ByRef lpdwProcessId As Integer) As Integer
        Private Declare Function OpenProcess Lib "kernel32" Alias "OpenProcess" (ByVal dwDesiredAccess As Integer, ByVal bInheritHandle As Integer, ByVal dwProcessId As Integer) As Integer
        Private Declare Function CloseHandle Lib "kernel32" Alias "CloseHandle" (ByVal hObject As Integer) As Integer
        Private Declare Function TerminateProcess Lib "kernel32" Alias "TerminateProcess" (ByVal hProcess As Integer, ByVal uExitCode As Integer) As Integer
        Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
        Private Const SYNCHRONIZE As Integer = &H100000
        Private Const PROCESS_TERMINATE As Integer = &H1
        Private Const WM_CLOSE As Integer = &H10
    
        ' Send the target the WM_CLOSE message.
        Private Sub btnSendCloseMessage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSendCloseMessage.Click
            ' Get the target's window handle.
            Dim target_hwnd As Integer = FindWindow(vbNullString, txtTargetTitle.Text)
            If (target_hwnd = 0) Then
                MessageBox.Show("Error finding target window handle")
                Exit Sub
            End If
    
            ' Send the application the WM_CLOSE message.
            PostMessage(target_hwnd, WM_CLOSE, 0, 0)
            MessageBox.Show("Sent WM_CLOSE message")
        End Sub
    
        ' Terminate the target.
        Private Sub btnTerminate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTerminate.Click
            Dim target_hwnd, target_process_id, target_process_handle As Integer
    
            ' Get the target's window handle.
            target_hwnd = FindWindow(vbNullString, txtTargetTitle.Text)
            If (target_hwnd = 0) Then
                MessageBox.Show("Error finding target window handle")
                Exit Sub
            End If
    
            ' Get the process ID.
            target_process_id = 0
            GetWindowThreadProcessId(target_hwnd, target_process_id)
            If (target_process_id = 0) Then
                MessageBox.Show("Error finding target process ID")
                Exit Sub
            End If
    
            ' Open the process.
            target_process_handle = OpenProcess( _
                SYNCHRONIZE Or PROCESS_TERMINATE, _
                0, target_process_id)
    
            If (target_process_handle = 0) Then
                MessageBox.Show("Error finding target process handle")
                Exit Sub
            End If
    
            ' Terminate the process.
            If (TerminateProcess(target_process_handle, 0) = 0) Then
                MessageBox.Show("Error terminating process")
            Else
                MessageBox.Show("Process terminated")
            End If
    
            ' Close the process.
            CloseHandle(target_process_handle)
        End Sub
    End Class
    ma mi dà :
    MessageBox.Show("Error finding target process handle")
    il Target è SplitCam.exe
    è non chiude l'applicazione

  6. #6
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,466
    Ma il processo lo avvii tu? E come?
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  7. #7
    Utente di HTML.it L'avatar di x69asterix
    Registrato dal
    Jan 2005
    Messaggi
    1,303
    dalla mia applicazione chiamo Process.Start(Application.StartupPath + "\SplitCam.exe")

  8. #8
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,466
    In questo caso, semplicemente, scrivi

    Dim p As Process

    ...

    p = Process.Start(Application.StartupPath + "\SplitCam.exe")

    e quando ti serve

    p.CloseMainWindow()

    se il processo ha una finestra con cui riceve messaggi di Windows, oppure

    p.Kill()

    per terminarlo piu' "duramente".
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  9. #9
    Utente di HTML.it L'avatar di x69asterix
    Registrato dal
    Jan 2005
    Messaggi
    1,303
    ottimo!!
    in entrambi sia
    p.CloseMainWindow()
    che
    p.Kill()
    eseguono la chiusura.
    Grazie mille!

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.