Click here to Skip to main content
16,011,542 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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.
C#
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.

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

Kindly provide your valuble Suggestions.
Posted
Updated 2-Oct-12 20:10pm
v3

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[^]
 
Share this answer
 
Comments
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900