Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C#
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
C#
private void btn_ExportCancel_Click(object sender, EventArgs e)
        {
            
            LAHDALClass objIEDDAL = new LAHDALClass();
            
            //btn_Export.Visible=false;
            //btn_ExportCancel.Visible = false;
             btn_Export.Enabled = false;
            btn_ExportCancel.Enabled = false;
            
          

            try
            {
                
                stopProcess = true;
                if (stopProcess == true & thrThreadExportProcess.IsAlive)
                {
                    
                    this.thrThreadExportProcess.Abort();
                   
                    
                    thrThreadExportProcess.IsBackground = false;
                    //thrThreadExportProcess = null;
                    pb_RunningImage.Visible = false;
                   
                   lb_Progress.Text = "Canceling...";
                       Application.DoEvents();
                        thrThreadExportProcess.Join();
                        thrThreadExportProcess = null;
                        

                        objIEDDAL.CancelExport(TrasnformedDS, Projectname, usersSavingPath);//, ExportTempTableName);
                       
                        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.
Posted
Updated 28-Oct-12 20:48pm
v8
Comments
OriginalGriff 23-Oct-12 3:13am    
We would need to see your code to be sure - please show the Export button code and the cancel button as well.
Use the "Improve question" widget to edit your question and provide better information.
D-Kishore 23-Oct-12 4:28am    
Hi OriginalGriff, Here i added code,kindly check the code and let me know what might be the problem

1 solution

Ideally it should not behave like the same, Try to set visiblity = false of your button in your event handler starting and assign true again at the end of the event handler.

Thanks,
Ambesha
 
Share this answer
 

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