Solved

Freeze and irresponsible when loop is running

Posted on 2011-09-14
7
239 Views
Last Modified: 2013-11-05
My app got freeze when running this loop, and resume when loop is over. Wonder is my loop wrong or sleep() cause the freeze? What should I change?

Thanks.


    Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
        Dim sendauto(1) As Byte
        sendauto(0) = &H55

        Dim timer1 As New System.Windows.Forms.Timer
        Dim idle0, idle1 As Integer
        timer1.Enabled = True
        idle1 = 100
        If ComboBox1.SelectedItem = "Beating heart video" Then
            idle0 = 2000
        End If

        For counter As Integer = 0 To 10

            Dim sendclear(1) As Byte
            sendclear(0) = &H45

            'timer1.Interval = idle

            Dim senddata(13) As Byte
            senddata(0) = sd0
            senddata(1) = sd1
            senddata(2) = sd2
            senddata(3) = sd3
            senddata(4) = sd4
            senddata(5) = sd5
            senddata(6) = sd6
            senddata(7) = sd7
            senddata(8) = sd8
            senddata(9) = sd9
            senddata(10) = sd10
            senddata(11) = sd11
            senddata(12) = sd12
            EzB_Connect1.EZB.Uart.SendSerial(EZ_B.Digital.DigitalPortEnum.D8, EZ_B.Uart.BAUD_RATE_ENUM.Baud_9600, byteArray:=sendauto)
            Threading.Thread.Sleep(100)
            'timer1.Interval = idle1
            EzB_Connect1.EZB.Uart.SendSerial(EZ_B.Digital.DigitalPortEnum.D8, EZ_B.Uart.BAUD_RATE_ENUM.Baud_9600, byteArray:=sendclear)
            Threading.Thread.Sleep(100)
            'timer1.Interval = idle1
            EzB_Connect1.EZB.Uart.SendSerial(EZ_B.Digital.DigitalPortEnum.D8, EZ_B.Uart.BAUD_RATE_ENUM.Baud_9600, byteArray:=senddata)
            'timer1.Interval = idle0
            Threading.Thread.Sleep(idle0)
        Next
    End Sub

Open in new window

0
Comment
Question by:zzl630
[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
  • 3
  • 2
  • 2
7 Comments
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 250 total points
ID: 36540731
Loops always freeze, because the framework waits for the method to terminate before going to other tasks.

The best way to prevent that is to add the following call inside the loop : Application.DoEvents. This stops the loop, look for and execute all pending events and then go back to the loop.

This should not be a problem for a 10 iterations loop, but in bigger loops, calling DoEvents on each loop can slow down things a lot. It might be better in such a case to call it every 10 or 50 or 100 loops. This is determined by trial an error most of the time.
0
 

Author Comment

by:zzl630
ID: 36545075
Thank you.

It seems DoEvents works for me, but still feel not very "smooth". Is it the way VB loops working?I don't recall Java has this problem. Should I add more DoEvents during the loop to make it feel more smooth? Is multithreading or BackgroundWorker has better performance?
0
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 250 total points
ID: 36546224
To make the loop smoother, do not add more add events, add less. As specified in my post, DoEvents can slow down a long loop. Instead of calling it each turn, put a counter in the loop and call it only from time to time.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 250 total points
ID: 36570544
You have thread.sleep calls which would obviously freeze the app as the main thread is being put to sleep. Either remove those calls or move this code to another thread.
0
 

Author Comment

by:zzl630
ID: 36578193
I used BackgroundWorker solve the problem. But now I am facing multi thread reference issue. How can I reference a variable in another thread?
If ComboBox1.SelectedItem = "Beating heart video" Then
            idle0 = 2000
        End If

Open in new window

ComboBox1.SelectItem is in another thread. How can I use it in BackgroundWorker thread?

Thanks,
ZZ
0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 250 total points
ID: 36582347
Before you call the backgroundworker, put the combobox1.selecteditem into a class variable and then access that variable in the backgroundworker.
0
 

Author Closing Comment

by:zzl630
ID: 36584836
works great! thank you.
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
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…
Suggested Courses

630 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