[Done] Window moves by itself

Just starting out? Need help? Post your questions and find answers here.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Window moves by itself

Post by netmaestro »

Delay or Sleep_ for more than just a few milliseconds chokes off window event handling. This kind of behavior (and worse) can be expected to result. Code such as Delay(5000) should never appear in a program unless it's executing in a thread while the main loop continues on processing events.
BERESHEIT
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Window moves by itself

Post by Dude »

Yes, the snippet was just to show the problem but I overlooked the fact that the OS was the cause due to lack of events.

I've since used this procedure instead to fix the problem:

Code: Select all

Procedure WindowDelay(ms)
  ; Other stuff here not shown.
  timeout.q=ElapsedMilliseconds()+ms
  Repeat
    WaitWindowEvent(1)
    ; And also here.
  Until ElapsedMilliseconds()>timeout
EndProcedure

OpenWindow(1,10,10,50,50,"")
OpenWindow(2,100,10,50,50,"")
OpenWindow(3,200,10,50,50,"")

WindowDelay(2000)
Last edited by Dude on Sat Mar 18, 2017 9:52 am, edited 1 time in total.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: [Done] Window moves by itself

Post by netmaestro »

@PB/MachineCode/Dude et al: The logical emptiness is more than I can fathom ...Nevermind. Sorry I posted. Seriously, I wish I hadn't.
BERESHEIT
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: [Done] Window moves by itself

Post by Dude »

I don't know what you mean by emptiness? I have a valid use for a delay like that in my code, which is actually part of a larger routine; so don't judge it based on just an example snippet like this, which of course looks stupid on its own. :P Edited my post above to express that. We both know that snippets aren't intended to show production code, but just to show the resulting solution.
User avatar
nco2k
Addict
Addict
Posts: 1344
Joined: Mon Sep 15, 2003 5:55 am

Re: [Done] Window moves by itself

Post by nco2k »

sorry but thats just bad. your WindowDelay() procedure is totally inefficient. it unnecessarily slows down the processing of whatever you are trying to do. it unnecessarily uses up more cpu time than actually required. it unnecessarily blocks the user from interacting with the window and giving him the freedom to cancel the operation. im not going to tell you again, that you should write a responsive app and use a separate thread. its not my job to stop people from writing bad code. instead, im simply going to join netmaestro and drink a nice and cool beer. 8)

c ya,
nco2k
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: [Done] Window moves by itself

Post by Thunder93 »

I suppose using windows isn't big part of his code? Maybe I'm missing something but I don't understand why his WindowDelay() procedure would slow up, or use more CPU?

I don't understand why multiple windows would be controlled by a timer to be all closed. However with the little demonstration code, this to be expected anyways.

I've seen Windows or dialog screens automatically close after small period of time while waiting for user action. Like commonly seen with security software.

I'm not much of an alcohol drinker, but I do like Revs once in awhile. :wink:

Code: Select all

Procedure WindowDelay(ms)
  ; Other stuff here not shown.
  timeout.q=ElapsedMilliseconds()+ms
  Repeat
    Event = WaitWindowEvent(1)
    
    Select Event
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 11, 22, 33
            Debug "Window #:"+Str(EventWindow())+" Button Clicked"
        EndSelect
    EndSelect    
    
  Until ElapsedMilliseconds()>timeout
EndProcedure

OpenWindow(1,10,10,50,50,"") : ButtonGadget(11, 10, 10, 100, 20, "Standard Button")
OpenWindow(2,100,10,50,50,"") : ButtonGadget(22, 10, 10, 100, 20, "Standard Button")
OpenWindow(3,200,10,50,50,"") : ButtonGadget(33, 10, 10, 100, 20, "Standard Button")

WindowDelay(12000)
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
User avatar
nco2k
Addict
Addict
Posts: 1344
Joined: Mon Sep 15, 2003 5:55 am

Re: [Done] Window moves by itself

Post by nco2k »

because you have to wait for the ui to finish, before you can process the next data chunk.

a code like this will finish faster and require less cpu time:

Code: Select all

For i = 0 To x
  ;do actual stuff
Next
than a code that introduces a random delay from a ui:

Code: Select all

For i = 0 To x
  ;wait for unnecessary stuff
  ;do actual stuff
