In My WPF Windows applications,
when user click on the export button, i am exporting large data to the .csv file in background worker.
Its working fine, but here i am facing the problem with cancelling the export.
My problem is how to cancel the Background process when user hits the cancel button.
Here is the detailed code.
when its running cmd.Excutereader();here it will take long time to get the data, at this time how do I cancel the process.
private void btnExport_Click(object sender, RoutedEventArgs e)
{
mWorker = new System.ComponentModel.BackgroundWorker();
mWorker.DoWork += new System.ComponentModel.DoWorkEventHandler(worker_DoWork);
mWorker.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(worker_ProgressChanged);
mWorker.WorkerReportsProgress = true;
mWorker.WorkerSupportsCancellation = true;
mWorker.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
dlg.FileName = "Document";
dlg.DefaultExt = ".csv";
dlg.Filter = "CSV (Comma delimited) (*.csv)|*.csv";
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
tabSchemeMngr.IsEnabled = false;
btnExport.IsEnabled = false;
if (dlg.FileName != "")
{
filename = dlg.FileName;
StartWorker();
}
}
}
void StartWorker()
{
if (!mWorker.IsBusy)
{
mWorker.RunWorkerAsync();
}
_busy.Set();
}
void CancelWorker()
{
if (mWorker.IsBusy)
{
mWorker.CancelAsync();
_busy.Set();
}
}
void PauseWorker()
{
_busy.Reset();
}
private void worker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
System.IO.StreamWriter sw = new System.IO.StreamWriter(filename, false);
SqlConnection sqlConnection = new SqlConnection(ConnectionStr);
SqlCommand cmd = new SqlCommand("PROC_IED_ExporttoText", sqlConnection);
cmd.CommandTimeout = 0;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@p_DataBaseName", SelectedDatabase);
cmd.Parameters.AddWithValue("@p_DistributionScheme", SelectedSchemeName);
cmd.Parameters.AddWithValue("@p_Peril", SelectedPeril);
cmd.Parameters.AddWithValue("@p_Occupancy", SelectedOccupancy);
cmd.Parameters.AddWithValue("@p_Country", SelectedCountry);
cmd.Parameters.AddWithValue("@p_State", selectedState);
cmd.Parameters.AddWithValue("@p_County", SelectedCounty);
cmd.Parameters.AddWithValue("@p_Zip", SelectedZip);
cmd.Parameters.AddWithValue("@p_YearBuilt", SelectedYearBuilt);
cmd.Parameters.AddWithValue("@p_ConstructionClass", SelectedConstrunctionClass);
cmd.Parameters.AddWithValue("@p_BuildingHeight", SelectedBuildingHeight);
cmd.Parameters.AddWithValue("@p_Year", SelectedYear);
cmd.Parameters.AddWithValue("@p_Measures", SelectedMeasures);
cmd.Parameters.AddWithValue("@p_AggregateLevel", SelectedAggregate);
cmd.Parameters.AddWithValue("@p_TlLatitude", SelectedCoordinateTopX);
cmd.Parameters.AddWithValue("@p_TlLongitude", SelectedCoordinateTopY);
cmd.Parameters.AddWithValue("@p_BrLatitude", SelectedCoordinateBottomX);
cmd.Parameters.AddWithValue("@p_BrLongitude", SelectedCoordinateBottomY);
DataTable dsYear = new DataTable();
try
{
sqlConnection.Open();
if (filename != "")
{
SqlDataReader export = cmd.ExecuteReader();
_busy.WaitOne();
if (mWorker.CancellationPending)
{
e.Cancel = true;
return;
}
DataTable Tablecolumns = new DataTable();
for (int i = 0; i < export.FieldCount; i++)
{
Tablecolumns.Columns.Add(export.GetName(i));
}
sw.WriteLine(string.Join(",", Tablecolumns.Columns.Cast<DataColumn>().Select(csvfile => csvfile.ColumnName)));
while (export.Read())
{
strRow = "";
for (int i = 0; i < export.FieldCount; i++)
{
strRow += export.GetValue(i).ToString();
if (i < export.FieldCount - 1)
{
strRow += this.separator;
}
}
sw.WriteLine(strRow);
}
sw.Close();
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (sqlConnection != null)
sqlConnection.Dispose();
}
}
private void worker_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
{
}
private string separator
{
get
{
return ",";
}
}
private void worker_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
if (e.Cancelled)
{
Xceed.Wpf.Toolkit.MessageBox.Show("Has been cancelled");
}
}
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
PauseWorker();
if (MessageBox.Show("Are you sure you want to cancel the Export?", "Message Alert", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
{
CancelWorker();
}
else
{
StartWorker();
}
}
Kindly tell me if you have any ideas to do this.