按钮单击以停止测试 [英] Button Click to stop a test

查看:59
本文介绍了按钮单击以停止测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个执行多个测试的测试引擎,直到执行完所有测试为止.

I have a Test Engine that executes multiple tests, until all tests have been executed.

每个测试对象都有一个 Execute Resume 方法.这些方法返回状态:

Each test object has an Execute and Resume method. These methods return a status:

  • 等待来自COM端口的答复
  • 等待用户单击按钮
  • 测试完成.

在GUI中,用户通过单击按钮来启动测试引擎.换句话说,按钮的click事件将调用测试引擎的start方法.

In the GUI, the User starts the Test Engine by clicking a button. In other words, the click event for the button calls the Test Engine's start method.

接下来,测试可能会通过COM端口发送消息,并且必须暂停,直到从COM端口接收到消息为止.我让COM端口中断处理该消息,然后调用测试引擎的resume方法.

Next, a test may send a message through the COM port and must suspend until a message is received from the COM port. I have the COM port interrupt process the message, then call the Test Engine's resume method.

测试可能需要用户提供信息.在这种情况下,测试将在文本框中显示一条消息,然后返回.按钮的click事件处理程序将调用测试引擎的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.

这工作正常,除非现在我需要进行测试以通过COM端口反复发送消息并接收答复.只需在GUI上单击一个按钮,即可终止此循环.

This works fine, except now I need to have a test repeatedly send message through the COM port and receive reply. This loop will only be terminated by a click from a button on the GUI.

我了解到COM端口中断处理程序在不同于GUI的线程中运行.测试通过使用委托执行GUI方法.

I have learned that the COM port interrupt handler runs in a different thread than the GUI. The tests execute GUI methods by using a delegate.

如何在等待COM端口消息的同时让测试引擎检查按钮是否单击?测试引擎应在接收到COM端口消息后检查 后是否单击按钮,以保持消息系统同步.

How can I have the Test Engine check for button click while also waiting for a COM port message? 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. The Test Engine would pend on the message semaphore, wake up, process the message, then check the stop button semaphore.

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

Using C# with Visual 2010 Express on Windows 7.

推荐答案

我认为 BackgroundWorker 组件可以帮助您解决此问题.它旨在采用一些代码并在后台线程中运行它,并具有一种简单的取消方法(按钮调用CancelAsync(),测试引擎会检查CancellationPending属性以查看是否需要取消).

I think the BackgroundWorker component might be able to help you with this. 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).

您要做的是在表单中添加一个.然后,将您的COM端口测试代码包装到一个方法中,并连接到工作程序的DoWork事件处理程序.

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.

要开始工作时,请调用runWorkerAsync().测试COM端口后,该工作程序代码应检查CancellationPending属性,如果为true,则返回.如前所述,您要取消的按钮事件处理程序调用CancelAsync()来设置该属性.

When you want to start working, call runWorkerAsync(). That worker code after testing the COM port should check the CancellationPending property, and return if it's true. As I mentioned, your button event handler for cancellation calls CancelAsync() to set that property.

BackgroundWorker还支持显示进度的事件,但是如果您不想使用它,则不必将其挂接.

BackgroundWorker also supports an event for showing progress, but you don't have to hook that up if you don't want to use.

这篇关于按钮单击以停止测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