Next
10 years or so ago, i wrote a small server in pb, but didnt want to use a threaded loop that constantly checks for changes, because i thought it would be a waste of cpu time. instead i used a winapi function that would allow me to attach a network callback, that notifies my window every time there was a change. for fun, i wrote a client that attempted thousands of thousands connections at the same time and my server was able to process them all in just a couple of seconds. so the server was pretty fast and cpu friendly, what more could i ask for?

out of curiousity, i tried out a threaded loop anyway and dettached all the calculations from the window loop. i ran the client again, but this time my server was able to finish in just a couple of milliseconds. going from seconds to milliseconds was a HUGE difference in performance. the netcode didnt have to wait for the window to finish redrawing etc. and could go full speed when required. i did everything in the thread and used Send/PostMessage() to notify my window about the progress. i also checked if the user attempted to close the window and ended the thread gracefully from the inside, instead of killing it from the outside. and when there were no network events, i simply let my thread rest for few milliseconds.

c ya
nco2k
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: [Done] Window moves by itself

Post by Thunder93 »

Thanks nco2k for being patient with me.

I know the importance of using threads to avoid tying up the rest programs operations.

Anything that's remotely time-consuming should be handled in threads.

Since he uses OpenWindow(), processing window messages whether it's in the main part or wrapped up under a procedure like the example demonstrates. As long as WindowDelay() is basically bottom-most call. I see this no difference than demonstrations style used all throughout the PB help file.
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: Window moves by itself

Post by Thunder93 »

nco2k wrote:, but a window should be responsive and things that need a long time to process, should be put in a separate thread. i mean, you could put a window and its loop in one thread and
I don't remember when, PB supported OpenWindow() in Threads. Ignoring it's important message about avoiding use in threads in the PB help file.

However this is what I get when I tested with the debugger;

[15:29:34] [ERROR] OpenWindow(): OpenWindow() can only be called from the main thread.

:lol:
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
User avatar
nco2k
Addict
Addict
Posts: 1344
Joined: Mon Sep 15, 2003 5:55 am

Re: [Done] Window moves by itself

Post by nco2k »

ever heard of a thing called the main thread? you put the window and its loop in the main thread and do the time consuming calculations in a separate thread. then you can use messages, variables etc. to communicate. jesus f*cking christ man, i thought im on a programing forum... :?

c ya,
nco2k
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: [Done] Window moves by itself

Post by Thunder93 »

Remember we're in coding questions forum now. :)

On the PB board, we have people coming here with different skill levels. There be off-days for people. There be confusions. Whatever the circumstances may be, saying his name in vain is uncalled for. Just like being disrespectful, or degrading is also uncalled for. You had the option to avoid answering, or simple answer in a respectful manner.
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
User avatar
nco2k
Addict
Addict
Posts: 1344
Joined: Mon Sep 15, 2003 5:55 am

Re: [Done] Window moves by itself

Post by nco2k »

wow your public message is such the opposite of the private message you just sent me, where you thanked me for all my effort. :lol:

>> we have people coming here with different skill levels
this has nothing to do with skill, but being able to read properly.

>> saying his name in vain is uncalled for
well, god shouldnt have made me an atheist then. :P

>> Just like being disrespectful, or degrading is also uncalled for
i apologize for expecting basic knowledge and common sense on a programing forum.

>> or simple answer in a respectful manner.
yea, like i did for the last 3 pages and yet it didnt get us nowhere. :?

>> Don't think you above everyone else, and stop looking down at others who may or may not know as much
oh buddy. :) this has nothing to do with not knowing. i will gladly help someone who doesnt know, but i cant help someone who doesnt want to adapt and accept basic guidelines for good code.

if you feel insulted by what i wrote earlier, then i hope for your sake that you dont visit other programing sites. people there nowhere near as patient and respectful as i am. but whatever.. everything has been said. im done with this thread and for all i care, it can be locked up.

c ya,
nco2k
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: [Done] Window moves by itself

Post by Thunder93 »

Hi. after my post. You responded to my PM, and read and made change to the post after I understood little better. You are quoting reductant message. :wink:

Edit: True I thanked you and that still stands. What I've PMed you still stands tall.
... You really seem to try to help the community, and PB development. You typically explain things very clearly and calmly, so kudos to you.
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: [Done] Window moves by itself

Post by Dude »

nco2k wrote:sorry but thats just bad. your WindowDelay() procedure is totally inefficient.
As I pointed out above in my reply to netmaestro, that's not the entire production code or the actual procedure being used. Just a quick example. Don't be so quick to judge. :)
Post Reply