Systemauslastung zu hoch beim Programm

  • VB.NET

Es gibt 6 Antworten in diesem Thema. Der letzte Beitrag () ist von oli-f.

    Systemauslastung zu hoch beim Programm

    Hallo VBer

    Ich habe eine frage, ich habe mir ein kleinen Patcher gemacht, doch der geht recht in die systemleistung rein. kann man das ein wenig verkleinern? der soll ja nur runterladen und nicht grosse funktionen ausführen oder sonst was.
    ich erstelle in mit "Optimieren"= JA und NET.Framework 4

    hier der code:

    Spoiler anzeigen

    VB.NET-Quellcode

    1. Imports System.IO
    2. Imports System.Security.Cryptography
    3. Public Class Form1
    4. Dim PATCHURL As String = "http://************.ch/refine/patcher_row/"
    5. Dim PATCHLIST As String = "http://************.ch/refine/patchlist.xml"
    6. Dim AppPath As String = My.Computer.FileSystem.CurrentDirectory
    7. Dim WithEvents PatchDownloader As New System.Net.WebClient
    8. Dim WithEvents PatchListDownloader As New System.Net.WebClient
    9. Dim DLURLS As New List(Of String)
    10. Dim ANZAHLDLS As Integer = 0
    11. Dim busy As Boolean = Nothing
    12. Public Function MD5FileHash(ByVal sFile As String) As String
    13. Dim MD5 As New MD5CryptoServiceProvider
    14. Dim Hash As Byte()
    15. Dim Result As String = ""
    16. Dim Tmp As String = ""
    17. Dim FN As New FileStream(sFile, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
    18. MD5.ComputeHash(FN)
    19. FN.Close()
    20. Hash = MD5.Hash
    21. For i As Integer = 0 To Hash.Length - 1
    22. Tmp = Hex(Hash(i))
    23. If Len(Tmp) = 1 Then Tmp = "0" & Tmp
    24. Result += Tmp
    25. Next
    26. Return Result
    27. End Function
    28. Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    29. lbl_aktuelledatei.Text = "Starte Patchvorgang.."
    30. BG_List.RunWorkerAsync()
    31. End Sub
    32. Private Sub BG_List_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BG_List.DoWork
    33. PatchListDownloader.DownloadFileAsync(New Uri(PATCHLIST), AppPath & "\patchlist.xml")
    34. End Sub
    35. Private Sub PatchListDownloader_DownloadFileCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles PatchListDownloader.DownloadFileCompleted
    36. lbl_aktuelledatei.Text = "Patchlist herruntergeladen. Starte nun das Patchen."
    37. PB_AktuDatei.Value = 0
    38. PB_GSM.Value = 0
    39. BG_Pacher.RunWorkerAsync()
    40. End Sub
    41. Private Sub BG_Pacher_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BG_Pacher.DoWork
    42. If BG_Pacher.CancellationPending = True Then
    43. BG_Pacher.CancelAsync()
    44. End If
    45. Try
    46. Dim xDocument As New Xml.XmlDocument
    47. xDocument.Load(AppPath & "\patchlist.xml")
    48. lbl_aktuelledatei.Text = "Starte nun das Patchen"
    49. For Each node As Xml.XmlNode In xDocument.SelectNodes("Dateien/Datei")
    50. Dim DateiPfad As String = AppPath & "\" & node.Attributes("Dateiname").InnerText
    51. Dim CheckSum As String = node.Attributes("Checksum").InnerText
    52. Dim check As Boolean = Nothing
    53. lbl_aktuelledatei.Text = "Prüfe : " & node.Attributes("Dateiname").InnerText
    54. If File.Exists(DateiPfad) = False Then
    55. DLURLS.Add(PATCHURL & node.Attributes("Dateiname").InnerText.Replace("\", "/"))
    56. Else
    57. If CheckSum = MD5FileHash(DateiPfad) Then
    58. check = True
    59. Else
    60. DLURLS.Add(PATCHURL & node.Attributes("Dateiname").InnerText.Replace("\", "/"))
    61. check = False
    62. End If
    63. End If
    64. Next
    65. ANZAHLDLS = DLURLS.Count
    66. PB_GSM.Maximum = ANZAHLDLS
    67. PB_GSM.Value = 0
    68. Catch ex As Exception
    69. End Try
    70. Do
    71. Try
    72. If PatchDownloader.IsBusy = True Then
    73. busy = True
    74. Else
    75. For Each DownloadDatei In DLURLS
    76. If IsNothing(DownloadDatei) Then Exit Do
    77. Do
    78. If busy = False Then
    79. lbl_aktuelledatei.Text = "Patche : " & IO.Path.GetFileName(DownloadDatei)
    80. Dim SpeicherOrt As String = My.Computer.FileSystem.CurrentDirectory & "\" & DownloadDatei.Replace(PATCHURL, "").Replace("/", "\")
    81. Dim uri As New Uri(DownloadDatei)
    82. If File.Exists(SpeicherOrt) = True Then
    83. File.Delete(SpeicherOrt)
    84. End If
    85. PatchDownloader.DownloadFileAsync(uri, SpeicherOrt)
    86. busy = True
    87. Exit Do
    88. End If
    89. Loop
    90. Next
    91. File.Delete(AppPath & "\patchlist.xml")
    92. Exit Do
    93. End If
    94. Catch ex As Exception
    95. End Try
    96. Loop
    97. End Sub
    98. Private Sub PatchDownloader_DownloadFileCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles PatchDownloader.DownloadFileCompleted
    99. busy = False
    100. If PB_GSM.Value = ANZAHLDLS Then
    101. Else
    102. PB_GSM.Value += 1
    103. End If
    104. End Sub
    105. Private Sub PatchDownloader_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles PatchDownloader.DownloadProgressChanged
    106. PB_AktuDatei.Maximum = e.TotalBytesToReceive
    107. PB_AktuDatei.Value = e.BytesReceived
    108. End Sub
    109. End Class


    Und em entschuldigt, ich bin erst seit ca 1 2 wochen wirklich am lernen =)

    mfg

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Fire88“ ()

    hm, also zwei Tipps zu dir:

    1) dein Code ist zu lang. Es sind nur wenige hier, die sich die Zeit nehmen um solche Probleme ernsthaft anzugehen. Frage muss kurz sein. So nach dem Moto: Hier Problem. Was machen?...

    2) ich denke, (ohne jetzt dein Programm auseinander genommen zu haben) dass dein Fenster einfach nicht reagiert. Oder was auch immer. Dazu gibt es mehrere Threads als Lösung. Ist aber für einen Anfänger relativ umständlich zu vermitteln. Vielleicht suchst du nach ersten Schritten im Netz. Baust das Programm um, und stellst dann deine Fragen.

    dazu muss du wissen:
    - wie man einen Thread erstellt
    - wie man eine Funktion oder einen sub delegiert (delegate)
    - was sich mit invoke aufsich hat.

    dann wird es denke ich mal besser laufen. die ganzen do schleifen können da raus. die machen so wieso nur die arbeit. das braucht die Form1 nicht...

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „EugenIS“ ()

    Danke für deine Antwort.
    Das Fenster geht schon, nur meine CPU geht auf ca 20-25% (hexacore und 16gb Ram)
    hmm ja da hst du recht das ich mit dem noch nicht soo vertraut bin =) aber ich versuche mein bestes.

    Was meinst du mit:

    EugenIS schrieb:

    die ganzen do schleifen können da raus. die machen so wieso nur die arbeit. das braucht die Form1 nicht...
    Kann ich die durch etwas anderes ersetzen?
    Danke
    so ich hoffe es funktioniert...

    VB.NET-Quellcode

    1. Imports System.IO
    2. Imports System.Security.Cryptography
    3. Public Class Form1
    4. Dim PATCHURL As New Uri("http://procoder.ch/refine/patcher_row/")
    5. Dim PATCHLIST As New Uri("http://procoder.ch/refine/patchlist.xml")
    6. Dim AppPath As String = Application.StartupPath
    7. Dim WithEvents PatchDownloader As New System.Net.WebClient
    8. Dim WithEvents PatchListDownloader As New System.Net.WebClient
    9. Dim DLURLS As New List(Of String)
    10. Dim MD5 As New MD5CryptoServiceProvider
    11. Public Function MD5FileHash(ByVal sFile As String) As String
    12. Dim Hash As Byte()
    13. Dim Result As String = ""
    14. Dim FN As New FileStream(sFile, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
    15. Hash = MD5.ComputeHash(FN)
    16. FN.Close()
    17. For i As Integer = 0 To Hash.Length - 1
    18. Result += Hash(i).ToString("X2")
    19. Next
    20. Return Result.ToUpper
    21. End Function
    22. Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    23. lbl_aktuelledatei.Text = "Starte Patchvorgang.."
    24. BG_List.RunWorkerAsync()
    25. End Sub
    26. Private Sub BG_List_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BG_List.DoWork
    27. PatchListDownloader.DownloadFile(PATCHLIST, AppPath & "\patchlist.xml")
    28. 'lbl_aktuelledatei.Text = "Patchlist herruntergeladen. Starte nun das Patchen."
    29. PB_AktuDatei.Value = 0
    30. PB_GSM.Value = 0
    31. BG_Pacher.RunWorkerAsync()
    32. End Sub
    33. Dim xDocument As New Xml.XmlDocument
    34. Private Sub BG_Pacher_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BG_Pacher.DoWork
    35. If BG_Pacher.CancellationPending Then
    36. BG_Pacher.CancelAsync()
    37. End If
    38. Try
    39. XDocument.Load(AppPath & "\patchlist.xml")
    40. 'lbl_aktuelledatei.Text = "Starte nun das Patchen"
    41. For Each node As Xml.XmlNode In xDocument.SelectNodes("Dateien/Datei")
    42. Dim DateiPfad As String = AppPath & "\" & node.Attributes("Dateiname").InnerText
    43. Dim CheckSum As String = node.Attributes("Checksum").InnerText
    44. 'lbl_aktuelledatei.Text = "Prüfe : " & node.Attributes("Dateiname").InnerText
    45. If Not File.Exists(DateiPfad) Then
    46. ' Datei existiert nicht
    47. DLURLS.Add(PATCHURL.AbsoluteUri & node.Attributes("Dateiname").InnerText.Replace("\", "/"))
    48. ElseIf Not CheckSum.ToUpper = MD5FileHash(DateiPfad) Then
    49. ' Datei inaktuell
    50. DLURLS.Add(PATCHURL.AbsoluteUri & node.Attributes("Dateiname").InnerText.Replace("\", "/"))
    51. Else
    52. ' Datei aktuell
    53. End If
    54. Next
    55. PB_GSM.Maximum = DLURLS.Count + 1
    56. PB_GSM.Value = 0
    57. Catch
    58. End Try
    59. Try
    60. For Each DownloadDatei In DLURLS
    61. If Not IsNothing(DownloadDatei) Then
    62. ' Spart wieder CPU
    63. ' IsBusy ersetzt busy
    64. While PatchDownloader.IsBusy
    65. Threading.Thread.Sleep(100)
    66. End While
    67. 'lbl_aktuelledatei.Text = "Patche : " & IO.Path.GetFileName(DownloadDatei)
    68. Dim SpeicherOrt As String = AppPath & "\" & DownloadDatei.Replace(PATCHURL.AbsoluteUri, "").Replace("/", "\")
    69. Dim uri As New Uri(DownloadDatei)
    70. If File.Exists(SpeicherOrt) Then
    71. File.Delete(SpeicherOrt)
    72. End If
    73. PatchDownloader.DownloadFileAsync(uri, SpeicherOrt)
    74. End If
    75. Next
    76. File.Delete(AppPath & "\patchlist.xml")
    77. Catch
    78. End Try
    79. Threading.Thread.Sleep(100)
    80. End Sub
    81. Private Sub PatchDownloader_DownloadFileCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles PatchDownloader.DownloadFileCompleted
    82. PB_GSM.Value += 1
    83. End Sub
    84. Private Sub PatchDownloader_DownloadProgressChanged(ByVal sender As Object, ByVal e As System.Net.DownloadProgressChangedEventArgs) Handles PatchDownloader.DownloadProgressChanged
    85. PB_AktuDatei.Maximum = e.TotalBytesToReceive
    86. PB_AktuDatei.Value = e.BytesReceived
    87. End Sub
    88. End Class

    Gredsoft schrieb:

    Schon blöd, wenn man auf seiner Profilpage seine eigene Homepage schreibt und hier im Thread die Website zensiert. :D

    naja, auf eine weise hast du schon recht, nur ist der Pfad im Programm nicht der von meinem Forum sondern von meinem Patchserver =)

    oli-f schrieb:

    so ich hoffe es funktioniert...

    Perfekt, das geht prima, hab nun nurnoch eine auslastung von ca 5-12%
    das muss ich mir nun sehr gut anschauen was du da alles verbessert hast,
    Vielen Vielen dank für deine/Eure Hilfe