Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
4.00/5 (1 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

1 solution

Hi,

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.
Thanks.
 
Share this answer
 
Comments
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.Start();
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+

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