Executable restarts after END statement

Just starting out? Need help? Post your questions and find answers here.
jessedavis
New User
New User
Posts: 5
Joined: Sun Feb 05, 2023 6:55 pm

Executable restarts after END statement

Post by jessedavis »

I'm new to all this. I wrote a short program:

Code: Select all

  If OpenConsole()
    Print("<> ")
    ikey$ = Input()
    End  
  EndIf
Works as expected F/5. Then I generate an executable (.exe) . The executable does not stop on the END statement. It restarts the program, opening another console window. Looking at the docs for what END does it tells me that it "Ends the program correctly".

I don't know what to do and I need the generated .exe files to work correctly.

Thanks in advance for your help.

// Code-Tags added (Kiffi)
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Executable restarts after END statement

Post by infratec »

Works here without problems.

PB 6.01b3 x86 ASM on Windows 10 x64.

Maybe you shold tell us your environment.
jessedavis
New User
New User
Posts: 5
Joined: Sun Feb 05, 2023 6:55 pm

Re: Executable restarts after END statement

Post by jessedavis »

Sorry, I always forget; Windows 10 PureBasic 6.0.0.0 LTS (Windows x64) 6/28/2022 5.16 MB Tried on 2 different machines.
thanks,
jd
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Executable restarts after END statement

Post by Oso »

A bit of a longshot, but I wonder if it's being caused by a sticking Enter key, in that you're hitting Enter to terminate the running code, then presumably it is returning you to the Windows file explorer from where you ran it. If the key is misbehaving, it might just run that executable again. By contrast, when you were running it through the IDE (by pressing F5), you wouldn't have found the same symptom, since it returned you back to your code. Just an idea, on the offchance it might help you. If not, it is undoubtedly an environment problem with the operating system, or with the machine. PureBasic doesn't do this.
jessedavis
New User
New User
Posts: 5
Joined: Sun Feb 05, 2023 6:55 pm

Re: Executable restarts after END statement

Post by jessedavis »

OK, I think I know the answer. The .exe file is started by clicking on the file name in explorer. All OK. The PureBasic routine starts OK. Console window is created but Windows OS is watching the Explorer window NOT the newly created console window. When you hit enter it is captured by Explorer which thinks I want to execute the highlighted file. Nothing ever gets sent to the PureBasic console window (application). This is, I am pretty sure, a bug. PureBasic is watching the wrong buffer. This only happens when the routine is compiled. It works OK when run from inside the IDE. I can't explain why it runs on your machine but not mine except that your version of PureBasic is slightly newer.
BarryG
Addict
Addict
Posts: 3292
Joined: Thu Apr 18, 2019 8:17 am

Re: Executable restarts after END statement

Post by BarryG »

jessedavis wrote: Sun Feb 05, 2023 9:14 pmI can't explain why it runs on your machine but not mine except that your version of PureBasic is slightly newer.
Rule #1 in red -> viewtopic.php?t=4701

Then report back if it's still happening.
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Executable restarts after END statement

Post by infratec »

I don't think that this has something to do with the version of PB.

Btw. have you set the compiler option to console?
Standard is windows.
User avatar
DeanH
Enthusiast
Enthusiast
Posts: 223
Joined: Wed May 07, 2008 4:57 am
Location: Adelaide, South Australia
Contact:

Re: Executable restarts after END statement

Post by DeanH »

jessedavis wrote:
OK, I think I know the answer....
That's what I see, too. When I run the program from File Explorer it opens but the focus remains on the File Explorer with the highlighted line. Pressing enter just re-runs the program again. If you click on the open console window it then behaves as expected.

I don't think this is a bug in PB but rather the way Windows behaves. I have seen this same behaviour if I open a console window from File Explorer instead of a desktop icon I have created. I was able to get this little program to work properly when I compiled the exe in Console format rather than Windows format. This is set in Compiler Options before compiling.

Aside: I have found that opening some programs from File Explorer may not correctly set the start-in path the way an icon can. I have also found that using drag-and-drop to make a desktop icon from a mapped drive often generates a UNC address style instead of the drive letter, if the icon properties are examined. Using the right-click Send To method, however, always works to make a correct desktop icon with the correct path.
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Executable restarts after END statement

Post by Little John »

I compiled the code from the first post of this thread to an executable (console format), using PB 6.00 LTS and PB 6.01 beta 3.
When launching the executable (on Windows 11), the program works as expected, and it does NOT restart here.
User avatar
NicTheQuick
Addict
Addict
Posts: 1224
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Executable restarts after END statement

Post by NicTheQuick »

Look into your task manager and see if the PID changes or if it keeps the same.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
jessedavis
New User
New User
Posts: 5
Joined: Sun Feb 05, 2023 6:55 pm

Re: Executable restarts after END statement

Post by jessedavis »

Further info to help with confusion. Steps in procedure:

1. Source code in PB IDE. Note that I have modified the code from the original to assist in exploration:

Code: Select all

 If OpenConsole()
    Print("<> ")
    Repeat
      Key$ = Inkey()  
      If Key$ <> "" 
        PrintN(Key$)           
      Else
        Delay(20) ;time for breathing
      EndIf
    Until Key$ = Chr(27) ;escape
    ikey$ = Inkey()
    Print( ikey$)
    Delay (1000)  ;pause to collect thoughts & watch what happens
    CloseConsole()
  End  
  EndIf
