0

I need to add a delay in my code for few seconds, for which I was using the following code:

Private Sub waitInMilliSeconds(ByVal interval As Integer)
        Dim stpwt As New Stopwatch
        stpwt.Start()
        Do While stpwt.ElapsedMilliseconds < interval
            ' To allows UI to remain responsive and process other threads
            Application.DoEvents()
        Loop
        stpwt.Stop()
End Sub

It was working fine until I included "Imports CANoe". After which I started getting error message, for line - Application.DoEvents(). Error message is :

'Application' is ambiguous, imported from the namespaces or types 'CANoe, System.Windows.Forms'.

Can someone please help me in sorting this problem? Or with an alternate way of adding delays?

P.S. :

  • I am a basic programmer & not an expert in VB.NET, so your comments are very valuable to me.
  • I am using Microsoft Visual Basic 2010 Express edition.
  • I am using CANoe to send some CAN signals on CAN bus.
  • I want to insert a delay which will allow execution of other threads in parallel (Ex: I have a handler which gets called whenever there is data incoming from the COM port. I don't want this to stop when I call a delay function)

Thanks.

SKO
  • 3
  • 2
  • 2
    Please **DON'T USE** `Application.DoEvents()`. To delay your code for a while, you may use `Thread.Sleep(*pass value in milliseconds*)`. Instead of using `Application.DoEvents()` to keep your UI responsive, you can use `MultiThreading` along with `Invoke` / `BeginInvoke` for controls to keep them responsive. Google it, there are many articles available about the same – boop_the_snoot Jul 24 '17 at 09:34
  • Even so many years later people still find `DoEvents()` solutions somewhere. They need to be removed! Using `Application.DoEvents()` is _**REALLY BAD PRACTICE**_, please read: [**Keeping your UI Responsive and the Dangers of Application.DoEvents**](https://blogs.msdn.microsoft.com/jfoscoding/2005/08/06/keeping-your-ui-responsive-and-the-dangers-of-application-doevents/). – Visual Vincent Jul 24 '17 at 10:36
  • Like already suggested you should use multithreading or the [**Task Parallel Library**](https://learn.microsoft.com/en-us/dotnet/standard/parallel-programming/task-parallel-library-tpl) instead. When you need to access UI controls from a background thread/task use `Control.Invoke()` or `Control.BeginInvoke()`. For more information see: [**Performing thread-safe calls using Control.Invoke()**](https://stackoverflow.com/documentation/vb.net/1913/threading/6235/performing-thread-safe-calls-using-control-invoke). – Visual Vincent Jul 24 '17 at 10:44
  • @VisualVincent : Thank you for your comments. I am checking about your suggestions. – SKO Jul 24 '17 at 11:23
  • @Nobody : Thank you for your comments. I am checking about your suggestions. – SKO Jul 24 '17 at 11:23

0 Answers0