B4J Question Sleep problem

strupp01

Active Member
Licensed User
Longtime User
I have written a small test program to illustrate my problem.
The sleep command was for me only a stop of the program for a set time.
The MsgBox in my program appears, befor the program has read all the data. But I do not want that. Without entries from Sleep in the program everything runs correctly.
Sleep should only update the label entries in my program.
What am I doing wrong ?
 

Attachments

  • Sleep_Test_Ordner.zip
    484.1 KB · Views: 185

Erel

B4X founder
Staff member
Licensed User
Longtime User
Note that you should use File - Export as zip when uploading projects.

Calling Sleep or Wait For is equivalent to calling Return. The current sub returns and the code execution continues from the parent. The sub will be resumed at some point in the future.

My recommendation is to remove all the sleep calls and only make the top method a resumable sub:
B4X:
Sub Button1_MouseClicked (EventData As MouseEvent)
   Label1.Visible = True
   Label2.Visible = True
   Label3.Visible = True


   Start_Zeit = DateTime.Time(DateTime.Now)
   Label2.Text = Start_Zeit
   Sleep(0)

   For z = 1 To 3
     Label2.Text = z & ". Hauptschleife Start"
     Sleep(0)
     Log(z & ". Hauptschleife Start")
     Daten_holen
   Next
   
   Dim res As Int = fx.Msgbox2(MainForm, "Möchten sie weitere Daten einlesen oder das Programm beenden ?", "Abfrage", "Weiter Einlesen", "", "Programm beenden",fx.MSGBOX_WARNING)
   If res = fx.DialogResponse.POSITIVE Then
   'Fenster wird geschlossen und es können neue Daten eingelesen werden
     Button1.Visible = True
   
   Else
     Close_Main     
   End If
End Sub
This way the msgbox will show after all tasks complete.
 
Upvote 0

strupp01

Active Member
Licensed User
Longtime User
Hello Erel,
Thank you for your answer. But still have problems with it.

I have not used file export, since the 2 TxT files are not taken with.

If I remove all sleep (0), the program runs without error. However, the label output is not displayed at runtime. Is this not possible? If so, can you change my program briefly and send me? I hope it is not too much effort.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I have not used file export, since the 2 TxT files are not taken with.
The files should be added to the Files tab (File.DirAssets).

I don't exactly understand your code. However if you want to call a resumable sub three times and wait for the three calls to complete then you can use Wait For:
B4X:
Sub Daten_holen
   For i = 1 To 3
     Einlesen.Daten_einlesen
   Next

   Ende_Zeit = DateTime.Time(DateTime.Now)
   Label2.Text = Ende_Zeit
   Sleep(0)
   For i = 1 To 3
     Einlesen.Daten_2_einlesen
   Next
   Ende_Zeit = DateTime.Time(DateTime.Now)
   Label2.Text = Ende_Zeit
   Sleep(0)
   CallSubDelayed(Me, "After_Daten_holen") 'There will be a warning here. Ignore it. It is a bug.
End Sub

Sub Button1_MouseClicked (EventData As MouseEvent)
   Label1.Visible = True
   Label2.Visible = True
   Label3.Visible = True


   Start_Zeit = DateTime.Time(DateTime.Now)
   Label2.Text = Start_Zeit
   Sleep(0)

   For z = 1 To 3
'     Sleep(0)
     Label2.Text = z & ". Hauptschleife Start"
     Sleep(0)
     Log(z & ". Hauptschleife Start")
     Daten_holen
   Next
   For i = 1 To 3
     Wait For After_Daten_holen
   Next
   Dim res As Int = fx.Msgbox2(MainForm, "Möchten sie weitere Daten einlesen oder das Programm beenden ?", "Abfrage", "Weiter Einlesen", "", "Programm beenden",fx.MSGBOX_WARNING)
   If res = fx.DialogResponse.POSITIVE Then
   'Fenster wird geschlossen und es können neue Daten eingelesen werden
     Button1.Visible = True
   
   Else
     Close_Main     
   End If
End Sub

You should avoid using global variables as the three instances of the resumable sub will run (almost) at the same time.
 
Upvote 0

strupp01

Active Member
Licensed User
Longtime User
I have tried your examples. The first variant does not run optimally. In the 1st For next loop, file 2 is not read. This happens only at the end, after the Msgbox has appeared.

The second variant is optimal from the outset.

The output of the times is also updated in the label with sleep (0). Only with the main loop, the label output "x. Main loop start" is rarely updated with sleep (0). This error occurs spontaneously until sleep (10). Only at sleep (20) I have a constant output of the label output "x. Main loop start". Is there an explanation ?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
The code you uploaded only reads this file: Test_data.txt. It doesn't read the other file.
Both Daten_einlesen and Daten_2_einlesen are executed. Make sure to remove the sleep calls from them.

Updating the same label from two different resumable subs is a mistake. You should move this line: Label2.Text = z & ". Hauptschleife Start" to Daten_holen and remove the Sleep(0) call.
 
Upvote 0

strupp01

Active Member
Licensed User
Longtime User
hat only the file Test_data.txt was read was an error of mine.
I moved the line Label2.Text = z & "Main loop start" and deleted the sleep (0). However, Label2.text is no longer updated.
I do not understand anything.:( B4A with doEvents is easier.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Don't use DoEvents in B4A if you want your code to work properly.

The code runs too fast so you are unable to see the label changes. Increase the sleep interval and it will work properly:
B4X:
Sub Daten_holen
   For i = 1 To 3
     Einlesen.Daten_einlesen
   Next

   Ende_Zeit = DateTime.Time(DateTime.Now)
   Label2.Text = Ende_Zeit
   Sleep(50)
   For i = 1 To 3
     Einlesen.Daten_2_einlesen
   Next
   Ende_Zeit = DateTime.Time(DateTime.Now)
   Label2.Text = Ende_Zeit
   Sleep(50)
   CallSubDelayed(Me, "After_Daten_holen")
End Sub

Sub Button1_MouseClicked (EventData As MouseEvent)
   Label1.Visible = True
   Label2.Visible = True
   Label3.Visible = True
   Start_Zeit = DateTime.Time(DateTime.Now)
   Label2.Text = Start_Zeit
   For z = 1 To 3
     Label2.Text = z & ". Hauptschleife Start"
     Sleep(50)
     Log(z & ". Hauptschleife Start")
     Daten_holen
     Wait For After_Daten_holen
   Next
  
Dim res As Int = fx.Msgbox2(MainForm, "Möchten sie weitere Daten einlesen oder das Programm beenden ?", "Abfrage", "Weiter Einlesen", "", "Programm beenden",fx.MSGBOX_WARNING)
   If res = fx.DialogResponse.POSITIVE Then
   'Fenster wird geschlossen und es können neue Daten eingelesen werden
     Button1.Visible = True
   
   Else
     Close_Main     
   End If
End Sub
 
Upvote 0

strupp01

Active Member
Licensed User
Longtime User
Thanks for your help and time
. The increase of the sleep-values I had already tried. Had only one statement from you in the head that would be enough to update a sleep (0).

So I can now take it into my current larger program.
 
Upvote 0
Top