Jump to content

High CPU usage


Recommended Posts

Hey forum!

Today I wrote my first AutoIt script and I noticed whenever I activate the "broadcasting" the cpu usage goes up to 15%. I was wondering why that is. The script does nothing else but read your currently playing song on Spotify or Winamp and broadcast it to ManyCam (I do not use a WinAmp Library of any sort, hence this only works with the old Winamp skin if anyone is wondering)

Here's my script:

#region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=Spotify_Icon2.ico
#AutoIt3Wrapper_Outfile=..\Spotify.exe
#AutoIt3Wrapper_Outfile_x64=..\Spotify_x64.exe
#AutoIt3Wrapper_Compression=4
#AutoIt3Wrapper_Compile_Both=y
#AutoIt3Wrapper_UseX64=y
#AutoIt3Wrapper_Res_Fileversion=1.0.0.3
#AutoIt3Wrapper_Res_Fileversion_AutoIncrement=y
#AutoIt3Wrapper_Res_requestedExecutionLevel=asInvoker
#AutoIt3Wrapper_Run_Tidy=y
#Tidy_Parameters=/rel
#AutoIt3Wrapper_Run_Obfuscator=y
#endregion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
TrayTip("", "PRESS NUMPAD0 TO QUIT", 1)
HotKeySet("{NUMPAD0}", "_Terminate")
; LOCAL VARS
Local $spotify = "[CLASS:SpotifyMainWindow]"
Local $manycam = "[CLASS:ManyCam_UI_Class]"
Local $winamp = "[CLASS:Winamp v1.x]"
$State_Old_Spotify = 0
$State_Old_ManyCam = 0
$State_Old_Winamp = 0
$State_Old_Use_Spotify = 0
$State_Old_Use_Winamp = 0
$State_Broadcast = 0
$State_ManyCam = 0
$State_Spotify = 0
$State_Winamp = 0
$hwndManyCam = 0
$hwndSpotify = 0
$hwndWinamp = 0
$bUseSpotify = 0
$bUseWinamp = 0
; GUI
Opt("GUIOnEventMode", 1)
$FMain = GUICreate("ManyCam - Now Playing", 235, 171, 358, 203)
GUISetFont(9, 400, 0, "Segoe UI")
$GApps = GUICtrlCreateGroup("Media Players", 8, 8, 217, 81)
$LSpotify = GUICtrlCreateLabel("Spotify:", 18, 28, 43, 17)
$LWinamp = GUICtrlCreateLabel("Winamp:", 18, 48, 51, 17)
$LSpotifyStatus = GUICtrlCreateLabel("not running", 104, 28, 77, 17)
$LManyCam = GUICtrlCreateLabel("ManyCam:", 18, 68, 57, 17)
$LWinampStatus = GUICtrlCreateLabel("not running", 104, 48, 85, 17)
$LManyCamStatus = GUICtrlCreateLabel("not running", 104, 68, 91, 17)
$RUseSpotify = GUICtrlCreateRadio("", 200, 28, 17, 17)
$RUseWinamp = GUICtrlCreateRadio("", 200, 48, 17, 17)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$LCurrentSong = GUICtrlCreateLabel("", 8, 96, 217, 26, BitOR($SS_CENTER, $SS_CENTERIMAGE, $WS_BORDER))
$BStartStop = GUICtrlCreateButton("Start Broadcasting", 8, 128, 153, 33)
$BExit = GUICtrlCreateButton("Exit", 168, 128, 57, 33)
; Properties
GUICtrlSetColor($LSpotifyStatus, 0xb22222)
GUICtrlSetColor($LWinampStatus, 0xb22222)
GUICtrlSetColor($LManyCamStatus, 0xb22222)
GUICtrlSetState($BStartStop, $GUI_DISABLE)
GUICtrlSetState($RUseSpotify, $GUI_DISABLE)
GUICtrlSetState($RUseWinamp, $GUI_DISABLE)
; Go
GUISetState()
; Events
GUISetOnEvent($GUI_EVENT_CLOSE, "OnExit")
GUICtrlSetOnEvent($BExit, "OnExit")
GUICtrlSetOnEvent($BStartStop, "OnStartStop")
GUICtrlSetOnEvent($RUseSpotify, "OnUseSpotify")
GUICtrlSetOnEvent($RUseWinamp, "OnUseWinamp")
; Messages
GUIRegisterMsg($WM_COMMAND, "_WM_COMMAND")
While 1
CheckForPlayers()
Sleep(500) ; Idle around
WEnd
Func _Terminate()
Exit 0
EndFunc   ;==>_Terminate
Func CheckForPlayers()
$hwndSpotify = WinGetHandle($spotify)
$hwndManyCam = WinGetHandle($manycam)
$hwndWinamp = WinGetHandle($winamp)
If $hwndManyCam Then
  $State_ManyCam = 1
