Solved

Running Async with 1ms delay

Posted on 2016-08-08
3
41 Views
Last Modified: 2016-08-09
Hi,
I would like to know why the current wait for 1 millisecond is not working. to me it looks more like a 50 millisecond wait.
what can I do to this this glith?

Public Class Form1
    Dim iCount As Long = 0

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim aaa = DoSomethingAndWaitAsync()
    End Sub

    Private Sub trmLabel_Tick(sender As Object, e As EventArgs) Handles trmLabel.Tick
        Dim iDate As DateTime = Date.UtcNow
        Label2.Text = iDate.ToString

        If iDate.Second = 59 Then
            iCount = 0
        Else
            If iCount > 0 Then 
                Label1.Text = (iCount / iDate.Second).ToString & " | " & iCount.ToString
            Else
                Label1.Text = iCount.ToString
            End If
        End If
    End Sub

    Public Async Function DoSomethingAndWaitAsync() As Task
        Dim i = True

        While (i = True)
            Await Task.Run(Function() DoSomething())
            Await Task.Delay(1)
        End While

    End Function

    Function DoSomething() As Long
        iCount += 1
        Return iCount
    End Function
End Class

Open in new window

0
Comment
Question by:XK8ER
3 Comments
 
LVL 2

Expert Comment

by:JesNoFear
ID: 41748130
why are you pulling seconds?
If iDate.Second = 59 Then
            iCount = 0
        Else
            If iCount <> iDate.Second Then
                Label1.Text = (iCount / iDate.Second).ToString & " | " & iCount.ToString
            Else
                Label1.Text = iCount.ToString
            End If
        End If
0
 
LVL 1

Author Comment

by:XK8ER
ID: 41748134
JesNoFear, the issue is not with the timer so ignore that part of the code
0
 
LVL 1

Accepted Solution

by:
MCSI Developers earned 500 total points
ID: 41748325
Dim i = True

        While (i = True)
            Await Task.Run(Function() DoSomething())
            Await Task.Delay(1)
        End While

Open in new window


Well I've noticed that your I will never ever be false as you can see above. Also I don't think it will wait because you start by running it then call delay at last. try swooping your code and be like this
 Await Task.Delay(1)
Await Task.Run(Function() DoSomehing())

Open in new window

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

747 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