Click here to Skip to main content
11,577,454 members (59,946 online)
Rate this: bad
good
Please Sign up or sign in to vote.
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:03pm
D-Kishore4.1K
Edited 2-Oct-12 20:10pm
v3

1 solution

Rate this: bad
good
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: 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 at 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ő at 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 at 3-Oct-12 6:46am
   
Thanks Zoltan, according to your suggestion i will rebuild the code.
Zoltán Zörgő at 3-Oct-12 6:58am
   
Good luck.
D-Kishore at 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
0 DamithSL 155
1 Afzaal Ahmad Zeeshan 125
2 OriginalGriff 115
3 Richard MacCutchan 100
4 Abhinav S 90
0 OriginalGriff 820
1 Sergey Alexandrovich Kryukov 676
2 Abhinav S 528
3 F-ES Sitecore 420
4 Suvendu Shekhar Giri 375


Advertise | Privacy | Mobile
Web03 | 2.8.150603.1 | Last Updated 3 Oct 2012
Copyright © CodeProject, 1999-2015
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