Need help with Random ganeration

02/09/2014 15:21 mlukac89#1
As title say i have next problem

Script need to works like this :
1. Open file (works)
2. When pressed start button, update status (works)
3. Need to count how many passwords are created (works) *FIXED
4. Need to loop all time when i press Start until i press Stop button (dont works)

Updated :

now script works it loop all time i edited it, but i cant drop it with Stop button, only shut it down in try icon....... :(

Whole script
Code:
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <File.au3>
#Region ### START Koda GUI section ### Form=

$Form1 = GUICreate("Form1", 483, 178, 201, 137)
$Group1 = GUICtrlCreateGroup("File path", 8, 8, 465, 65)
$browse = GUICtrlCreateButton("Browse", 24, 32, 75, 25)
$filePath = GUICtrlCreateLabel("", 112, 36, 346, 17)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group2 = GUICtrlCreateGroup("Passwords created", 8, 80, 465, 57)
$passwordsCreated = GUICtrlCreateLabel("", 24, 104, 430, 17)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$startBtn = GUICtrlCreateButton("Start", 8, 144, 75, 25)
$stopBtn = GUICtrlCreateButton("Stop", 88, 144, 75, 25)
$status = GUICtrlCreateLabel("Idle", 415, 151, 57, 18)
GUICtrlSetFont(-1, 9, 800, 0, "Verdana")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

Global $file, $filePath
Global $IsPaused = 1

While 1
	$nMsg = GUIGetMsg()
	Switch $nMsg
		Case $GUI_EVENT_CLOSE
			Exit

		Case $browse
			; browse for file and put it in label too see path
			$file = FileOpenDialog("Open file to write passwords", "", "(*.txt)", "1")
			GUICtrlSetData($filePath, $file)

		Case $startBtn
			; if file isn't opened
			If $file == "" Then
				MsgBox(48, "Error", "Please choose file to write in!")
			Else
				$IsPaused = -1
				GUICtrlSetData($status, "Running")

				; this need to loop until STOP button is pressed
				;For $i = 1 To 99
				Do
					If $IsPaused = -1 Then
						FileWrite($file, _RandomPassword(10) & @CRLF) ; write to file
						$countLines = _FileCountLines($file) ; count lines in file
						GUICtrlSetData($passwordsCreated, $countLines) ; update passwords filed
					EndIf
				Until False
				;Next
			EndIf

		Case $stopBtn
			_stop()

	EndSwitch
WEnd

; function to generate random passwords
Func _RandomPassword($length)
	$text = ""
	For $i = 1 To $length
		$text &= Chr(Random(65, 90, 1))
	Next
	Return $text
EndFunc

; function to stop executing script
Func _stop()
	$IsPaused = 1
	GUICtrlSetData($status, "Stoped")
EndFunc
Updated - help guys :( :( :(
02/09/2014 17:27 SpieleHacksInfo#2
Code:
Do
		If $IsPaused = -1 Then
		        FileWrite($file, _RandomPassword(10) & @CRLF) ; write to file
		        $countLines = _FileCountLines($file) ; count lines in file
        		GUICtrlSetData($passwordsCreated, $countLines) ; update passwords filed
 		EndIf
		$nMsg = GUIGetMsg()
		Switch $nMsg
		        Case $stopBtn
			_stop()
		EndSwitch
Until $nMsg = $stopBtn
but on thing: stop <> paused
02/09/2014 17:38 mlukac89#3
This dont works too first of all wont start and then you press stop button it closes program, i need when i press start to start loop untill i press stop but not to shut it down if you know what i mean

Compile my code and look on what i mean
02/09/2014 17:58 SpieleHacksInfo#4
Quote:
Originally Posted by mlukac89 View Post
This dont works too first of all wont start and then you press stop button it closes program, i need when i press start to start loop untill i press stop but not to shut it down if you know what i mean

Compile my code and look on what i mean
lol then you do something wrong.
learn the basic first

Code:
#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <File.au3>
#Region ### START Koda GUI section ### Form=

$Form1 = GUICreate("Form1", 483, 178, 201, 137)
$Group1 = GUICtrlCreateGroup("File path", 8, 8, 465, 65)
$browse = GUICtrlCreateButton("Browse", 24, 32, 75, 25)
$filePath = GUICtrlCreateLabel("", 112, 36, 346, 17)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group2 = GUICtrlCreateGroup("Passwords created", 8, 80, 465, 57)
$passwordsCreated = GUICtrlCreateLabel("", 24, 104, 430, 17)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$startBtn = GUICtrlCreateButton("Start", 8, 144, 75, 25)
$stopBtn = GUICtrlCreateButton("Stop", 88, 144, 75, 25)
$status = GUICtrlCreateLabel("Idle", 415, 151, 57, 18)
GUICtrlSetFont(-1, 9, 800, 0, "Verdana")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

Global $file, $filePath
Global $IsPaused = 1
Global $countLines = 0
While 1
	$nMsg = GUIGetMsg()
	Switch $nMsg
		Case $GUI_EVENT_CLOSE
			Exit

		Case $browse
			; browse for file and put it in label too see path
			$file = FileOpenDialog("Open file to write passwords", "", "(*.txt)", "1")
			GUICtrlSetData($filePath, $file)

		Case $startBtn
			If $file == "" Then
				MsgBox(48, "Error", "Please choose file to write in!")
			Else
				$IsPaused = -1
				GUICtrlSetData($status, "Running")

				; this need to loop until STOP button is pressed
				;For $i = 1 To 99
				Do
					If $IsPaused = -1 Then
						FileWrite($file, _RandomPassword(10) & @CRLF) ; write to file
						$countLines = _FileCountLines($file) ; count lines in file
						$countLines = $countLines + 1
						GUICtrlSetData($passwordsCreated, $countLines) ; update passwords filed
					EndIf
					$nMsg = GUIGetMsg()
					Switch $nMsg
						Case $stopBtn
			                _stop()
					EndSwitch
				Until $nMsg = $stopBtn
				;Next
			EndIf

		Case $stopBtn
			_stop()

	EndSwitch
WEnd

; function to generate random passwords
Func _RandomPassword($length)
	$text = ""
	For $i = 1 To $length
		$text &= Chr(Random(65, 90, 1))
	Next
	Return $text
EndFunc

; function to stop executing script
Func _stop()
	$IsPaused = 1
	GUICtrlSetData($status, "Stoped")
EndFunc
it wont help you learning this language if you asking for every little thing like loops
02/09/2014 18:11 mlukac89#5
Ok thx for tip but i still dont know why this must be in Do ... Until

Why dont works Do ... Until $IsPaused = 1 because that is function when you press Stop button, or Do ... Until $stopBtn

I know that it can be stoped with _IsPressed() function but you cant put variable in that function

This loops are too complicated :p

Code:
				Do
					If $IsPaused = -1 Then
						FileWrite($file, _RandomPassword(10) & @CRLF) ; write to file
						$countLines = _FileCountLines($file) ; count lines in file
						$countLines = $countLines + 1
						GUICtrlSetData($passwordsCreated, $countLines) ; update passwords filed
					EndIf
					$nMsg = GUIGetMsg() ; from here
					Switch $nMsg
						Case $stopBtn
			                _stop()
					EndSwitch ; to here
				Until $nMsg = $stopBtn