[VB2008] Filtrowanie treści

Cześć!

Mam skrypt który wyświetla listę procesów i ich czas pracy do TextBox1, za pomocą przycisku Button1 zapisuje w pliku.

Chciałbym przefiltrować listę procesów i zapisać tylko jeden o danej nazwie, wie ktoś w jaki sposób to zrobić?

Skrypt:

Public Class Form1


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


        Dim AllProcesses() As Process = Process.GetProcesses()


        For idx = 0 To AllProcesses.Length - 1

            Try

                Dim ProcessTime As TimeSpan = DateTime.Now - AllProcesses(idx).StartTime


                TextBox1.AppendText(AllProcesses(idx).ProcessName + " " + ProcessTime.ToString() + Environment.NewLine)

            Catch Ex As Exception

                TextBox1.AppendText(AllProcesses(idx).ProcessName + " brak dostępu!" + Environment.NewLine)

            End Try

        Next


    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        FileOpen(1, "c:\plik.txt", OpenMode.Output)

        Print(1, TextBox1.Text)

        FileClose(1)

    End Sub

End Class

Pozdrawiam!

Np tak:

Public Class Form1

Dim ProcesDoZapisu As String

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

 Dim idx As Integer

 Dim DanyProces As String

 DanyProces = "svchost"

ProcesDoZapisu=""

        Dim AllProcesses() As Process = Process.GetProcesses()

        For idx = 0 To AllProcesses.Length - 1

            Try

                Dim ProcessTime As TimeSpan = DateTime.Now - AllProcesses(idx).StartTime

                TextBox1.AppendText(AllProcesses(idx).ProcessName + " " + ProcessTime.ToString() + Environment.NewLine)

If AllProcesses(idx).ProcessName = DanyProces Then ProcesDoZapisu = ProcesDoZapisu + AllProcesses(idx).ProcessName + " " + ProcessTime.ToString() + Environment.NewLine

            Catch Ex As Exception

                TextBox1.AppendText(AllProcesses(idx).ProcessName + " brak dostępu!" + Environment.NewLine)

            End Try

        Next

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        FileOpen(1, "c:\plik.txt", OpenMode.Output)

        Print(1, ProcesDoZapisu)

        FileClose(1)

    End Sub

Dzięki, właśnie o takie coś mi chodziło :slight_smile:

Ale nie wiem jak podczepić to pod Timer (zamiast przycisku)

Szukałem poradników ale nigdzie nie znalazłem wykonywania czynności przez timer co jakiś czas,

były tylko przykłady stoperów.

Umieszczasz Timer na formie. Ustatwiasz interval czyli co ile milisekund ma być wykonywana procedura(standardowo ustawione na 100; 1000 milisekund=1 sekunda)

No i wpisujesz procdurę jaka ci pasuje.

Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick

'Tu wpisujesz procedurę

    End Sub

Wpisuje do timera kod spod przycisku, ale nadal nie działa:

Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        FileOpen(1, "c:\plik.txt", OpenMode.Output)

        Print(1, ProcesDoZapisu)

        FileClose(1)

    End Sub

Zegar standardowo jest nieaktywny, to znaczy jest ustawione: Timer1.enabled=false

musisz zatem zmienić na: Timer1.enabled=true we właściowoścach lub np. przy starcie programu:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Timer1.enabled=true

End Sub

żeby procesy były odświeżane to musi być za każdym razem uruchamiana procedura która jak na razie uruchamiana jest tylko raz tutaj:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim idx As Integer

        Dim DanyProces As String

        Dim AllProcesses() As Process = Process.GetProcesses()

        DanyProces = "svchost"

        For idx = 0 To AllProcesses.Length - 1

            Try

                Dim ProcessTime As TimeSpan = DateTime.Now - AllProcesses(idx).StartTime

                TextBox1.AppendText(AllProcesses(idx).ProcessName + " " + ProcessTime.ToString() + Environment.NewLine)

                If AllProcesses(idx).ProcessName = DanyProces Then ProcesDoZapisu = ProcesDoZapisu + AllProcesses(idx).ProcessName + " " + ProcessTime.ToString() + Environment.NewLine

            Catch Ex As Exception

                TextBox1.AppendText(AllProcesses(idx).ProcessName + " brak dostępu!" + Environment.NewLine)

            End Try


        Next

    End Sub

Krótko mówiąc procedurę tą należało by dać do zegara.

Timer działa. Tylko procesy nie są odświeżane,tzn czas zrzucany jest raz, podczas startu programu. Da się to jakoś naprawić?

Niezależnie kiedy i gdzie zegar uaktywnimy, to od tego momentu jego uaktywnienia działa bez żadnych dodatkowy zapętleń. Jak nie wierzysz, to dodaj na próbę linię:

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        licznik = licznik + 1

        TextBox1.Text = licznik

         End Sub

Wcześniej oczywiście deklarując zmienną licznik

Public Class Form1

    Dim ProcesDoZapisu As String

    Dim licznik As long

Żeby następowało odświeżanie to procedurę którą umieściłeś tutaj:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim idx As Integer

        Dim DanyProces As String

        Dim AllProcesses() As Process = Process.GetProcesses()

        DanyProces = "svchost"

        For idx = 0 To AllProcesses.Length - 1

            Try

                Dim ProcessTime As TimeSpan = DateTime.Now - AllProcesses(idx).StartTime

                TextBox1.AppendText(AllProcesses(idx).ProcessName + " " + ProcessTime.ToString() + Environment.NewLine)

                If AllProcesses(idx).ProcessName = DanyProces Then ProcesDoZapisu = ProcesDoZapisu + AllProcesses(idx).ProcessName + " " + ProcessTime.ToString() + Environment.NewLine

            Catch Ex As Exception

                TextBox1.AppendText(AllProcesses(idx).ProcessName + " brak dostępu!" + Environment.NewLine)

            End Try

        Next

    End Sub

należało by też dać do zegara. – Dodane 07.01.2011 (Pt) 16:47 – Procedura w zegarze mogła by np. wyglądać tak:

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        Dim idx As Integer

        Dim DanyProces As String

        Dim AllProcesses() As Process = Process.GetProcesses()

        DanyProces = "svchost"

        ProcesDoZapisu = "" : TextBox1.Text = ""

        For idx = 0 To AllProcesses.Length - 1

            Try

                Dim ProcessTime As TimeSpan = DateTime.Now - AllProcesses(idx).StartTime

                TextBox1.AppendText(AllProcesses(idx).ProcessName + " " + ProcessTime.ToString() + Environment.NewLine)

                If AllProcesses(idx).ProcessName = DanyProces Then ProcesDoZapisu = ProcesDoZapisu + AllProcesses(idx).ProcessName + " " + ProcessTime.ToString() + Environment.NewLine

            Catch Ex As Exception

                TextBox1.AppendText(AllProcesses(idx).ProcessName + " brak dostępu!" + Environment.NewLine)

            End Try

        Next

           Print(1, ProcesDoZapisu)

   End Sub

Otwarcie pliku do zapisu wystarczy dać tylko raz np.tu:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

       FileOpen(1, "c:\plik.txt", OpenMode.Output)

    End Sub