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

x
?
Solved

Freeze and irresponsible when loop is running

Posted on 2011-09-14
7
Medium Priority
?
245 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 1000 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 1000 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
Industry Leaders: 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 1000 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 1000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

670 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