Sign in to follow this  
Followers 0
peanutym

how to stop my infinite loop

22 posts in this topic

So i am having an issue with the GUI that when i open a file it runs an infinite loop of the function i called. I know that there is a better way i am just not sure how to run the GUI and have the actions work outside of a while loop.

Specific issue is that in the GUI where i do Fileopendialog then call GetPrice() it runs over and over none stop. I know that the function is puting the info there as i can see it but it just runs over and over. I have tried to make $file = 0 and exitloop in there but still doesnt seem to work.

Can someone help me out on a better way to run the GUI thats not a while loop so that this isnt ifinite? or is there a better way of calling this inside the while loop?

Thanks in advance

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Global $itemprice
Global $itempricelist
Global $file
Gui()
;GetPrice()
;GetTypeId()
;Get
Func Gui()
    Local $msg, $sortbutton
    GUICreate("Compare Items", 900, 600) ; will create a dialog box that when displayed is centered
    GUISetState(@SW_SHOW) ; will display an empty dialog box

;Menu
$filemenu = GUICtrlCreateMenu ("&File")
$fileopen = GUICtrlCreateMenuItem("Open", $filemenu)
$fileexit = GUICtrlCreateMenuItem("Exit", $filemenu)

; Label
GUICtrlCreateLabel("Price Range", 0, 0, 70, 20)
GUICtrlCreateLabel("Item ID", 70, 0, 70, 20)
GUICtrlCreateLabel("Buy Price", 140, 0, 70, 20)
GUICtrlCreateLabel("Sell Price", 210, 0, 70, 20)
GUICtrlCreateLabel("Min Difference", 280, 0, 80, 20)

$pricerange = GUICtrlCreateInput("", 0, 20, 70, 20)
$itemid = GUICtrlCreateInput("", 70, 20, 70, 20)
$buyprice = GUICtrlCreateInput("", 140, 20, 70, 20)
$sellprice = GUICtrlCreateInput("", 210, 20, 70, 20)
$mindifference = GUICtrlCreateInput("", 280, 20, 80, 20)
$sortbutton = GUICtrlCreateButton("Sort", 360, 20, 40, 20)

$itempricelist = GUICtrlCreateList("", 0, 80, 900, 300) ; Box with all the info
$importbutton = GUICtrlCreateButton("Import", 360, 400, 100, 40) ;import button


   GUICtrlSetData ($itempricelist, $itemprice)
 
   
; Run the GUI until the dialog is closed
While 1
      $msg = GUIGetMsg()
      If $msg = $GUI_EVENT_CLOSE Then
   ExitLoop
   EndIf
  
   If $msg = $fileopen Then
   $file = FileOpenDialog("Choose File", "D:\", "Text Files (*.*)")
   GetPrice($file)
   EndIf
  
   If $msg = $fileexit Then
   ExitLoop
   EndIf
  
   WEnd
   GUIDelete()
EndFunc   ;==>Gui
Func GetPrice($file)
;did file open and ok for reading
If $file = -1 Then
   MsgBox(0, "error", "Unable to open file.")
   Exit
EndIf
While 1
   $itemprice = FileReadLine($file)
   If @error = -1 Then ExitLoop
      $price = StringSplit($itemprice, ",", 0)
   ;MsgBox (0, "Price", $price[1]) ;Testing to see what line i needed and if it worked.
   GUICtrlSetData ($itempricelist, $price[1])
   WEnd
EndFunc

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Func GetPrice($file)
    $hFileHandle = FileOpen($file, 0)
    ;did file open and ok for reading
    If $hFileHandle = -1 Then
        MsgBox(0, "error", "Unable to open file.")
        Exit
    EndIf
    While 1
        $itemprice = FileReadLine($hFileHandle)
        If @error = -1 Then ExitLoop
        $price = StringSplit($itemprice, ",", 0)
        ;MsgBox (0, "Price", $price[1]) ;Testing to see what line i needed and if it worked.
        GUICtrlSetData ($itempricelist, $price[1])
    WEnd
    FileClose($hFileHandle)
EndFunc

Edited by czardas

Share this post


Link to post
Share on other sites

ahh yes fileclose, thank you kind sir!

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Actually it was a bit more than that. The line count was not being incremented and I couldn't figure out why until I noticed that the help file example was using a file handle created by FileOpen() rather than the actual file path. It seemed odd that it would almost work using the path, but because it kept reading the first line it could never exit the loop. Strange behaviour. ;)

Anyway it seems okay now. :)

