Click here to Skip to main content
Sign Up to vote bad
good
See more: 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.Start();
               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)
                {
                   thrThreadExportProcess.Abort();
                   thrThreadExportProcess.Join();
                   File.Delete(csvPath);
                }
                   
Kindly provide your valuble Suggestions.
Posted 2 Oct '12 - 20:03
D-Kishore3.2K
Edited 2 Oct '12 - 20:10


1 solution

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[^]
  Permalink  
Comments
D-Kishore - 3 Oct '12 - 2:45
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:50
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:46
Thanks Zoltan, according to your suggestion i will rebuild the code.
Zoltán Zörgő - 3 Oct '12 - 6:58
Good luck.
D-Kishore - 5 Oct '12 - 5:54
mine 4

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 369
1 OriginalGriff 190
2 Tadit Dash 168
3 CPallini 163
4 Aarti Meswania 153
0 Sergey Alexandrovich Kryukov 10,169
1 OriginalGriff 7,749
2 CPallini 4,181
3 Rohan Leuva 3,482
4 Maciej Los 3,089


Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 3 Oct 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid