按钮 单击可停止测试
我有一个执行多个测试的测试引擎,直到所有测试都已执行。
每个测试对象都有一个Execute
和Resume
方法。这些方法返回一个状态:
- 等待 COM 端口的回复
- 等待用户
- 测试完成的按钮单击。
在 GUI 中,用户通过单击按钮启动测试引擎。换句话说,按钮的单击事件调用测试引擎的启动方法。
接下来,测试可以通过 COM 端口发送消息,并且必须暂停,直到从 COM 端口接收到消息。我让 COM 端口中断处理消息,然后调用测试引擎的恢复方法。
测试可能需要来自用户的信息。在这种情况下,测试会在文本框中显示一条消息,然后返回。按钮的单击事件处理程序将调用测试引擎的resume 方法。
这工作正常,但现在我需要测试通过 COM 端口重复发送消息并接收回复。该循环只能通过单击 GUI 上的按钮来终止。
我了解到 COM 端口中断处理程序与 GUI 运行在不同的线程中。测试通过使用委托来执行 GUI 方法。
如何让测试引擎在等待 COM 端口消息的同时检查按钮单击情况?测试引擎应在收到 COM 端口消息后检查按钮是否单击,以保持消息系统同步。
注释:
我正在考虑让测试引擎在工作线程中运行,其中信号量用于从 COM 端口接收的消息,另一个信号量用于停止按钮。测试引擎将挂起消息信号量,唤醒,处理消息,然后检查停止按钮信号量。
在 Windows 7 上将 C# 与 Visual 2010 Express 结合使用。
I have a Test Engine that executes multiple tests, until all tests have been executed.
Each test object has an Execute
and Resume
method. These methods return a status:
- Waiting for reply from COM port
- Waiting for button click from User
- Testing completed.
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.
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.
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.
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.
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.
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.
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.
Using C# with Visual 2010 Express on Windows 7.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为 BackgroundWorker 组件可能可以帮助您有了这个。它的设计目的是获取一些代码并在后台线程中运行它,并使用一种简单的取消方法(按钮调用 CancelAsync(),并且您的测试引擎将检查 CancellationPending 属性以查看是否需要取消)。
您要做的就是在您的表单中添加一个。然后,将 COM 端口测试代码包装在一个方法中,并将其连接到工作线程的 DoWork 事件处理程序。
当您想要开始工作时,请调用 runWorkerAsync()。测试 COM 端口后的工作代码应检查 CancellationPending 属性,如果为真则返回。正如我所提到的,用于取消的按钮事件处理程序调用 CancelAsync() 来设置该属性。
BackgroundWorker 还支持显示进度的事件,但如果您不想使用,则不必将其连接起来。
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).
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.
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 also supports an event for showing progress, but you don't have to hook that up if you don't want to use.