Edited by czardas

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

yep works great thanks again.

i do have another question if you dont mind.

On the line in the function that has the

GUICtrlSetData ($itempricelist, $price[1])

i changed it to

GUICtrlSetData ($itempricelist, $price[1] & $price[3] & $price[8])

how do i format this? where do i need to look for that. Becuase its all run together and i need some spacing like a tab between or something.

Edited by peanutym

Share this post


Link to post
Share on other sites

If you want them on different lines in your Listbox, do this:

GUICtrlSetData ($itempricelist, $price[1] & "|" &  $price[3] & "|" &  $price[8])

If you want them all on the same line but spaced with a tab, do this:

GUICtrlSetData ($itempricelist, $price[1] & @TAB & $price[3] & @TAB & $price[8])

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

i had tried the | and that works for new line. But the @TAB does not. i copied yours to be sure i wasnt mistyping. It still runs them all together, no spacing.

do i need another include at the top to make it work?

Edited by peanutym

Share this post


Link to post
Share on other sites

What happens when you use the @TAB? What are you trying to get as an output in the listbox?


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

With @TAB there is no visual separation in the List control, although the character is present in the string.. TAB is not always the best choice because it is used in different ways by different controls. If you just use spaces instead of @TAB you can get the visual separation.

GUICtrlSetData ($itempricelist, $price[1] & "   " & $price[3] & "   " & $price[8])

You may consider using a different type of control such as a listView control which gives you separate columns.

Edited by czardas

Share this post


Link to post
Share on other sites

#10 ·  Posted

I rarely if ever use ListBoxes so I wasn't sure what the output would be.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#12 ·  Posted

looks like the spaces will do waht i want. tab had no reaction the output was somethign to the effect of

"123price456bid"

where i wanted

123 price 456 bid

thanks for the help.

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

also brew what do you use to show info like this? im curious as the more that i look at it i wish that it was setup more like a table so that it didnt have to be spaced out but just told what portion of the table to go to. im still pretty new with auto it so i dont know all of the options yet.

edit found what i was looking for. _insertcolumn works alot better than the spacing i was doing.

Edited by peanutym

Share this post


Link to post
Share on other sites

#15 ·  Posted

I'm partial to using ListViews, only because I have more control over the columns and I'm more familiar with them.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#16 ·  Posted

good thing cause that brings me to my next question. after reading more i found that list view is alot better for waht i wanted to do. I need to make a loop to make the info go from line to line as it finds it in the text file its reading. I am not sure how to go about it though. As you can see i changed it up so that the list view will add a new line in the list view. Currently the additem works and it is adding each line on a seperate line for the first item that i want. But the other 4 are adding everything to the first line.

So if i have a txt file that has

a, 1, b, 2, 3

c, 4, d, 5, 6

then it writes

a 4 d 5 6

c

instead of just adding the info of the correct lines it overwrites the first line everytime.

Func GetPrice($file)
    $hFileHandle = FileOpen($file, 0)
    ;did file open and ok for reading
    If $hFileHandle = -1 Then
        MsgBox(0, "error", "Unable to open file.")
        Exit
    EndIf
    While 1
        $itemprice = FileReadLine($hFileHandle)
        If @error = -1 Then ExitLoop
        $price = StringSplit($itemprice, ",", 0)
        ;MsgBox (0, "Price", $price[1]) ;Testing to see what line i needed and if it worked.
        ;GUICtrlSetData ($itempricelist, $price[1] & $price[3] & $price[8] & $price[12] & $price[13])
     _GUICtrlListView_AddItem($itempricelist, $price[5], 1)
        _GUIctrlListView_AddSubItem($itempricelist, 0, $price[7], 1)
        _GUIctrlListView_AddSubItem($itempricelist, 0, $price[8], 2)
        _GUIctrlListView_AddSubItem($itempricelist, 0, $price[2], 7)
        _GUIctrlListView_AddSubItem($itempricelist, 0, $price[3], 8)
    WEnd
    FileClose($hFileHandle)
 EndFunc

