0

I have a program written in VB 2010 express under windows xp which does some heavy number crunching combined with serial communication. When it "gets too busy" it does not update the screen anymore (done simply with Textbox.text = "any text") and it does not respond to user input in any text box. Not even to a click on any text box.

Is there a way I can get the program to do screen update and respond to user input while it is busy?

thanks

Tim Williams
  • 154,628
  • 8
  • 97
  • 125
user387184
  • 10,953
  • 12
  • 77
  • 147
  • are you using VB.NET? Not sure if this is possible in VBA, but the general approach is to use additional thread for any CPU/memory/network heavy operations. So UI runs in the main thread and all additional calculations are running in a separate thread. When you do all-in-one thread - this leads to delays for processing UI related code – oleksii Jan 02 '12 at 02:09

3 Answers3

1

Use a BackgroundWorker to perform intensive work (rather than using the UI thread):

If you need finer grain control then create and manage your own threads: Multithreaded Applications (but the BackgroundWorker would probably meet your needs).

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
  • actually I started to use the worker class to then realize there is a refresh() method for text fields. That's all I needed in my case. I fortunately found it in the context of learning about the BackgroundWorker – user387184 Jan 02 '12 at 10:33
1

You'll probably need to move the heavy calculation and communication to a separate thread. In other words, it sounds like you may need to do some multithreading.

From the description of the problem, it sounds like the GUI updates and the calculation are performed in a single thread, therefore, when the heavy calculations are taking up all the computing resources, there's no computing resources to update (or respond to) the GUI.

An approach to take in these types of situation is to start a new thread with computation intensive (or potentially blocking operation such as I/O) processing in a separate thread, allowing the GUI to stay alive on the main thread.

Microsoft seems to have an introduction to multithreaded applications in Visual Basic, using the facilities provided in the .NET framework, so that might be a good starting point.

coobird
  • 159,216
  • 35
  • 211
  • 226
0

This question is tagged VBA and I will respond accordingly. If the tag is wrong then this answer can be disregarded.

Is there a way I can get the program to do screen update

The DoEvents statement will force a screen update.

Sometimes DoEvents is not enough. An alternative is proposed here.

Add the following line at the top of your module (outside of your function):

Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Then add this line in place of, or in addition to, DoEvents:

Sleep 1   'This will pause execution of your program for 1 ms

You might try increasing the length of time you pause the program using sleep if 1 ms doesn't work.

and respond to user input while it is busy?

This is not possible in VBA.

Community
  • 1
  • 1
Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188