Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Running Async with 1ms delay

Posted on 2016-08-08
3
Medium Priority
?
88 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 2

Accepted Solution

by:
Mlungisi Ndlela earned 2000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

670 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