Share this post


Link to post
Share on other sites

#17 ·  Posted (edited)

What function did you use to create the ListView? Please post all the code. How many columns do you need? you need to increment the second parameter in the function GUICtrlList_AddSubItem.

Edited by czardas

Share this post


Link to post
Share on other sites

#18 ·  Posted

9 columns total. only need info that is listed for now. the others will be filled with math later. so i am hoping to use anything you give me here later to show the other averages and whatnot later on.

thanks again for your help with this.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <GuiImageList.au3>
Global $itemprice
Global $itempricelist
Global $file
Gui()
Func Gui()
    Local $msg, $sortbutton, $hListView
    GUICreate("Compare Items", 900, 600) ; will create a dialog box that when displayed is centered
    GUISetState(@SW_SHOW) ; will display an empty dialog box
$itempricelist = GUICtrlCreateListView("", 0, 80, 900, 300) ; Box with all the info

;Menu
$filemenu = GUICtrlCreateMenu ("&File")
$fileopen = GUICtrlCreateMenuItem("Open", $filemenu)
$fileexit = GUICtrlCreateMenuItem("Exit", $filemenu)
   ; Add columns
    _GUICtrlListView_InsertColumn($itempricelist, 0, "Name", 100)
    _GUICtrlListView_InsertColumn($itempricelist, 1, "Buying Price", 100)
    _GUICtrlListView_InsertColumn($itempricelist, 2, "Selling Price", 100)
_GUICtrlListView_InsertColumn($itempricelist, 3, "Profit %", 100)
    _GUICtrlListView_InsertColumn($itempricelist, 4, "Average Volume", 100)
    _GUICtrlListView_InsertColumn($itempricelist, 5, "Total Profit", 100)
_GUICtrlListView_InsertColumn($itempricelist, 6, "Other Sellers", 100)
    _GUICtrlListView_InsertColumn($itempricelist, 7, "Region", 100)
    _GUICtrlListView_InsertColumn($itempricelist, 8, "System", 100)

   GUICtrlSetData ($itempricelist, $itemprice)
 
   
; Run the GUI until the dialog is closed
While 1
      $msg = GUIGetMsg()
      If $msg = $GUI_EVENT_CLOSE Then
   ExitLoop
   EndIf
  
   If $msg = $fileopen Then
   $file = FileOpenDialog("Choose File", "D:documentseve onlinemarket dump", "Text Files (*.*)")
   GetPrice($file)
   EndIf
  
   If $msg = $fileexit Then
   ExitLoop
   EndIf
  
   WEnd
   GUIDelete()
EndFunc   ;==>Gui

Func GetPrice($file)
    $hFileHandle = FileOpen($file, 0)
    ;did file open and ok for reading
    If $hFileHandle = -1 Then
        MsgBox(0, "error", "Unable to open file.")
        Exit
    EndIf
    While 1
        $itemprice = FileReadLine($hFileHandle)
        If @error = -1 Then ExitLoop
        $price = StringSplit($itemprice, ",", 0) ; split string into array
  ; add info into the list view from $price
     _GUICtrlListView_AddItem($itempricelist, $price[5], 1)
  _GUIctrlListView_AddSubItem($itempricelist, 0, $price[7], 1)
  _GUIctrlListView_AddSubItem($itempricelist, 0, $price[8], 2)
  _GUIctrlListView_AddSubItem($itempricelist, 0, $price[2], 7)
  _GUIctrlListView_AddSubItem($itempricelist, 0, $price[3], 8)
    WEnd
    FileClose($hFileHandle)
EndFunc

Share this post