2. Click on compile/run in PB IDE. Control goes to executing code in console window as is normal. Everything works correctly.
End of behavior #1.
3. In PB IDE click on Compiler/Create Executable with no changes to Compiler settings - everything default. Save output to file system. All OK, so far.
4. Navigate to file location and click on .exe file (PB IDE still running in another window). .exe file executes opening console window.
5. NOT working correctly. Keyboard is not connected to this process. I can type away and no characters appear in console window.
6. Click on console window (top drag bar). Now keyboard is connected to the console window and code works as expected.
This is not correct. In every other piece of software when I start it from the file explorer window control is transferred to the newly created window. NOT HERE. You might ask why I care. Just click on the window. Well, the customer (neophyte programmer) will be lost when nothing happens as expected when he tries to use the program. Notice that I try to activate the console window by printing "<>" to it. This has no effect on keyboard input.
I am reasonably sure this is not a normal Windows behavior or is it a Windows problem. I am reluctant to report this as a bug in PB because I am not sure. I could be doing something stupid.
I need in order to (for this application) create an .exe file as it is unreasonable to ask customers to install PB and run inside the IDE.

Note that the software performs identically on a second completely independent machine.

Thanking you in advance,
jd

// Code-Tags added (Kiffi)
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Executable restarts after END statement

Post by Oso »

jessedavis wrote: Mon Feb 06, 2023 4:09 pm Further info to help with confusion. Steps in procedure:
Hello Jesse, I have also found that the console window displays behind the explorer window. I'm not sure of the reason, but the below resolves it and allows you to position the window.

As you will see, there is a procedure called Set_Up_Console() defined at the start of the programme, after which your own main code appears (I have shown here the complete code, including your own). Within your small piece of code, is the call. You will find this works perfectly well and always displays the console in the foreground.

Code: Select all

; **
; ** Code courtesy of Mijikai, please see thread :
; ** https://www.purebasic.fr/english/viewtopic.php?p=590749#p590749
; **
Procedure Set_Up_Console()
  
  Define librno.i
  Define handle.i
  Define hwnd.i
  Define csbi.CONSOLE_SCREEN_BUFFER_INFO
  Define conoffset.i
  Define conlength.i
  Define cx, cy
  
  OpenConsole()

  librno.i=OpenLibrary(#PB_Any,"kernel32.dll")
  hwnd.i=CallFunction(librno.i,"GetConsoleWindow")
  CloseLibrary(librno.i)

  handle = GetStdHandle_(#STD_OUTPUT_HANDLE)
  If handle <> #INVALID_HANDLE_VALUE
    If GetConsoleScreenBufferInfo_(handle,@csbi)
      If SetConsoleTextAttribute_(handle,#BACKGROUND_BLUE|#BACKGROUND_GREEN|#BACKGROUND_RED)
        FillConsoleOutputAttribute_(handle,#BACKGROUND_BLUE|#BACKGROUND_GREEN|#BACKGROUND_RED,csbi\dwSize\x * csbi\dwSize\y,conoffset,@conlength)
      EndIf
    EndIf
  EndIf

  cx = GetSystemMetrics_(#SM_CXSCREEN);
  cy = GetSystemMetrics_(#SM_CYSCREEN);
  SetWindowPos_(hwnd,#Null,-5, 100,#Null,#Null,#SWP_NOSIZE|#SWP_NOZORDER)
  
EndProcedure

If OpenConsole()
  Set_Up_Console()
  Print("<> ")
  ikey$ = Input()
  End  
EndIf
EDIT : Credit original author of code
Last edited by Oso on Tue Feb 07, 2023 9:23 am, edited 1 time in total.
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Executable restarts after END statement

Post by Oso »

Just a brief note as a point of discussion for others on the forum, I only came to PB in July, at which time version 6.0 was already released, so therefore I'm not sure how previous PB versions behaved in this regard. I initially began working with console applications and I noticed that from the Windows file explorer, the compiled console executable always opened behind the explorer window. I didn't pay much attention to it at the time and I just lived with it, regarding it as nothing more than a quirk, but we've seen two people thrown by this behaviour — first was BrianW last week, which I guess was the same case and the OP of this thread.

I noticed that after I began using the Windows API to set the console colours and set an exact x, y screen position of the console (courtesy of Mijikai) the console always opened in the foreground, hence the suggestion to the OP above. Hope this is useful to know anyway.
jessedavis
New User
New User
Posts: 5
Joined: Sun Feb 05, 2023 6:55 pm

Re: Executable restarts after END statement

Post by jessedavis »

Thanks a lot. This appears to work as expected. I am not really sure how I would have figured this out from the docs without your help. :)
Oso
Enthusiast
Enthusiast
Posts: 595
Joined: Wed Jul 20, 2022 10:09 am

Re: Executable restarts after END statement

Post by Oso »

jessedavis wrote: Mon Feb 06, 2023 7:15 pm Thanks a lot. This appears to work as expected. I am not really sure how I would have figured this out from the docs without your help. :)
That's what the forum is here for Jesse. Probably others more enlightened than I am will be able to explain it, but I'm not sure why the console is invoked as a background window, or even if this only happens in certain cases. I'm sure we'll find out what's going on. :wink:
Post Reply