简体   繁体   English

按钮单击以停止测试

[英]Button Click to stop a test

I have a Test Engine that executes multiple tests, until all tests have been executed. 我有一个执行多个测试的测试引擎,直到所有测试都已执行。

Each test object has an Execute and Resume method. 每个测试对象都有一个ExecuteResume方法。 These methods return a status: 这些方法返回状态:

  • Waiting for reply from COM port 等待来自COM端口的回复
  • Waiting for button click from User 等待用户点击按钮
  • Testing completed. 测试完成。

In the GUI, the User starts the Test Engine by clicking a button. 在GUI中,用户通过单击按钮来启动测试引擎。 In other words, the click event for the button calls the Test Engine's start method. 换句话说,按钮的click事件将调用测试引擎的start方法。

Next, a test may send a message through the COM port and must suspend until a message is received from the COM port. 接下来,测试可以通过COM端口发送消息,并且必须暂停,直到从COM端口接收到消息为止。 I have the COM port interrupt process the message, then call the Test Engine's resume method. 我让COM端口中断处理该消息,然后调用测试引擎的resume方法。

A test may need information from the user. 测试可能需要来自用户的信息。 In this case, the test displays a message in a text box, then returns. 在这种情况下,测试将在文本框中显示一条消息,然后返回。 A click event handler of a button will call the Test Engine's resume method. 按钮的click事件处理程序将调用测试引擎的resume方法。

This works fine, except now I need to have a test repeatedly send message through the COM port and receive reply. 这工作正常,除非现在我需要进行测试以通过COM端口反复发送消息并接收回复。 This loop will only be terminated by a click from a button on the GUI. 仅通过单击GUI上的按钮才能终止此循环。

I have learned that the COM port interrupt handler runs in a different thread than the GUI. 我了解到COM端口中断处理程序在不同于GUI的线程中运行。 The tests execute GUI methods by using a delegate. 测试通过使用委托执行GUI方法。

How can I have the Test Engine check for button click while also waiting for a COM port message? 如何在等待COM端口消息的同时让测试引擎检查按钮是否单击? The Test Engine should check for button click after receiving a COM port message in order to keep the message system synchronized. 为了保持消息系统同步,测试引擎应收到COM端口消息检查按钮是否单击。

Notes: 笔记:
I was thinking about having the Test Engine run in a worker thread, with semaphores for message received from COM port and another semaphore for the stop button. 我正在考虑让测试引擎在工作线程中运行,其中的信号灯用于从COM端口接收消息,而另一个信号灯用于停止按钮。 The Test Engine would pend on the message semaphore, wake up, process the message, then check the stop button semaphore. 测试引擎将在消息信号挂起 ,唤醒,处理信息,然后检查停止按钮信号灯。

Using C# with Visual 2010 Express on Windows 7. 在Windows 7上将C#与Visual 2010 Express一起使用

I think the BackgroundWorker component might be able to help you with this. 我认为BackgroundWorker组件可能会帮助您。 It's designed to take some code and run it in a background thread, with an easy method for cancellation (the button calls CancelAsync(), and your test engine would check the CancellationPending property to see if it needs to cancel). 它旨在采用一些代码并将其在后台线程中运行,并具有一种简单的取消方法(按钮调用CancelAsync(),测试引擎会检查CancellationPending属性以查看是否需要取消)。

What you'd do is add one to your form. 您要做的就是在表单中添加一个。 Then you wrap up your COM port testing code in a method, which you hook up to the worker's DoWork event handler. 然后,将您的COM端口测试代码包装到一个方法中,并连接到工作程序的DoWork事件处理程序。

When you want to start working, call runWorkerAsync(). 当您要开始工作时,请调用runWorkerAsync()。 That worker code after testing the COM port should check the CancellationPending property, and return if it's true. 测试COM端口后,该工作程序代码应检查CancellationPending属性,如果为true,则返回。 As I mentioned, your button event handler for cancellation calls CancelAsync() to set that property. 如前所述,用于取消的按钮事件处理程序调用CancelAsync()来设置该属性。

BackgroundWorker also supports an event for showing progress, but you don't have to hook that up if you don't want to use. BackgroundWorker还支持显示进度的事件,但是如果您不想使用它,则不必将其挂起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM