Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Screen getting blank while running

Posted on 2006-10-28
4
Medium Priority
?
224 Views
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
        Next
During this process of running the coding if i minimise the form and open someother
program and after that if i again open the vb.net form i get a blank screen. The form controls
reappears only after the complete process is done
Can anybody help me witrh this
0
Comment
Question by:pearlym
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 10

Expert Comment

by:gangwisch
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.
0
 
LVL 13

Accepted Solution

by:
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
0
 
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;

Fernando
0
 
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...
            System.Threading.Thread.Sleep(yyy)
        Next

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.

~IM
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

660 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