Else
  $State_ManyCam = 0
EndIf
If $hwndSpotify Then
  $State_Spotify = 1
Else
  $State_Spotify = 0
EndIf
If $hwndWinamp Then
  $State_Winamp = 1
Else
  $State_Winamp = 0
EndIf
If $State_ManyCam <> $State_Old_ManyCam Then
  If $State_ManyCam = 1 Then
   GUICtrlSetData($LManyCamStatus, "running")
   GUICtrlSetColor($LManyCamStatus, 0x228b22)
  Else
   GUICtrlSetData($LManyCamStatus, "not running")
   GUICtrlSetColor($LManyCamStatus, 0xb22222)
  EndIf
EndIf
If $State_Spotify <> $State_Old_Spotify Then
  If $State_Spotify = 1 Then
   GUICtrlSetData($LSpotifyStatus, "running")
   GUICtrlSetColor($LSpotifyStatus, 0x228b22)
   GUICtrlSetState($RUseSpotify, $GUI_ENABLE)
  Else
   GUICtrlSetData($LSpotifyStatus, "not running")
   GUICtrlSetColor($LSpotifyStatus, 0xb22222)
   GUICtrlSetState($RUseSpotify, $GUI_DISABLE)
  EndIf
EndIf
If $State_Winamp <> $State_Old_Winamp Then
  If $State_Winamp = 1 Then
   GUICtrlSetData($LWinampStatus, "running")
   GUICtrlSetColor($LWinampStatus, 0x228b22)
   GUICtrlSetState($RUseWinamp, $GUI_ENABLE)
  Else
   GUICtrlSetData($LWinampStatus, "not running")
   GUICtrlSetColor($LWinampStatus, 0xb22222)
   GUICtrlSetState($RUseWinamp, $GUI_DISABLE)
  EndIf
EndIf
If $State_Old_ManyCam <> $State_ManyCam Or $State_Old_Spotify <> $State_Spotify Or $State_Old_Winamp <> $State_Winamp Or $State_Old_Use_Spotify <> $bUseSpotify Or $State_Old_Use_Winamp <> $bUseWinamp Then
  If $State_ManyCam == 1 And ($State_Spotify == 1 Or $State_Winamp == 1) And ($bUseSpotify == 1 Or $bUseWinamp == 1) Then
   GUICtrlSetState($BStartStop, $GUI_ENABLE)
   GUICtrlSetData($LCurrentSong, GetCurrentTitle())
  Else
   GUICtrlSetState($BStartStop, $GUI_DISABLE)
   GUICtrlSetData($LCurrentSong, "")
  EndIf
EndIf
$State_Old_ManyCam = $State_ManyCam
$State_Old_Spotify = $State_Spotify
$State_Old_Winamp = $State_Winamp
$State_Old_Use_Spotify = $bUseSpotify
$State_Old_Use_Winamp = $bUseWinamp
EndFunc   ;==>CheckForPlayers
Func GetCurrentTitle()
If $bUseSpotify == 1 Then
  If WinGetTitle($hwndSpotify) == "Spotify" Then
   $title = ""
  Else
   $title = StringReplace(WinGetTitle($hwndSpotify), "Spotify - ", "", 1)
  EndIf
