Improve company productivity with a Business Account.Sign Up


Screen getting blank while running

Posted on 2006-10-28
Medium Priority
Last Modified: 2010-04-23
i have a form with a button and a status bar on it
i have a simple coding like this on button click event
        Dim i As Integer
        For i = 1 To 100000
           TextBox1.Text = i
            i = i * 1
            StatusBar1.Text = " Hai my value is " & i
During this process of running the coding if i minimise the form and open someother
program and after that if i again open the form i get a blank screen. The form controls
reappears only after the complete process is done
Can anybody help me witrh this
Question by:pearlym
LVL 10

Expert Comment

ID: 17827794
yes you need threading. Also keep in mind that another thing that is slowing your process down is i=i*1 (kind of senseless).

This is only worth 25 points so i will stop here. I hope in the future you will give experts more
credibility with more points. But with 25 points it just seems like you dont appreciate my time.
LVL 13

Accepted Solution

newyuppie earned 75 total points
ID: 17828664
first the i = i*1 is pointless, you dont need to multply anything, the variable "i" increases automatically in your loop.

try adding Application.DoEvents() after you set the statusbar text
LVL 64

Expert Comment

by:Fernando Soto
ID: 17828911
Hi pearlym;

The reason why the User Interface is unresponsive after minimizing the window and then reopening it is due to the face that the program is running in a single thread of execution. So what is happening is that when you click on the button to run the button click event handler no other code is running in you application and no other events are being handled until the code in question is completed. You will note if you haven’t already is that the value in the text box does not change either until you come back from the button click event. To solve your problem you can do one of two things, 1. create a new thread for that code as gangwisch states in his post or 2. Insert an statement inside the For loop, Application.DoEvents( ) as newyuppie has stated.  In solution 1 the new thread runs concurrently with the User Interface thread and so the UI will respond. In solution 2 the DoEvents will go to the threads message loop to see if there are any events that need to be taken care of and then return back to continue working on the code that had called it. Solution 2 is much easier in this case.

As newyuppie stated, i = i * 1 does nothing except waste more time in the loop. Another thing about this line of code is that this is bad programming practice. You should never adjust the loop control variable it could cause you problems.

Have a great day;

LVL 86

Expert Comment

by:Mike Tomlinson
ID: 17829066
It's important to note that simply placing code in another thread does not magically "free up" your main UI thread so it remains responsive.

What you have there is a tight processing loop which as Fernando explained prevents your app from processing its normal messages because it is "stuck" in the loop.

So what happens if you place that code in another thread?  The main UI thread no longer gets "stuck" in the loop...BUT the GUI STILL has to update itself for EVERY iteration of the loop!  So we really haven't freed it up much at all.  We still expect it to do just as much work as before, but because of the new thread involved we actually have given it more work since it now has to use the Invoke() method with Delegates to marshal the calls across the threads.

So how can we fix that?  One way is to "throttle" the loop back so it isn't so "tight" anymore.  Since the loop is now in its own thread we can safely put it to sleep without causing the main UI thread to freeze up:

        For i As Integer = 1 To xxx
            ' ... code ...

            ' make this thread stop briefly
            ' thus giving other threads a
            ' chance to do their own thing
            ' without being constantly
            ' pestered with our info...

Another option is to not force the GUI to update for every iteration of the loop.  You could update the the GUI every 10 iterations for instance.

But when dealing with long operations ask yoursef:

    Does the user REALLY need to see every single "step" in the process?

Often times the info is flying by so fast that the user can't even read it.  It is just a waste of processor time.  Most users are happy as long as there is some indication that the process is still ongoing.  This can be accomplished with updates as far apart as every 1/2 second.

One final thought:  Sometimes it makes sense to reverse the update direction.  Instead of the thread telling the GUI when to update...the GUI can "query" the thread.  You can place a Timer on the form that periodically grabs a value from the thread (you can use a global variable here for simplicity) and displays it in a control.


Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Watch the video to learn how one can deal with PST file corruption issue with an outstanding Kernel for Outlook PST Repair Tool easily. Using this tool, non-technical users can swiftly perform the repair process to restore their essential data witho…
From store locators to asset tracking and route optimization, learn how leading companies are using Google Maps APIs throughout the customer journey to increase checkout conversions, boost user engagement, and optimize order fulfillment. Powered …

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question