Solved

Freeze and irresponsible when loop is running

Posted on 2011-09-14
7
225 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

821 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