简体   繁体   English

如何附加非UI线程的SynchronizationContext

[英]How to attach SynchronizationContext of Non-UI thread

I am working on unit testing and I have to pass context of one thread to other one. 我正在进行单元测试,必须将一个线程的上下文传递给另一个线程。 In real life the first one would be UI thread and all will be ok (I hope). 在现实生活中,第一个将是UI线程,并且一切都会好起来(我希望如此)。 But my unit test has not UI (it is some testing thread). 但是我的单元测试没有UI(这是一些测试线程)。 I have read: " One thread that always has a SynchronizationContext is the UI thread. " Is it possible to attach context of one thread (Non UI) to other one? 我读过:“ 一个始终具有SynchronizationContext的线程是UI线程。 ”是否可以将一个线程(非UI)的上下文附加到另一个线程? How to do that? 怎么做?

I think you might need to rethink your testing strategy. 我认为您可能需要重新考虑您的测试策略。

If you really want to test interaction between GUI and some background task Thread safety wise, you can create simple WinForm and try it out. 如果您真的想测试GUI和某些后台任务Thread安全性之间的交互,则可以创建简单的WinForm并尝试一下。

Or try to create test so that your GUI form is behaving as expected after you get some results from your back thread (ie displaying it correctly). 或者尝试创建测试,以便在从后线程获得一些结果(即正确显示它)后,GUI表单的行为符合预期。

Creating unit tests for GUIs are quite tricky. 为GUI创建单元测试非常棘手。 That's why people tend to remove all the business and any other logic outside GUI so that it can be easily testable. 这就是为什么人们倾向于删除GUI之外的所有业务和任何其他逻辑,以便可以轻松对其进行测试。

maybe you can consider using WPF with MVVM, or WinForms with MVP, which makes it easier to draw that line 也许您可以考虑将WPF与MVVM一起使用,或者将WinForms与MVP一起使用,这使得绘制该线条更加容易

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

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