关于使用冗长的操作停止线程 [英] About stoping threads with lengthy operations

查看:79
本文介绍了关于使用冗长的操作停止线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

我有一个程序,该程序同时使用一些线程来检索页面,等待响应,解析响应,然后使用Sqlite将数据保存在DB中.

现在,当我想关闭程序并运行这些线程时,它需要一些时间才能关闭.

Hello guys!

I have a program that uses some threads at the same time that retrieve a page, wait for response, parse the response and then save the data in a DB using Sqlite.

Now when i want to close my program and there are these threads running it takes some time so it closes. how can i abort the page retrieval or the DB update/insert when i need to close my program, quickly?

推荐答案

使用异步阅读器执行方法(BeginExecuteReader/EndExecuteReader而不是ExecuteReader.)
Use the asynchronous reader execute methods (BeginExecuteReader/EndExecuteReader instead of ExecuteReader, for instance).


我会说隐藏表单,直到操作完成,然后将其关闭.如果等待最多只有几秒钟,然后打算使它显示为响应性,这将是足够的...

BackgroundWorker也可以使用和取消...假设您可以经常检查取消,则后台任务将很快被取消.

如果您有一个较长的操作,则可以考虑将该操作拆分为多个操作...
I would say hide the form until the operation is completed and then close it. This would be adequate if the wait is at most a few seconds and then intend is to make it appears responsive...

BackgroundWorker can also be used and cancelled... Assuming that you can check for cancellation often enought, then the background task would be cancelled soon.

If you have a single long operation, then you might consider splitting that operation into multiple ones...


这篇关于关于使用冗长的操作停止线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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