Dear experts,
for example in my c# windows application, in export button i am starting one thread,
here i am exporting sql data to csv file. Its working fine.
When user hits the cancel button i disable the export button and stoping the thread, after deleting the file i am enabling the Export button.
while thread stops takes some time, at this moment of time when i click already disabled export button-its firing after the thread stops.
Here is my code
Export Button
private void btn_Export_Click(object sender, EventArgs e)
{
ErrorMessage1.Visible = false;
ErrorMessage2.Visible = false;
LAHDALClass objLAHDALClass = new LAHDALClass();
Savingpath();
if (cmb_ProjectName.Text== "Create New Project")
{
MessageBox.Show("Please select Project Name from the Project Name dropdown");
return;
}
if (!string.IsNullOrEmpty(cmb_TrasnformedDS.Text))
{
Valid = true;
}
else
{
MessageBox.Show("Please select a transformed dataset from the Transformed Dataset Name dropdown.");
cmb_TrasnformedDS.Focus();
return;
}
if (!string.IsNullOrEmpty(cmb_ProjectName.Text))
{
Valid = true;
}
else
{
MessageBox.Show("Please select Project Name from the Project Name dropdown");
cmb_ProjectName.Focus();
return;
}
if (Valid)
{
btn_Export.Enabled = false;
cmb_TrasnformedDS.Enabled = false;
cmb_ProjectName.Enabled = false;
DisableImport_TabPage();
DisableTransform_TabPage();
DisableInputfiletypeRB();
lb_Progress.Location = new System.Drawing.Point(55, 18);
lb_Progress.Text = "Exporting Started...";
panel1.Visible = true;
pb_RunningImage.Visible = true;
Projectname = cmb_ProjectName.Text;
TrasnformedDS = cmb_TrasnformedDS.Text;
ExportedFilePath = ConfigurationManager.AppSettings["ExportedFiles"].ToString();
ExportedFilePath = ExportedFilePath + Projectname +"\\"+ TrasnformedDS + ".csv";
if (rb_FlatFile.Checked & rb_WH.Checked)
{
rb_HW.Enabled = false;
}
if (rb_FlatFile.Checked & rb_HW.Checked)
{
rb_WH.Enabled = false;
}
if (File.Exists(ExportedFilePath))
{
panel1.Visible = false;
btn_TrnsCancel.Enabled = false;
if (MessageBox.Show("The transformed table already exists in the selected project folder.\nPlease create a new project folder to be able to export the data again.", "Message Alert", MessageBoxButtons.OK, MessageBoxIcon.Warning) == DialogResult.OK)
{
FunctionEnable("EnableUIcontrols");
}
}
else
{
CsvfileWriter = new StreamWriter(ExportedFilePath);
thrThreadExportProcess = new Thread(new ThreadStart(startExport));
thrThreadExportProcess.Start();
thrThreadExportProcess.IsBackground = true;
}
}
}
private void startExport()
{
Savingpath();
LAHDALClass objIEDDAL = new LAHDALClass();
try
{
EnableCancelButton("EnableUIcontrols");
Thread.Sleep(0);
Stopwatch swra = new Stopwatch();
swra.Start();
objIEDDAL.ExportData(TrasnformedDS, Projectname, CsvfileWriter);
swra.Stop();
Console.WriteLine(swra.ElapsedMilliseconds);
}
catch (Exception ex)
{
throw ex;
}
}
cancel button code
private void btn_ExportCancel_Click(object sender, EventArgs e)
{
LAHDALClass objIEDDAL = new LAHDALClass();
btn_Export.Enabled = false;
btn_ExportCancel.Enabled = false;
try
{
stopProcess = true;
if (stopProcess == true & thrThreadExportProcess.IsAlive)
{
this.thrThreadExportProcess.Abort();
thrThreadExportProcess.IsBackground = false;
pb_RunningImage.Visible = false;
lb_Progress.Text = "Canceling...";
Application.DoEvents();
thrThreadExportProcess.Join();
thrThreadExportProcess = null;
objIEDDAL.CancelExport(TrasnformedDS, Projectname, usersSavingPath);
rb_HW.Enabled = true;
rb_WH.Enabled = true;
panel1.Visible = false;
}
EnableImport_TabPage();
EnableTransform_TabPage();
EnableInputfiletypeRB();
cmb_TrasnformedDS.Enabled = true;
cmb_ProjectName.Enabled = true;
cmb_ProjectName.Text = "";
btn_Export.Enabled = true;
}
catch (Exception ex)
{
throw ex;
}
}
Plese give me your ideas.