Solved

Freeze and irresponsible when loop is running

Posted on 2011-09-14
7
219 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
  • 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

813 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now