[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 335
  • Last Modified:

How to programmatically wait without freezing the application

I have created a program that is receiving events from an other class. Now i want to wait untill a specific Value is reactched. How do i go about doing this. Please Help!

Private connected As Boolean = false
Private FirstData as Boolean = false
Private modem As New Class_Comm

Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        AddHandler modem.event1, AddressOf Me.EventHandler

        do while connected = false
              'Just waiting to get connected
        loop

        do while FirstData = false
              'Just waiting to get first data
        loop
End Sub

Sub EventHandler(ByVal modem_regel As String)
        if modem_regel = "9600" then connected = true
        if modem_regel <> ""  and connected = true then FirstData = true
End Sub
0
hsjames
Asked:
hsjames
  • 5
  • 2
  • 2
1 Solution
 
AlexFMCommented:
You can add line
Application.DoEvents
to the beginning of Form_Load function. In this case form will be shown on the screen immediately.

But I think the better way is to remove waiting code at all and show form with disabled controls (except functions which can be used in disconnected state). When data is received, enable controls and give to user full functionality.
0
 
hsjamesAuthor Commented:
Hi AlexFM,

Yes, i have tried to add the Application.DoEvents butt this still puts the processor to it's limit. I also tried this:

        do while connected = false
              'Just waiting to get connected
              System.Threading.Thread.Sleep(1000)
        loop

At first it looked like it was working, but the application was not refreshing anymore and i lost control of the application.
0
 
AlexFMCommented:
You can add DoEvents also to the loop:

        do while connected = false
              Application.DoEvents()
              System.Threading.Thread.Sleep(1000)
        loop

But I recommend you the second way with disabling controls. Define what functions should not be available in disconnected state. Disable appropriate controls initially and enable them when connection is established.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I agree with AlexFM...disable all appropriate controls until you are connected.

You want to keep the Interval in Sleep() very small:

        While Not connected
            Application.DoEvents() ' Keep application responsive
            System.Threading.Thread.Sleep(100) ' Reduce CPU usage
        End While

This is because the application does not respond to user events and does not repaint itself while it is sleeping.
0
 
hsjamesAuthor Commented:
Hi AlexFM,

I have just tried this and the result is the same. It looks like the sleep(1000) also affects the EventHandler performance so this is not the way i would like to go. Isn't it possible to use events triggers in the EventHandler routine to do this?

Regards
0
 
hsjamesAuthor Commented:
I will make the sleep smaller (100)
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
As I said, keep the value in Sleep() very small.

You might want to go as low as 50.

Why can't you just do?    (going with AlexFMs suggestion)

    Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' Force the form to show itself in a disabled state
        Me.Show()
        Me.Enabled = False
        Application.DoEvents()

        AddHandler modem.event1, AddressOf Me.EventHandler
    End Sub

    Sub EventHandler(ByVal modem_regel As String)
        If modem_regel = "9600" Then
            connected = true
            Me.Enabled = True
        End If
        if modem_regel <> ""  and connected = true then FirstData = true
    End Sub
0
 
hsjamesAuthor Commented:
Ill try this!

One moment please.

0
 
hsjamesAuthor Commented:
Thanks for your help. I have kept the delay as low as 10 and reduced the cpu usage from 100% to 7%. Disabling the controls wasnt even nessesary.

regards,


HSJAMES
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 5
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now