ElseIf $bUseWinamp == 1 Then
  $title = WinGetTitle($hwndWinamp)
  If StringLeft($title, 6) == "Winamp" Then
   $title = ""
  Else
   If StringRight($title, 15) == "Winamp [Paused]" Then
    $title = ""
   Else
    $title = StringTrimRight(StringMid($title, StringInStr($title, " ", 0, 1) + 1), 9)
   EndIf
  EndIf
EndIf
Return $title
EndFunc   ;==>GetCurrentTitle
Func OnStartStop()
If ($State_Broadcast) Then
  GUICtrlSetState($RUseSpotify, $GUI_DISABLE)
  GUICtrlSetState($RUseWinamp, $GUI_DISABLE)
  GUICtrlSetData($BStartStop, "Stop Broadcasting")
  ; Send current song to ManyCam
  $currentSong = GetCurrentTitle()
  ControlSetText($hwndManyCam, "", "[CLASS:RichEdit20W]", $currentSong)
  GUICtrlSetData($LCurrentSong, $currentSong)
  Do
   If $currentSong <> GetCurrentTitle() Then
    $currentSong = GetCurrentTitle()
    ControlSetText($hwndManyCam, "", "[CLASS:RichEdit20W]", $currentSong)
    GUICtrlSetData($LCurrentSong, $currentSong)
    Sleep(1000)
   EndIf
  Until Not $State_Broadcast
Else
  ControlSetText($hwndManyCam, "", "[CLASS:RichEdit20W]", "")
  $State_Broadcast = 0
  GUICtrlSetData($BStartStop, "Start Broadcasting")
  If $State_Spotify Then
   GUICtrlSetState($RUseSpotify, $GUI_ENABLE)
  EndIf
  If $State_Winamp Then
   GUICtrlSetState($RUseWinamp, $GUI_ENABLE)
  EndIf
EndIf
EndFunc   ;==>OnStartStop
Func OnUseSpotify()
$bUseSpotify = 1
$bUseWinamp = 0
EndFunc   ;==>OnUseSpotify
Func OnUseWinamp()
$bUseSpotify = 0
$bUseWinamp = 1
EndFunc   ;==>OnUseWinamp
Func OnExit()
Exit 0
EndFunc   ;==>OnExit
Func _WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
Switch BitAND($wParam, 0x0000FFFF)
  Case $BStartStop
   $State_Broadcast = Not $State_Broadcast
  Case $BExit
   $State_Broadcast = Not $State_Broadcast
EndSwitch
Return $GUI_RUNDEFMSG
EndFunc   ;==>_WM_COMMAND

(Tab indents don't seem to be 100% correct here)

This is probably really bad code and I apologise in advance!

Link to comment
Share on other sites

Not tested but this could be likely the reason.

In the Do Until loop you constantly execute GetCurrentTitle() with no sleep in between.

Do
    If $currentSong <> GetCurrentTitle() Then
        $currentSong = GetCurrentTitle()
        ControlSetText($hwndManyCam, "", "[CLASS:RichEdit20W]", $currentSong)
        GUICtrlSetData($LCurrentSong, $currentSong)
        Sleep(1000)
    EndIf
    Sleep(500); <--- Insert Sleep here
Until Not $State_Broadcast

and hey, for your first Autoit script this is pretty slick! ;)

Edited by qsek
Teamspeak 3 User Viewer - Quick and functional TS3 Query script, which shows online users.Cached Screenshot Deleter - Deletes older Fraps Screenshots if they exceed a specified limit.Unresolved Topics:Intercept and modify dragdrop text behaviour in scite
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...