Click here to Skip to main content
15,892,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

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));

                }
            }
        }
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

C#
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
Posted
Updated 22-Oct-12 19:38pm
v6
Comments
Sergey Alexandrovich Kryukov 22-Oct-12 23:57pm    
You need to show some more code, when the thread is created. What's the idea behind using Join? Why next statement deletes some file?
--SA
D-Kishore 23-Oct-12 0:05am    
I updated question with my full code. kidnly check
D-Kishore 23-Oct-12 0:06am    
if we are not joining the thread while deleting we will get the error like "the file used by some other process, we cant access the file.
Sergey Alexandrovich Kryukov 23-Oct-12 1:39am    
Do you understand that Join itself is a blocking call? Why do you use it? And if you think you need it, why are you surprised that you have a delay?! This is a blocking call...

Perhaps you need to explain your goal first, if you need practical help. If you just need some explanation, that's basically it: this call waits for a thread to terminate.
--SA
D-Kishore 23-Oct-12 6:14am    
Don't be angry, my question is like while terminating the thread, why its hangs?
And with out using Join(); file can't be delete we will get the as mentioned above.

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