Forcing to finish - StartActivity

Shay

Well-Known Member
Licensed User
Longtime User
I have the following code

Sub Activity_Create(FirstTime As Boolean)

StartActivity(License)

Activity.LoadLayout("Main_Menu")



the problem is that it is loading the "Main_Menu" before it finished to run the "License" module

how to prevent it?
 

JesseW

Active Member
Licensed User
Longtime User
You could have a flag, like
B4X:
Dim LicenseDoneFlag As Boolean: LicenseDoneFlag = False
in Sub Process_Globals in your main activity. Then add this loop between the startactivity call for license and the loadlayout for the mainmenu
B4X:
Do While LicenseDoneFlag = False
    DoEvents
Loop

Then in your license activity, insert this line where it suits you best
B4X:
main.LicenseDoneFlag = True
which will satisfy the Do/Loop above and allow execution to fall thru to the loadlayout.

Hope this helps
 
Upvote 0

Shay

Well-Known Member
Licensed User
Longtime User
nice idea but, it is not working, never loading the license module...
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
You should not use this:
B4X:
Do While LicenseDoneFlag = False
    DoEvents
Loop

as it will tie up the processor and stop other events from running.

If you really need to wait for "License" to return you should set up a timer that checks periodically for the flag, and only load the main layout when the flag is set.

Also you may want to ony call "License" when "FirstTime" is true.
 
Upvote 0

JesseW

Active Member
Licensed User
Longtime User
You should not use this:
B4X:
Do While LicenseDoneFlag = False
    DoEvents
Loop

as it will tie up the processor and stop other events from running.

If you really need to wait for "License" to return you should set up a timer that checks periodically for the flag, and only load the main layout when the flag is set.

Also you may want to ony call "License" when "FirstTime" is true.

Excuse me??? What do you think DoEvents keyword does? It returns control to the message queue so waiting messages, or events, can be processed. Doing it the way below, ie without the DoEvents keyword, will tie up the processor

B4X:
Do While LicenseDoneFlag = False
Loop
 
Last edited:
Upvote 0

agraham

Expert
Licensed User
Longtime User
I'm afraid that I have to agree with kickaha. Looping, with or without the DoEvents, will occupy its processor timeslices 100% with or without messages to process. Suspending while waiting for a Timer is a better solution. On the other hand kickaha is not correct to say that events can't run during DoEvents. One reason for using DoEvents is to allow some other events to run. DoEvents uses the same mechanism to process messages as the modal Dialogs do and so allows view events to run just like a CustomDialog allows.
 
Upvote 0

thedesolatesoul

Expert
Licensed User
Longtime User
I have the following code

Sub Activity_Create(FirstTime As Boolean)

StartActivity(License)

Activity.LoadLayout("Main_Menu")



the problem is that it is loading the "Main_Menu" before it finished to run the "License" module

how to prevent it?

I think your method of loading activities is wrong.
If I were you, I would set the License activity as the Main activity and then call the Main activity.
But if you want to do it this way, you can also do it.
Create a new Sub in the Main activity that does all the initialising (loading layouts and stuff).
and once the License has been loaded in the other activity, the Main sub from License.
 
Upvote 0

JesseW

Active Member
Licensed User
Longtime User
Shay, it sounds like to me from your queries, that you want to validate a license, and only proceed further into the program if the license validation passes. If this is the case, why not have the license validation activity as your main activity, thus having it called first, and then on successful validation start the main program activity? Then, you can do it like this:

B4X:
If ValidationSuccessful = True Then StartActivity("program") 'my syntax may be off since I'm free typing
Activity.Finish    'kill THIS validation activity

This way, the main program activity will only get started upon successful validation, and whichever the case, the validation activity is released. If validation is not successful, the only open activity, the validation activity, will be closed thus closing the app. If successful, the program activity will start and the app will not close since it still has an open activity, the program activity.

Does this help?

Edit: I see someone else has the same thoughts as I. Thanks thedesolatesoul... :)
 
Upvote 0

Shay

Well-Known Member
Licensed User
Longtime User
I agree thedesolatesoul, but since I decided to all the license check too late, it is too much work to change everything now.

this did the trick: (combining the 2 solutions)

I added all my create activity code, below the license module load -
inside the timer that loads after licenese module set "true" to the flag

Meaning

Sub Activity_Create(FirstTime As Boolean)
StartActivity(License)
Time3.Enabled = True
End Sub

Sub Time3_Tick
If DoneLicense = True Then
Time3.Enabled = False
Activity.LoadLayout("Main_Menu")



Thank you all
 
Upvote 0

JesseW

Active Member
Licensed User
Longtime User
Shay, glad you got your issue worked out :)

...Looping, with or without the DoEvents, will occupy its processor timeslices 100% with or without messages to process. Suspending while waiting for a Timer is a better solution...

Question. Just so I know. Time to learn :) What is the downside to using 100% of my timeslice? If this were a Windows system, I could see it reducing the time spent in the Idle process. It would have no effect on the system as a whole, or negative effects on other processes would it? It might be a little higher drain on the battery?

The problem I see with using a timer is how often do you set it? If you set it to check often enough that the user can't see the time between their action and the _Tick event to fire, then shouldn't you just use a loop? If you set it long enough to offset the time difference, then the user might see a second or two, or perhaps longer, inbetween their action and the app responding. That wouldn't be good... I guess you could throw up a progress dialog during the execution of their action, then let the _Tick event bring it down to fake them out into thinking something is really going on :D
 
Last edited:
Upvote 0

Shay

Well-Known Member
Licensed User
Longtime User
I am using welcome screen that apears for 4 sec. in that 4 sec, I also check for updates and license

and I set the second timer (in the main) for 1/2 sec, so in worse case user will see black screen for 1/2 sec

looks to work fine, but still need to see the affect of removing all my code from the create and moving into the timer
(I will have to QA it...)
 
Upvote 0

kickaha

Well-Known Member
Licensed User
Longtime User
Excuse me??? What do you think DoEvents keyword does? It returns control to the message queue so waiting messages, or events, can be processed. Doing it the way below, ie without the DoEvents keyword, will tie up the processor

B4X:
Do While LicenseDoneFlag = False
Loop
You are excused!

As I understood things, using DoEvents like that does not allow the OS to process the messages fully.

A link to an earlier thread
 
Last edited:
Upvote 0

JesseW

Active Member
Licensed User
Longtime User
Well then, it seems I learned something today. Thanks for pointing out that thread. My apologies :)
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
What is the downside to using 100% of my timeslice? If this were a Windows system, I could see it reducing the time spent in the Idle process. It would have no effect on the system as a whole, or negative effects on other processes would it?
Possibly! By occupying an entire time slice you are denying the system and other applications those processor cycles. If a thread can't use its timeslice it suspends early and frees the system. Admittedly most PCs don't use all their cycles and spend most time idling, but even desktops have power saving code in the idle loop so be green - save the planet!

It might be a little higher drain on the battery?
Exactly, burning processor cycles on a mobile device burns battery life.
 
Upvote 0
Top