Dear experts,
In my windows application, i am intiating one simple thread on the export button click.
here i am exporting sql table data to .csv file. its working fine.
When ever user hits the cancel button i am aborting the thread and deleting the exported csv file form the system.its working fine.
But the my problem is while joining thread, application gets haging some time.
My export button code
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));
}
}
}
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;
}
}
my Cancel button code
if (thrThreadExportProcess.IsAlive)
{
thrThreadExportProcess.IsBackground = false;
pb_RunningImage.Visible = false;
lb_Progress.Text = "Canceling..."
thrThreadExportProcess.Abort();
thrThreadExportProcess.Join();
File.Delete(ExportedFilePath);
}
Kindly give me valuble sugessions.
Please send me your ideas