简体   繁体   English

C#类设计 - 事件和长时间运行的方法

[英]C# Class Design - Events & long running methods

I have a class that has some long running methods, in which they do DB queries, retrieve a list of objects, iterate through them and do some IO work. 我有一个类有一些长时间运行的方法,在这些方法中,它们进行数据库查询,检索对象列表,迭代它们并执行一些IO工作。

How should this class design be approached? 该课程设计应该如何处理?

Should I have OnComplete events in my class if the task completes? 如果任务完成,我应该在班级中有OnComplete事件吗?

I do want to give the consumer the ability to cancel/abort, so simply reporting when the method completes is not good enough. 我确实希望让消费者能够取消/中止,因此只需在方法完成时报告就不够好了。

Initially I had a BackgroundWorker (and much of the logic) in my UI code, however I separated the logic so it's all in a separate library. 最初我在我的UI代码中有一个BackgroundWorker (以及大部分逻辑),但是我将逻辑分开,所以它们都在一个单独的库中。

I was considering adding the background worker in the class, and simply passing creating similar events as is raised by the background worker. 我正在考虑在类中添加后台工作程序,并简单地传递后台工作程序引发的类似事件。 Is this the best approach? 这是最好的方法吗?

There is nothing inherintly wrong with using a background worker in your class. 在您的班级中使用后台工作程序没有任何遗憾。 Probably what you are really looking for is the Event-based Asynchronous Pattern . 你真正想要的是基于事件的异步模式

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

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