Solved

Freeze and irresponsible when loop is running

Posted on 2011-09-14
7
209 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video discusses moving either the default database or any database to a new volume.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 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

9 Experts available now in Live!

Get 1:1 Help Now