Link to post
Share on other sites

#19 ·  Posted (edited)

Because you are using ListView UDF functions, it's better to create the ListView control using _GUICtrlListView_Create() The ListView UDF functions all begin with _GUICtrlListView_ followed by the rest of the function name. Although the changes I made to the function seemed to work with the native function GUICtrlCreateListView() The native and UDF functions don't always work together so well.

Anyway there are a few changes. You should to study the help file examples to see what you can do with the listview control.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <GuiImageList.au3>
Global $itemprice
Global $itempricelist
Global $file
Gui()
Func Gui()
    Local $msg, $sortbutton, $hListView
    $hGUI = GUICreate("Compare Items", 900, 600) ; will create a dialog box that when displayed is centered

    $itempricelist = _GUICtrlListView_Create($hGUI, "Name", 0, 80, 900, 300) ; Box with all the info
    _GUICtrlListView_SetColumnWidth($itempricelist, 0, 100)

    ;Menu
    $filemenu = GUICtrlCreateMenu ("&File")
    $fileopen = GUICtrlCreateMenuItem("Open", $filemenu)
    $fileexit = GUICtrlCreateMenuItem("Exit", $filemenu)
   ; Add columns
    ;_GUICtrlListView_InsertColumn($itempricelist, 0, "Name", 100)
    _GUICtrlListView_InsertColumn($itempricelist, 1, "Buying Price", 100)
    _GUICtrlListView_InsertColumn($itempricelist, 2, "Selling Price", 100)
    _GUICtrlListView_InsertColumn($itempricelist, 3, "Profit %", 100)
    _GUICtrlListView_InsertColumn($itempricelist, 4, "Average Volume", 100)
    _GUICtrlListView_InsertColumn($itempricelist, 5, "Total Profit", 100)
    _GUICtrlListView_InsertColumn($itempricelist, 6, "Other Sellers", 100)
    _GUICtrlListView_InsertColumn($itempricelist, 7, "Region", 100)
    _GUICtrlListView_InsertColumn($itempricelist, 8, "System", 100)

    GUICtrlSetData ($itempricelist, $itemprice)
    GUISetState(@SW_SHOW) ; will display an empty dialog box

    ; Run the GUI until the dialog is closed
    While 1
        $msg = GUIGetMsg()
        If $msg = $GUI_EVENT_CLOSE Or $msg = $fileexit Then
            ExitLoop
        ElseIf $msg = $fileopen Then
            $file = FileOpenDialog("Choose File", "D:documentseve onlinemarket dump", "Text Files (*.*)")
            GetPrice($file)
        EndIf
    WEnd
    ;GUIDelete()
EndFunc   ;==>Gui

Func GetPrice($file)
    $hFileHandle = FileOpen($file, 0)
    ;did file open and ok for reading
    If $hFileHandle = -1 Then
        MsgBox(0, "error", "Unable to open file.")
        Exit
    EndIf

    $iCount = 0 ; We need to count the item number.
    While 1
        $itemprice = FileReadLine($hFileHandle)
        If @error = -1 Then ExitLoop
        $price = StringSplit($itemprice, ",", 0) ; split string into array
        ; add info into the list view from $price
         _GUICtrlListView_AddItem($itempricelist, $price[5], 1)
         _GUIctrlListView_AddSubItem($itempricelist, $iCount, $price[7], 1)
         _GUIctrlListView_AddSubItem($itempricelist, $iCount, $price[8], 2)
         _GUIctrlListView_AddSubItem($itempricelist, $iCount, $price[2], 7)
         _GUIctrlListView_AddSubItem($itempricelist, $iCount, $price[3], 8)
         $iCount += 1 ; Increment item count
     WEnd
    FileClose($hFileHandle)
EndFunc

Edit

Just fixed the column width for the first column.

Edited by czardas

Share this post


Link to post
Share on other sites

#20 ·  Posted

thanks for that ill get working on it. gotta go to a bday party now so wont be till tomorrow.

Share this post


Link to post
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
Sign in to follow this  
Followers 0