Programm zieht 13% Auslastung obwohl es fast nix macht

  • VB.NET

Es gibt 8 Antworten in diesem Thema. Der letzte Beitrag () ist von ebrithil.

    Programm zieht 13% Auslastung obwohl es fast nix macht

    Hallo Vb-Paradise

    Ich bin am Ende angelangt und komme net weiter. Meine Software welche, alle Aktionen von Skype ausliesst und diese Per Serial-Port an einen Arduino UNO mit LCDisplay weitergibt, saugt nach kurzer Zeit immer 13% Auslastung obwohl es nix grosses macht, ausser alle 5 Sekunden den Skypestatus ausliesst.

    Vieleicht sehe ich den Fehler nicht und jemand kann mir dabei helfen:

    VB-Code

    VB.NET-Quellcode

    1. Option Strict On
    2. Imports SKYPE4COMLib
    3. Public Class Form1
    4. #Region "Globale Variablen"
    5. Dim Skype As Skype = New Skype
    6. Dim Time As String
    7. Dim old_Time As String
    8. Dim Read As String
    9. Dim Disp As Boolean = False
    10. Dim First As Boolean = True
    11. Dim Online As Boolean = False
    12. Dim Skype_Senders As New List(Of String)
    13. Dim Skype_Messages As New List(Of String)
    14. Dim Index As Integer
    15. Dim Blacklist As String = "Jonas Fe/2"
    16. Dim Status(0 To 1000) As Boolean
    17. Dim First_2 As Boolean = True
    18. #End Region
    19. #Region "Globale Funktionen"
    20. Private Sub sleep(ByVal milliseconds As Integer)
    21. Dim sw As New Stopwatch
    22. sw.Start()
    23. Do Until sw.ElapsedMilliseconds = milliseconds
    24. My.Application.DoEvents()
    25. Loop
    26. sw.Reset()
    27. End Sub
    28. Public Sub Check()
    29. Read = Nothing
    30. SP.Write(CStr(4))
    31. SP.Write(vbCr)
    32. End Sub
    33. Public Sub tog_light()
    34. Read = Nothing
    35. SP.Write(CStr(5))
    36. SP.Write(vbCr)
    37. End Sub
    38. Public Sub Write(ByVal message As String, ByVal line As Integer)
    39. Try
    40. Read = Nothing
    41. If SP.IsOpen = False Then
    42. SP.Open()
    43. End If
    44. SP.Write(CStr(line))
    45. For i As Integer = 0 To message.Length - 1
    46. Dim s As String
    47. s = message.Substring(i, 1)
    48. SP.Write(s)
    49. Next
    50. SP.Write(vbCr)
    51. sleep(50)
    52. Catch
    53. End Try
    54. End Sub
    55. Private _Encoding As System.Text.Encoding
    56. Public Sub New()
    57. InitializeComponent()
    58. _Encoding = System.Text.Encoding.Default
    59. End Sub
    60. Private Sub SP_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SP.DataReceived
    61. Dim ToRead As Integer = SP.BytesToRead
    62. If ToRead > 0 Then
    63. Dim Buffer(ToRead - 1) As Byte
    64. SP.Read(Buffer, 0, ToRead)
    65. Read &= _Encoding.GetString(Buffer)
    66. End If
    67. End Sub
    68. Private Sub ToolStripMenuItem3_Click(sender As Object, e As System.EventArgs) Handles ToolStripMenuItem3.Click
    69. NotifyIcon1.Dispose()
    70. End
    71. End Sub
    72. Private Sub try_Connect()
    73. For Each con As String In My.Computer.Ports.SerialPortNames
    74. Try
    75. If SP.IsOpen Then
    76. SP.Close()
    77. End If
    78. SP.PortName = con
    79. SP.Open()
    80. Check()
    81. Catch ex As Exception
    82. End Try
    83. sleep(150)
    84. If Not Read = Nothing Then
    85. If Read.Contains("Ebrithil-Display") Then
    86. Exit For
    87. End If
    88. End If
    89. Next
    90. Try
    91. Check()
    92. sleep(150)
    93. If Not Read = Nothing Then
    94. If Read.Contains("Ebrithil-Display") Then
    95. Disp = True
    96. Check_Disp()
    97. First = True
    98. If Online = True Then
    99. Online = False
    100. Else
    101. Online = True
    102. End If
    103. Else
    104. Disp = False
    105. Check_Disp()
    106. End If
    107. Else
    108. Disp = False
    109. Check_Disp()
    110. End If
    111. Catch ex As Exception
    112. Disp = False
    113. Check_Disp()
    114. End Try
    115. End Sub
    116. #End Region
    117. #Region "Initialisierung"
    118. Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    119. Me.Hide()
    120. For Each con As String In My.Computer.Ports.SerialPortNames
    121. Try
    122. If SP.IsOpen Then
    123. SP.Close()
    124. End If
    125. SP.PortName = con
    126. SP.Open()
    127. Check()
    128. Catch ex As Exception
    129. End Try
    130. sleep(150)
    131. If Not Read = Nothing Then
    132. If Read.Contains("Ebrithil-Display") Then
    133. Disp = True
    134. Check_Disp()
    135. Exit For
    136. End If
    137. End If
    138. Next
    139. Try
    140. Check()
    141. sleep(150)
    142. If Not Read = Nothing Then
    143. If Read.Contains("Ebrithil-Display") Then
    144. Disp = True
    145. Check_Disp()
    146. Else
    147. Disp = False
    148. Check_Disp()
    149. End If
    150. Else
    151. Disp = False
    152. Check_Disp()
    153. End If
    154. Catch ex As Exception
    155. Disp = False
    156. Check_Disp()
    157. End Try
    158. 'tm_Con.Start()
    159. Me.Hide()
    160. ini_Skype()
    161. My.Application.DoEvents()
    162. tm_Skype.Start()
    163. tm_Time.Start()
    164. tm_Skype_Status.Start()
    165. Write(" ", 1)
    166. Write(" ", 2)
    167. Write(" ", 3)
    168. End Sub
    169. #End Region
    170. #Region "Connection"
    171. Private Sub Check_Disp()
    172. If Disp = False Then
    173. NotifyIcon1.Text = "Ebrithil-Display not found"
    174. ToolStripMenuItem1.Text = NotifyIcon1.Text
    175. Else
    176. NotifyIcon1.Text = "Ebrithil-Display on " & SP.PortName
    177. ToolStripMenuItem1.Text = NotifyIcon1.Text
    178. End If
    179. End Sub
    180. #End Region
    181. #Region "Timer's"
    182. 'Zeit
    183. Private Sub tm_Time_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tm_Time.Tick
    184. Dim h, m, day, month, year As String
    185. h = CStr(My.Computer.Clock.LocalTime.Hour)
    186. If My.Computer.Clock.LocalTime.Minute.ToString.Length = 1 Then
    187. m = "0" & My.Computer.Clock.LocalTime.Minute
    188. Else
    189. m = CStr(My.Computer.Clock.LocalTime.Minute)
    190. End If
    191. If My.Computer.Clock.LocalTime.Day.ToString.Length = 1 Then
    192. day = "0" & My.Computer.Clock.LocalTime.Day
    193. Else
    194. day = CStr(My.Computer.Clock.LocalTime.Day)
    195. End If
    196. If My.Computer.Clock.LocalTime.Month.ToString.Length = 1 Then
    197. month = "0" & My.Computer.Clock.LocalTime.Month
    198. Else
    199. month = CStr(My.Computer.Clock.LocalTime.Month)
    200. End If
    201. year = CStr(My.Computer.Clock.LocalTime.Year)
    202. Time = " " & h & ":" & m & " " & day & "." & month & "." & year
    203. If First = True Then
    204. Try
    205. Write(Time, 0)
    206. First = False
    207. Catch ex As Exception
    208. End Try
    209. End If
    210. ToolStripMenuItem2.Text = CStr(Disp)
    211. If Not old_Time = Time Then
    212. If Disp = True Then
    213. Try
    214. Write(Time, 0)
    215. Catch ex As Exception
    216. End Try
    217. End If
    218. End If
    219. old_Time = Time
    220. End Sub
    221. #End Region
    222. #Region "Skype"
    223. Private Sub ini_Skype()
    224. Skype.Attach(7, False)
    225. AddHandler Skype.MessageStatus, AddressOf skype_MessageStatus
    226. End Sub
    227. Sub skype_MessageStatus(ByVal msg As ChatMessage, ByVal status As TChatMessageStatus)
    228. Try
    229. If status = TChatMessageStatus.cmsReceived Or status = TChatMessageStatus.cmsSent Then
    230. Dim Body As String = msg.Body
    231. Dim Name As String = msg.FromDisplayName
    232. If Not Blacklist.Contains(Name) And Not Body = Nothing Then
    233. If Body.Length > 40 Then
    234. Dim b As String = Body
    235. For i As Integer = 1 To CInt(Math.Round(Body.Length / 40 + 0.5))
    236. Skype_Senders.Add(Name & " writes:")
    237. Skype_Messages.Add(b)
    238. Try
    239. b = b.Remove(0, 40)
    240. Catch ex As Exception
    241. b = Nothing
    242. Body = Nothing
    243. End Try
    244. Next
    245. Else
    246. Skype_Senders.Add(Name & " writes:")
    247. Skype_Messages.Add(Body)
    248. End If
    249. If Online = True Then
    250. Online = False
    251. Else
    252. Online = True
    253. End If
    254. End If
    255. End If
    256. Catch ex As Exception
    257. End Try
    258. End Sub
    259. Private Sub tm_Skype_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tm_Skype.Tick
    260. If Disp = True Then
    261. If Index > Skype_Senders.Count - 1 Then
    262. Skype_Senders.Clear()
    263. Skype_Messages.Clear()
    264. Index = 0
    265. Else
    266. If Skype_Messages.Item(Index).Length > 20 Then
    267. Dim Message As String = Skype_Messages.Item(Index)
    268. Write(Message.Substring(0, 20), 2)
    269. Message = Message.Remove(0, 20)
    270. Write(Message, 3)
    271. Write(Skype_Senders.Item(Index), 1)
    272. Else
    273. Write(Skype_Senders.Item(Index), 1)
    274. Write(Skype_Messages.Item(Index), 2)
    275. Write(" ", 3)
    276. End If
    277. Index += 1
    278. End If
    279. End If
    280. End Sub
    281. Private Sub tm_Skype_Status_Tick(sender As System.Object, e As System.EventArgs) Handles tm_Skype_Status.Tick
    282. If First_2 = True Then
    283. Dim i As String = CStr(0)
    284. For Each user As SKYPE4COMLib.User In Skype.Friends
    285. If user.OnlineStatus = TOnlineStatus.olsOnline Then
    286. Status(CInt(i)) = True
    287. Else
    288. Status(CInt(i)) = False
    289. End If
    290. i = CStr(1 + CInt(i))
    291. Next
    292. First_2 = False
    293. Else
    294. Dim i As String = CStr(0)
    295. For Each user As SKYPE4COMLib.User In Skype.Friends
    296. If Status(CInt(i)) = True And Not user.OnlineStatus = TOnlineStatus.olsOnline Then
    297. Status(CInt(i)) = False
    298. If user.DisplayName = Nothing Then
    299. If user.FullName = Nothing Then
    300. Else
    301. Skype_Senders.Add(user.FullName & " is now:")
    302. Skype_Messages.Add(" Offline")
    303. End If
    304. Else
    305. Skype_Senders.Add(user.DisplayName & " is now:")
    306. Skype_Messages.Add(" Offline")
    307. End If
    308. End If
    309. If Status(CInt(i)) = False And user.OnlineStatus = TOnlineStatus.olsOnline Then
    310. Status(CInt(i)) = True
    311. If user.DisplayName = Nothing Then
    312. If user.FullName = Nothing Then
    313. Else
    314. Skype_Senders.Add(user.FullName & " is now:")
    315. Skype_Messages.Add(" Online")
    316. End If
    317. Else
    318. Skype_Senders.Add(user.DisplayName & " is now:")
    319. Skype_Messages.Add(" Online")
    320. End If
    321. End If
    322. i = CStr(1 + CInt(i))
    323. Next
    324. End If
    325. End Sub
    326. #End Region
    327. Private Sub ToolStripMenuItem4_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem4.Click
    328. try_Connect()
    329. End Sub
    330. End Class



    Download (1.4 MB) des ganzen Projekts

    Danke im Vorraus


    mfg Ebrithil
    @kinsi
    ja, aber ich glaube net das die 2 Timer, eine Auslastung von 13& erzielen, auf nem Intel I7 3.9 GHz

    @Samus Aran
    öhm :D Array für die Statusse der User zu speichern


    Edit: Hab die Intervalle mal hoch gepusht, immer noch die 13% -.-
    Staten. /trollface

    Arrays wurden in VB6 mal so deklariert, heutzutage macht man das so: Dim Status(1000) As Boolean
    Außerdem seh ich im Code Dinge wie If xyz = False oder If xyz = True.
    = True kannst du ohnehin weglassen und statt = False kannst du If Not xyz nutzen.
    In C# ist das alles imho viel eleganter gemacht.
    Lässt sich in den Sessel fallen, die Knöchel knacken und schreibt Assembler
    Zum eigentlichem Performanceproblem: Ich hab mir deinen Code jetzt nicht bis ins Detail angeschaut, aber was auf jeden Fall schlecht programmiert ist, ist die sleep-Funktion, denn du führst ständig die Funktion My.Application.DoEvents() aus, während dein Programm "wartet".
    Stattdessen ist es sinnvol, wenn du irgendeinen dauerhaften Vorgang hast (wie das Senden des Skypestatus), diesen in einem anderen Thread als im GUI-Thread auszuführen. Schau dir dazu die System.Threading.Thread-Klasse bzw. das Thema Multithreading an. In diesem Thread kannst du dann auch mit der normalen Thread.Sleep-Funktion aus dem Framework die 5 Sekunden abwarten, ohne dass sich das Form aufhängt.

    youtube.com/watch?v=rWKhEBsql0o
    msdn.microsoft.com/de-de/library/hyz69czz.aspx
    Hmm ok danke, werd ich mal machen


    So, irgendwie ziehts jetzt nimmer 13% sondern brav 0 ^^ ich hab kein Schimmer wie, aber an alle Danke die sich die Zeit genommen haben mir zu helfen.

    mfg Ebrithil