Trying to update text area while 'stuff' is executing in main window

Hi

I have a window in which one can define multiple database connections.
On that window is a ‘Connect All’ button.
On the same window is a textArea object which I want to write logging info.
This button then loops through the possible connections and attempts to connect.

What I am trying (and failing) to is provide the status of each connection as it loops through.

textArea.Refresh(True) does not update the textArea until all connections are complete.

I have tried the following
1 - Define a timer than runs every half second and does a refresh of textArea - Didn’t help
2 - Open a new dialog (floating) with text area and update the text area in that - no good
3 - Same as 2 but using a timer - still no good.

I looked at thread but didnt really understand it

Any pointers / help would be appreciated - Thank you in advance

You need to move the code doing the actual updates into a thread; it sounds like you are running these on the main thread with the UI, so all of the other things you are trying are blocked.
You could also use the timer to do the updates; on each call to the timer’s Run, execute one connection (code from the button where you are doing it now).
Perhaps share the code in the button doing the updates and we can suggest how to refactor into a timer.

Hi Matthew.

The code behind the button is:-

Var sfConnector As New SFConnector ( )

//Var dlg As new winLogger
//dlg.Show


// Establish Connections by looping through the UI Config
For Each uiConn As UIConnection In app.gblUIConnections
  //dlg.txtLogger.AddText("Attempt Connection to " + uiConn.SnowflakeAccount +EndOfLine)
  //dlg.txtLogger.Refresh
  
  txtAreaLogger.AddText("Attempt Connection to " + uiConn.SnowflakeAccount +EndOfLine)
  txtAreaLogger.Refresh(True)
    
  sfConnector.ODBCConnector = New ODBCDatabase
  System.EnvironmentVariable("ODBCINI") = uiConn.LocationOfODBCFile
  sfConnector.ODBCConnector.DataSource = uiConn.DSN
  sfConnector.ODBCConnector.UserName = uiConn.SnowflakeUsername
  sfConnector.ODBCConnector.Password = app.strCryption.decString(uiConn.SnowflakePassword)
  Try
    sfConnector.ODBCConnector.Connect()
    txtAreaLogger.AddText("Connection OK" + EndOfLine)
    //dlg.txtLogger.AddText("Connection OK" + EndOfLine)
    //dlg.txtLogger.Refresh
    sfConnector.ODBCConnector.Close()
  Catch dberr As DatabaseException
    //tbsfUsername.ReadOnly = False
    //dlg.txtLogger.AddText("Connection Failed " + dberr.Message + EndOfLine)
    //dlg.txtLogger.Refresh
    txtAreaLogger.AddText("Connection Failed " + dberr.Message + EndOfLine)
  End Try
  
  
Next

What I tried was to get the timer to refresh the txtAreaLogger control.
I think what you’re saying is to get the timer event to perform each connection and the button would simply enable the timer and disable when all connections have been attempted - did I understand correctly ?
Any help would be appreciated - Thanks

What you might try is:

  1. In the button, start the timer and reset the “loop counter” (see below)
  2. In the timer’s Action, you’ll execute one connection (the code in the For Each above). You’ll need to pull one item from app.gblUIConnections in each call to the timer, so (assuming this is an array), keep a variable with the loop counter. When you get to the end (nothing left to pull), set the timer’s run mode back to 0 so it stops firing

Alternatively, you put most of the above code into in a new thread subclass. Put it in the Run event, and then instantiate a new instance of the thread in the button and run it. However, you need to take the code that accesses the UI (TextLogger) out because you can’t access the UI from the thread. You either need to use the UserInterfaceUpdate event on the thread or still have a timer looking at some shared progress info. In your case, this is why using a timer might be a quicker solution.

The timer won’t fire while the code in the button is running.

Exactly.

I’ll give that a shot - appreciate you pointing me in the right direction - Thank you

Works a treat - Cheers