Solved

Running Async with 1ms delay

Posted on 2016-08-08
3
70 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 1

Accepted Solution

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
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…

751 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