Click here to Skip to main content
13,295,953 members (51,918 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hi Experts,

In my C# windows application.
for example i have two buttons(Export and Cancel).

If i hit export button it will perfrom the operation like exporting/writing some large data to .CSV file.
Its working fine.

While writing to the .CSV file, when i hit the cancel button writing file need to be stop and delete the file.

Please share with me your ideas.
Posted 20-Sep-12 20:43pm

1 solution

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

Solution 1


You can Create BackgroundWorker which can be used to work in background and you can handle events to perform your desired work.

Check MSDN Article : Background Worker[^]

Hope it will work for you.
D-Kishore 21-Sep-12 1:54am
Yes Amit,

Here i am using thread for export button.
When user hit the cancel button here i am aborting the thread. after aborting the thread i am deleting the file.while deleting the file i am getting the exception like 'The process cannot access the file 'D:\myCSVfile.csv' because it is being used by another process.'
Kindly give me an idea
@AmitGajjar 21-Sep-12 1:55am
are you using Background worker or simple Thread ?
D-Kishore 21-Sep-12 1:57am
using simple thread.
thrThreadExportProcess = new Thread(() => startExport(csvPath));
thrThreadExportProcess.IsBackground = true;
@AmitGajjar 21-Sep-12 2:02am
Ok, Use thrThreadExportProjecss.Join() to waint for thread to abort. after that you need to write logic to delete the file.
D-Kishore 21-Sep-12 2:08am
Thanks a lot,
It works fine
mine is 5+
@AmitGajjar 21-Sep-12 2:09am
Thanks D-Kishore

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 |
Web02 | 2.8.171207.1 | Last Updated 21 Sep 2012
Copyright © CodeProject, 1999-2017
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