Sub wird 2 mal ausgeführt (und ich find den Fehler nicht -.-)

  • VB.NET

Es gibt 4 Antworten in diesem Thema. Der letzte Beitrag () ist von der_Kurt.

    Sub wird 2 mal ausgeführt (und ich find den Fehler nicht -.-)

    Hallo...
    Ich hab hier en recht komischer Fehler....mit diesem Code (fertiger Dwonload mit webclient):

    VB.NET-Quellcode

    1. Private Sub dlfertig(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles downloader.DownloadFileCompleted
    2. If e.Cancelled = False Then
    3. Timer1.Stop()
    4. dekodieren(saveurl, Form1.settings(6) & "/" & Path.GetFileNameWithoutExtension(saveurl) & ".mp3")
    5. Else
    6. Exit Sub
    7. End If
    8. End Sub

    wird die Sub "dekodieren" 2 mal gestartet....dadurch wird FFMPEG natürlcih auch doppelt gestartet...

    An der Dekodieren-Sub kanns auch nicht liegen, da der Selbe Fehler auch bei nem anderen Programm vorkommt (und da trägts statt einem Listvew-item auch gleich 2 ein ) oO

    Ich hab zwar schon ne idee wie ich des Lösen könnt, aber eigentlich ist das doch nicht normal dass ein Download zweimal Fertig ist ^^

    Hoffe jemand weis ne Lösung (Außer beim DLfertig ereignis variable hochzählen lassen und dekodieren nur bei 0 ausführen oder so ähmlich :D)


    MfG Stefan

    VB.NET-Quellcode

    1. Imports System.Drawing
    2. 'Imports System.Management
    3. Imports System.Text.RegularExpressions
    4. Imports System.Net
    5. Imports System.Web
    6. Imports System.IO
    7. Public Class dekodownloadfenster
    8. Dim downrate As Integer = 0
    9. Dim oldfortschritt As Integer = 0
    10. Dim newfortschritt As Integer = 0
    11. Dim totalb As Integer = 0
    12. Dim recentb As Integer = 0
    13. Public request As HttpWebRequest
    14. Public request2 As HttpWebRequest
    15. Public response As HttpWebResponse
    16. Public source As String
    17. Public response2 As HttpWebResponse
    18. Public source2 As String
    19. Public WithEvents web As New WebClient
    20. Public downloadurl As String
    21. Delegate Sub OneArgD(ByVal arg As Object)
    22. Private MausLocation As Point
    23. Dim downloadbeendet As Boolean = True
    24. Dim vid As Integer = 1
    25. Public youtubeurl As String
    26. Public justvideo As Boolean = False
    27. Dim saveding As String
    28. Public CS_DROPSHADOW As Integer = 0
    29. Dim saveurl As String
    30. Private WithEvents downloader As WebClient
    31. Public Sub MakeDownloadURL(ByVal url As String, ByRef output As String, Optional ByRef type As String = "", Optional ByRef size As Integer = 0)
    32. 'PERFORMS CLEANUP
    33. downloadurl = ""
    34. If request IsNot Nothing Then
    35. Try
    36. request.Abort()
    37. Catch
    38. End Try
    39. End If
    40. If response IsNot Nothing Then
    41. Try
    42. response.Close()
    43. Catch
    44. End Try
    45. End If
    46. Try
    47. source = ""
    48. Catch
    49. End Try
    50. request = DirectCast(HttpWebRequest.Create(url.ToString), HttpWebRequest)
    51. Try
    52. response = DirectCast(request.GetResponse, HttpWebResponse)
    53. Catch
    54. type = ""
    55. Exit Sub
    56. End Try
    57. source = New StreamReader(response.GetResponseStream, System.Text.Encoding.Default).ReadToEnd
    58. If source.IndexOf("video_id") > -1 Then
    59. 'This grabs the .flv location URL ready for doownloading.
    60. Dim signature As String = source
    61. If signature.Contains("&fmt_url_map=") = True Then
    62. Try
    63. signature = System.Text.RegularExpressions.Regex.Split(signature, "&fmt_url_map=")(1)
    64. Catch
    65. End Try
    66. ElseIf signature.Contains(Chr(34) + "fmt_url_map" + Chr(34) + ": " + Chr(34)) = True Then
    67. Try
    68. signature = System.Text.RegularExpressions.Regex.Split(signature, Chr(34) + "fmt_url_map" + Chr(34) + ": " + Chr(34))(1)
    69. Catch
    70. End Try
    71. Else
    72. Debug.WriteLine("Critical Error: Download URL could not be produced.")
    73. type = ""
    74. Exit Sub
    75. End If
    76. 'This changes the URL to conform to internet standards eg. %20 = " "
    77. signature = signature.Remove(0, 5)
    78. signature = signature.Replace("%252C", ",")
    79. signature = signature.Replace("%2F", "/")
    80. signature = signature.Replace("%3D", "=")
    81. signature = signature.Replace("%3F", "?")
    82. signature = signature.Replace("%3A", ":")
    83. signature = signature.Replace("%26", "&")
    84. Dim sig2 As String = signature
    85. sig2 = sig2.IndexOf("http", 30)
    86. signature = signature.Remove(sig2, signature.Length - sig2)
    87. signature = signature.Remove(signature.IndexOf("%"), 7)
    88. 'Removes any remaining % characters from the string.
    89. signature = signature.Replace("%", "")
    90. 'Checks for and fixes any malformed http requests in the string.
    91. If signature.StartsWith("ttp://") Then
    92. signature = signature.Replace("ttp://", "http://")
    93. End If
    94. 'Checks for and Removes any remaining C characters from the string.
    95. If signature.Chars(signature.Length - 1) = "C" Then
    96. signature = signature.Remove(signature.Length - 1, 1)
    97. End If
    98. 'Checks for videos such as http://www.youtube.com/watch?v=LvLCvdeWcsw that would not download due to a new layout of download data.
    99. If signature.Contains("rv.2.rating") Then
    100. Dim n As Integer = signature.IndexOf(ChrW(34) + ",")
    101. signature = signature.Remove(n, signature.Length - n)
    102. End If
    103. 'PERFORMS CLEANUP
    104. If request2 IsNot Nothing Then
    105. Try
    106. request2.Abort()
    107. Catch
    108. End Try
    109. End If
    110. If response2 IsNot Nothing Then
    111. Try
    112. response2.Close()
    113. Catch
    114. End Try
    115. End If
    116. Try
    117. request2 = DirectCast(HttpWebRequest.Create(signature), HttpWebRequest)
    118. Catch
    119. type = ""
    120. Exit Sub
    121. End Try
    122. response2 = DirectCast(request2.GetResponse, HttpWebResponse)
    123. If response2.ContentType = "video/x-flv" Then
    124. type = ".flv"
    125. output = signature
    126. size = response2.ContentLength
    127. ElseIf response2.ContentType = "video/mp4" Then
    128. type = ".mp4"
    129. output = signature
    130. size = response2.ContentLength
    131. Else
    132. type = ""
    133. output = ""
    134. Exit Sub
    135. End If
    136. End If
    137. End Sub
    138. Private Sub dekodefenster_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    139. Label1.Text = youtubeurl
    140. Me.ShowInTaskbar = True
    141. Me.Visible = True
    142. Dim tup
    143. downloadbeendet = True
    144. Dim filetype As String = ""
    145. Dim size As Integer = 0
    146. MakeDownloadURL(youtubeurl, downloadurl, filetype, size)
    147. If filetype = ".flv" Then
    148. tup = ".flv"
    149. ElseIf filetype = ".mp4" Then
    150. tup = ".mp4"
    151. Else
    152. MsgBox("An Error occured and the requested file(s) could not be downloaded." + vbCrLf + "This is normally caused by changes to the You-Tube Website.", MsgBoxStyle.Critical)
    153. Exit Sub
    154. End If
    155. ' Dim saveurl
    156. Label2.Text = downloadurl
    157. Dim webb As New WebBrowser
    158. webb.Navigate(youtubeurl)
    159. Do While Not webb.ReadyState = WebBrowserReadyState.Complete
    160. Application.DoEvents()
    161. Loop
    162. If Not My.Computer.FileSystem.DirectoryExists("C:\Taskload") Then
    163. My.Computer.FileSystem.CreateDirectory("C:\Taskload")
    164. End If
    165. Dim title As String = webb.DocumentTitle.Remove(0, 10)
    166. title = title.Replace("""", "")
    167. title = title.Replace("*", "")
    168. title = title.Replace("?", "")
    169. title = title.Replace("|", "")
    170. title = title.Replace(">", "")
    171. title = title.Replace("<", "")
    172. title = title.Replace("\", "")
    173. title = title.Replace("/", "")
    174. title = title.Replace(":", "")
    175. saveurl = Form1.videopfad & "\" & title & tup
    176. Label1.Text = "Lade " & webb.DocumentTitle.Remove(0, 10)
    177. webb.Navigate("http://google.de")
    178. laden(New Uri(downloadurl), saveurl)
    179. 'dekodieren(saveurl, Form1.settings(6) & "/" & Path.GetFileNameWithoutExtension(saveurl) & ".mp3")
    180. saveding = saveurl
    181. End Sub
    182. Public Sub dekodieren(ByVal input, ByVal output)
    183. Dim ffmpeg As New Process
    184. ffmpeg.StartInfo.FileName = Einstellungen.ffmpegpfad.Text 'PFAD ZU FFMPEG das musst du nacher halt noch umändern..... ähm aber dann startet er DEIN ffmpeg und das is am arsch :D ja ersetz doch einfach des ffmpeg oO im projekttordner ? achso...au
    185. 'ffmpeg.StartInfo.UseShellExecute = False
    186. 'ffmpeg.StartInfo.RedirectStandardError = True
    187. 'ffmpeg.StartInfo.RedirectStandardOutput = True
    188. 'ffmpeg.StartInfo.CreateNoWindow = True
    189. Dim blablabla As String = Einstellungen.TextBox1.Text
    190. blablabla = blablabla.Replace("%input%", input)
    191. blablabla = blablabla.Replace("%bitrate%", Einstellungen.audiioquali.Value)
    192. blablabla = blablabla.Replace("%output%", output)
    193. ffmpeg.StartInfo.Arguments = blablabla
    194. ffmpeg.Start()
    195. 'If input.Contains(".flv") = True Then
    196. ' 'FFmpeg.StartInfo.Arguments = "<--- Hier sollten die Startargumente hin für FLV... ich hab die immer benützt: siehe Komme unten drunter"
    197. ' '" -i " + input + " -ab 128 -f mp3 " + Path.Combine(outputfolder, Path.GetFileNameWithoutExtension(input) + ".mp3")
    198. ' ffmpeg.StartInfo.Arguments = " -i " + input + " -ab 128 -f mp3 " + output
    199. ' ' FFmpeg.StartInfo.Arguments = " -i " + input + " -ar 44100 -ac 2 -ab 128 " + Path.Combine(outputfolder, Path.GetFileNameWithoutExtension(input) + ".mp3")
    200. 'Else
    201. ' ffmpeg.StartInfo.Arguments = (" -i """ & input & """ -acodec libmp3lame -ab 320kb -ac 2 -ar 44100" & " " & "" & output)
    202. 'End If
    203. 'ffmpeg.Start()
    204. 'MsgBox("ffmpeg gestartet")
    205. ffmpeg.WaitForExit()
    206. ffmpeg.Close()
    207. Me.Close()
    208. End Sub
    209. Public Sub laden(ByVal dlurl As Uri, ByVal surl As String)
    210. downloader = New Net.WebClient
    211. AddHandler downloader.DownloadFileCompleted, AddressOf dlfertig
    212. AddHandler downloader.DownloadProgressChanged, AddressOf httpClient_DownloadProgressChanged
    213. downloader.DownloadFileAsync(dlurl, surl)
    214. Timer1.Start()
    215. ' Label1.Text = "Datei: " & saveurl.ToString
    216. End Sub
    217. Private Sub dlfertig(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles downloader.DownloadFileCompleted
    218. If e.Cancelled = False Then
    219. Timer1.Stop()
    220. dekodieren(saveurl, Form1.settings(6) & "/" & Path.GetFileNameWithoutExtension(saveurl) & ".mp3")
    221. Else
    222. 'laden(downlurl, saveurl)
    223. End If
    224. End Sub
    225. Private Sub httpClient_DownloadProgressChanged(ByVal sender As Object, _
    226. ByVal e As System.Net.DownloadProgressChangedEventArgs) _
    227. Handles downloader.DownloadProgressChanged
    228. With ProgressBar1
    229. .Value = e.ProgressPercentage
    230. End With
    231. ' KB-Fortschrittanzeige im Label
    232. Dim TotalBytes As Long = CLng(e.TotalBytesToReceive / 1024)
    233. Dim Bytes As Long = CLng(e.BytesReceived / 1024)
    234. If TotalBytes < 1 Then TotalBytes = 1
    235. If Bytes < 1 Then Bytes = 1
    236. recentb = Bytes
    237. totalb = TotalBytes
    238. newfortschritt = Bytes
    239. Label2.Text = Bytes.ToString & " KB von " & TotalBytes.ToString & " KB @ " & downrate & " kB/s" 'heruntergeladen." ' & e.UserState
    240. End Sub
    241. Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    242. downrate = newfortschritt - oldfortschritt
    243. ' Label2.Text = recentb.ToString & " KB von " & totalb.ToString & " KB @ " & abc & " kB/s" 'heruntergeladen." ' & e.UserState
    244. oldfortschritt = newfortschritt
    245. If downrate > My.Settings.rate Then
    246. My.Settings.rate = downrate
    247. Form1.maxdown.Text = My.Settings.rate & " kB/s"
    248. Form1.NotifyIcon1.Text = "Taskload - Höchste Downloadrate: " & My.Settings.rate & "kB/s"
    249. End If
    250. End Sub
    251. End Class



    Sehr viele kommentare :D
    Du hast ein Webclient-Objekt namens downloader mit "Withevents" deklariert...

    VB.NET-Quellcode

    1. Private WithEvents downloader As WebClient

    ...du hast demnach schon einen Handler, der das DownloadFileCompleted-Event bearbeitet.

    VB.NET-Quellcode

    1. Private Sub dlfertig(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs) Handles downloader.DownloadFileCompleted

    ...dennoch fügst du noch einen hinzu...?

    VB.NET-Quellcode

    1. downloader = New Net.WebClient
    2. AddHandler downloader.DownloadFileCompleted, AddressOf dlfertig


    Wenn du zweimal beim Pizza-Fritzen anrufst, gibt's zweimal Pizza.

    änliche thema: [VB 2010] Mehrfaches starten eines Prozesses verhindern (siehe lösung von Mono)

    @alex: warum gibst du schon wieder einen Link auf einen anderen Beitrag, wenn hier schon eine eigene Lösung vorliegt?