0

I'm writting a VB.Net app that interacts with a CAN bus in order to control some motors that are on it. With the CAN bus I can turn on and off each motor, in normal and reverse mode, and read their current consumption. I have implemented two checkbox for each motor, to make it run normal or reverse, and a textbox where I show the actual current consumption, this is actually working. Now what I want to do is to automatice some tests, for example:

  • Run each motor for two seconds in each direction and record it's consumption to see if the values are correct.

To do this I've created a TestMotor class where I send a ref of the two buttons that turns on in normal or reverse each motor. Each test class represents the test of each motor, so if I have 4 motors, I'll create 4 TestMotor objects giving them the two checkboxes that power on that motor.

Finally that TestMotor class have a executeTest which mainly does runs the motor during two seconds in each direction by checking and unchecking the checkboxes. This two secconds delay it's being done with a delay between them.

My problem is that if I run those tests directly on the main thread the Thread.sleep blocks UI thread, but if I create a thread and then execute those tests there those are executing all at the same time.

How would you solve this? I've been trying with async tasks, timers, and now I was going to try some semaphores or something, but a little help about the best way to resolve this would be great.

Some code:

In main class

for each test in test
    test.executeTest()
end for

TestMotor class

public sub executeTest()
    ckNormal.checked = true
    Thread.sleep(2000)
    ckNormal.checked = false
    ckReverse.checked = true
    Thread.sleep(2000)
    ckReverse.checked = false
end sub

Thanks!

rul3s
  • 332
  • 1
  • 2
  • 18

2 Answers2

0

Don't use Thread.sleep() use Task.Delay()

The response from @StephenCleary THIS THREAD.

Use Thread.Sleep when you want to block the current thread.

Use Task.Delay when you want a logical delay without blocking the current thread.

bmvr
  • 817
  • 2
  • 9
  • 25
  • If I use Task.Delay in the main thread, it simply closes the program, not working, I think it's a good idea but to be with a new thread, and then here I have the problem of seeing how to make the main threat to wait executing new tests until the actual haven't finished. – rul3s Jun 22 '17 at 11:17
  • @rul3s : Did you await the delay? `Await Task.Delay(2000)` – Visual Vincent Jun 22 '17 at 11:45
  • yes rul32 do it as @VisualVincent said, threat it as an async task – bmvr Jun 22 '17 at 12:12
0

Yes, I've tried too, but if I do the Task.Delay with async tasc the problem is that normal and reverse mode are tried at the same time. Finally I've made it doing this:

Private Sub autocheck_btStart_Click(sender As Object, e As EventArgs) Handles autocheck_btStart.Click
    If Not currentlyTesting Then
        currentlyTesting = True
        principal.setRecording(True, ecu)
        Dim t As Task = Task.Run(Sub()
                                     startTesting()
                                 End Sub)
        principal.setRecording(False, ecu)
        currentlyTesting = False
    End If
End Sub

Public Sub startTesting()
    For Each test In tests
        tbDebug.AppendText("Testing motor " + test.getName() + Environment.NewLine)
        test.executeTest()
    Next
    showResults()
End Sub

Public Sub executeTest()
    isTesting = True
    ckNormal.Checked = True
    Thread.Sleep(2000)
    ckNormal.Checked = False
    Thread.Sleep(200)
    ckReverse.Checked = True
    Thread.Sleep(2000)
    ckReverse.Checked = False
    Thread.Sleep(200)
    isTesting = False
End Sub

Thanks for your advices!!

rul3s
  • 332
  • 1
  • 2
  • 18