[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VB.NET Function improvement

Posted on 2009-02-19
9
Medium Priority
?
528 Views
Last Modified: 2013-11-26
I need to improve a simple function. The function uses a public(global) boolean variable that if it's 'true' it causes the application to go into a loop thus 'pausing' the app.
   The problem is that it is consuming too many resources while paused. Any help in  is appreciated.
Public Sub WasPauseButtonHit()
        '**************************************
        'CHECK IF PAUSE BUTTON HIT ROUTINE ****
        '**************************************
 
        'PAUSE BUTTON HIT! ************
        If Pause = True Then
            Do While Pause = True
                frmMain.ToolStripStatusLabel5.Text = "<<< MANUALLY PAUSED >>>"
                System.Windows.Forms.Application.DoEvents()
            Loop
        End If
        '******************************
    End Sub

Open in new window

0
Comment
Question by:tommycod
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 4

Accepted Solution

by:
siancell earned 2000 total points
ID: 23686640

Public Sub WasPauseButtonHit()
        '**************************************
        'CHECK IF PAUSE BUTTON HIT ROUTINE ****
        '**************************************
 
        'PAUSE BUTTON HIT! ************
        If Pause = True Then
            Do While Pause = True
                'make the app do this every second
                System.Threading.Thread.Sleep(1000)
                frmMain.ToolStripStatusLabel5.Text = "<<< MANUALLY PAUSED >>>"
                System.Windows.Forms.Application.DoEvents()
            Loop
        End If
        '******************************
    End Sub

Open in new window

0
 
LVL 48

Expert Comment

by:jpaulino
ID: 23686717
siancell,
Adding a System.Threading.Thread.Sleep(1000) will freeze even more the form.
 
tommycod,
You don't need to write to the ToolStripStatusLabel5 inside the loop, but that's not the most important. Why it freezes ? Can you show us how you use that ?
0
 

Author Comment

by:tommycod
ID: 23686778
using:
System.Threading.Thread.Sleep(1000)
seems to lock the application up after a matter of seconds...

any other ideas? Thanks.
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 

Author Comment

by:tommycod
ID: 23686824
Hi jpaulino,
  It doesnt actually feeze in my original routine.. it just uses the CPU to 100%.. even with a dual proc.
there must be a way to suspnd the app without using up system resources..

The app uses the function to check occationaly if the 'pause' button has been hit while it is running.

it's called like this:
Call WasPauseButtonHit() 'SEE IF PAUSE WAS DETECTED

And if the 'pause button' was pressed it sets the Public(global) below:
Pause = True

Let me know if you need anything else..


0
 
LVL 48

Expert Comment

by:jpaulino
ID: 23686912
Ok, I understand that, but what i'm trying to understand is why you're using that ? Why do you need that pause loop.
0
 

Author Comment

by:tommycod
ID: 23686936
jpaulino,
  The application connects to various webpages and parsed the data. The pause checking function is in a loop of the main sub that is going through the list of links one by one.. it checks if the pause button has been hit after visiting each link...
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 23687280
I agree with jpaulino...there is probably a ~better~ way to implement a pause.

But to answer your original question, just use a SMALLER Sleep() Interval to reduce CPU usage:

            frmMain.ToolStripStatusLabel5.Text = "<<< MANUALLY PAUSED >>>"
            Do While Pause = True
                System.Threading.Thread.Sleep(50) ' small interval (won't "freeze" the form) to reduce CPU usage
                Application.DoEvents() ' keep app "responsive"
            Loop

If you are happy with the perfomance of that...then give the points to siancell as he originally suggested Sleep(), albeit with too large an Interval.
0
 

Author Comment

by:tommycod
ID: 23687776
Hi,
  If there is a better way please let me know..  Its simply a function that checks for a pause in the middle of a loop.  I lowered the pause.. doesnt seems to free up the app. If there are any better solutions please let me know. Otherwise I'll award points to siancell. Thanks.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 23687935
It's hard to diagnose exactly where the problem is without seeing more of your code...
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

830 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