B4J Question DoEvents equivalent

Yafuhenk

Active Member
Licensed User
Longtime User
Hi,

When importing a csv file into a database I want to set the mouse pointer to WAIT.
Without a DoEvents to computer is too busy to show the this mouse pointer.
Is there an equivalent for DoEvents in Java, or a trick that I can use?
 

dilettante

Active Member
Licensed User
Longtime User
There is a traditional workaround for doing background work on the UI thread that was a staple of VB programming for a very long time. As described in the VB6 documentation, using DoEvents for this is considered a poor and hazardous "solution."

So if you don't want and don't need the overhead of additional worker threads you can do the same thing: use a timer to drive your background activity until completion. This also makes it easy to leave a "Cancel" button enabled, update a progress indicator, etc. while other controls are disabled to avoid potential havoc.

This can have the downside of making the background activity take a little longer to complete than if done using "straight line code" but uses far less machine resources than DoEvents (and without its dangers) and even less than spawning a worker thread.

You use a very short timer interval, something around 16ms or less. Anything to return back to the message loop and let it breathe.

Then you unroll your background activity into 3 parts:

  • Initialize your workload, change mouse cursor, disable most of the UI, and enable the timer.
  • Process a quantum of work in the timer event handler, e.g. import 100 records or something. Recognize the end of work, if so disable the timer and go on to Finalize.
  • Finalize. Close files, change mouse cursor back, etc. Re-enable the rest of the UI.
 
Upvote 0

romario87027

Member
Licensed User
Longtime User
When I do an insert I would like to see a progressdialog!

If SQL1.IsInitialized = False Then
SQL1.InitializeSQLite(File.DirApp, "data/gestioneordini.db", True)
End If

StrSql = "DELETE FROM BARCODE"
SQL1.ExecNonQuery(StrSql)

SQL1.BeginTransaction

For i = 0 To RSean.Size - 1
M= RSean.Get(i)
CodInt = M.get("COD_INT")
Ean = M.get("EAN")

SQL1.ExecNonQuery2("INSERT INTO BARCODE ( COD_INT,Ean ) VALUES(?,?)", Array As String(CodInt, Ean))




Next
SQL1.TransactionSuccessful
I can not use the progressbar and progressdialog in B4j. I Started by two days to use B4j. Thank you for responding
 
Upvote 0

romario87027

Member
Licensed User
Longtime User
are 65000 records takes about 30 seconds. I would like to see a
B4X:
Private progress As ProgressIndicator

Even when sending a request to the server, I wanted to use as a progressdialog in B4a

B4X:
Sub FetcharticoliList(info As String ,StrSql2 As String)
    'Gets all the available countries
    ExecuteRemoteQuery(StrSql2,info)
End Sub
On B4a progressdialog exists in B4j? I also try your suggestion. Thanks again
 
Upvote 0

romario87027

Member
Licensed User
Longtime User
Hello Erel,I followed your suggestion but it gives me error
B4X:
SQL1.BeginTransaction
For Each m As Map In RSean
CodInt = m.get("COD_INT")
Ean = m.get("EAN")
s = Array As String(CodInt, Ean)
SQL1.AddNonQueryToBatch("INSERT INTO  BARCODE ( COD_INT,Ean ) VALUES(?,?)",s )
Next

SQL1.ExecNonQueryBatch("SQL")

Device "Intel(R) HD Graphics" (\\.\DISPLAY1) initialization failed :
WARNING: bad driver version detected, device disabled. Please update your driver to at least version 8.15.10.2302
Program started.

Response from server: []
Exception in runnable
java.lang.RuntimeException: java.lang.Exception: Sub sql_nonquerycomplete was not found.
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:112)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:69)
at anywheresoftware.b4a.BA$3.run(BA.java:172)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:176)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:176)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:17)
at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:67)
at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.Exception: Sub sql_nonquerycomplete was not found.
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:100)
... 11 more
Thank
 
Upvote 0

romario87027

Member
Licensed User
Longtime User
B4X:
Sub SQL_NonQueryComplete (Success As Boolean)
    Log("NonQuery: " & Success)
    If Success = False Then Log(LastException)
End Sub
OK ,MISSING
 
Upvote 0
Top