C#Windows应用程序中的Thread.abort()和Thread.join() [英] Thread.abort() and Thread.join() in c# windows applications

查看:116
本文介绍了C#Windows应用程序中的Thread.abort()和Thread.join()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,专家,
在我的C#Windows应用程序中,

我有两个按钮可以导出和取消.

在我的导出按钮中,这里我将sql数据表导出到在线程下运行的.csv file.its.

Hi Experts,
In my c# windows application,

I have two buttons export and cancel.

In my export button, here i am exporting sql datatable to .csv file.its running under the thread.

thrThreadExportProcess = new Thread(new ThreadStart(startExport));
               thrThreadExportProcess.Start();
               thrThreadExportProcess.IsBackground = true;



在其导出过程运行时,用户在此处单击取消"按钮,我正在中止线程,然后加入线程并删除导出的文件.

在加入Thread的同时,应用程序get挂起了一段时间,然后它将运行.

我的问题是为什么它挂了一段时间.

这是取消"按钮中代码的一部分.



while its exporting process is running, user hits the cancel button here i am aborting the thread,then joining the thread and deleting the exported file.

while its joining Thread, application get hangs up some time then it will run.

my question is why its hangs up some time.

here is the part of the code in cancel button.

if (thrThreadExportProcess.IsAlive)
                {
                   thrThreadExportProcess.Abort();
                   thrThreadExportProcess.Join();
                   File.Delete(csvPath);
                }


请提供您的宝贵建议.


Kindly provide your valuble Suggestions.

推荐答案

您是否调试了线程?线程代码本身可能未正确处理中止信号.您应该使用此部分代码来更新问题.由于多种原因,简单地调用中止可能会有问题.您应该考虑改用标志.请阅读本文: http://www.interact-sw.co.uk/iangblog /2004/11/12/cancellation [ ^ ].
由于您使用的是c#4.0,因此您也可以使用后台工作者: BackgroundWorker线程和支持取消 [ ^ ]
Have you debugged the thread? It is possible that the thread code itself is not handling the abort signal properly. You should update the question with this part of the code. Simply calling abort can be problematic because of several reasons. You should consider using a flag instead. Please read this article: http://www.interact-sw.co.uk/iangblog/2004/11/12/cancellation[^].
Since you are in c# 4.0, you can use also a background worker: BackgroundWorker Threads and Supporting Cancel[^]


这篇关于C#Windows应用程序中的Thread.abort()和Thread.join()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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