Click here to Skip to main content
12,550,447 members (45,438 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# C#4.0
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.IsBackground = true;

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)
Kindly provide your valuble Suggestions.
Posted 2-Oct-12 20:03pm
Updated 2-Oct-12 20:10pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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:[^].
Since you are in c# 4.0, you can use also a background worker: BackgroundWorker Threads and Supporting Cancel[^]
D-Kishore 3-Oct-12 2:45am
Hi Zoltan,Thanks for the reply,
here the thread is aboritg() properly,but at the joing time only its taking some time
Zoltán Zörgő 3-Oct-12 2:50am
I know. But calling abort will not solve several problems. Just because you passed the abort row in the code, does not mean that abort is performed, since it is an asyncronous call. Join is only passed when thread is finished aborting. And there you have the problem. Please read the article!
D-Kishore 3-Oct-12 6:46am
Thanks Zoltan, according to your suggestion i will rebuild the code.
Zoltán Zörgő 3-Oct-12 6:58am
Good luck.
D-Kishore 5-Oct-12 5:54am
mine 4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.161021.1 | Last Updated 3 Oct 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100