Hy all,

I'm developing a windowsforms app and I'm trying to use threads.

     public void Thread1(DataTable dt2,DataTable dt,int x,int y) 
        {   
            string cui = "";
            string cod_asis = "";
            for (int i = x; i < y; i++)//dt.Rows.Count
            {
                var currentRow = dt.Rows[i];
                cui = currentRow["cui"].ToString().Trim();
                cod_asis = currentRow["cod_asis"].ToString().Trim();
                cui = Main_App.Clase.VerificareCIF.FormatezCIF(cui);

                Verificare_Online_single(cui, cod_asis, dt2.Rows[i], dt.Rows[i],script1); //this method is takes some time and with this I write the datatable
                //Thread.Sleep(1);
            }

        }


   private void Parcurgere_Verificare_Online()
        {
            DataTable dt = (DataTable)gridControl.DataSource;
            DataTable dt2 = CreateTable(dt.Rows.Count);//tbl;

            foreach (System.Data.DataColumn col in dt.Columns) col.ReadOnly = false;
            foreach (System.Data.DataColumn col in dt2.Columns) col.ReadOnly = false;

            DevExpress.Data.CurrencyDataController.DisableThreadingProblemsDetection = true;


           ThreadStart job = new ThreadStart(() => Thread1(dt2, dt, 0, dt.Rows.Count/2));
           Thread thread = new Thread(job);
          // thread.Priority = ThreadPriority.Highest;
           //thread.IsBackground = true;
           thread.Start();

           ThreadStart job2 = new ThreadStart(() => Thread1(dt2, dt, dt.Rows.Count /2, dt.Rows.Count));
           Thread thread2 = new Thread(job2);
           //thread2.IsBackground = true;
           thread2.Start();

            thread.Join();
            thread2.Join();

            gridControl.DataSource = null;
            gridControl.DataSource = dt2;

 }

Is there a better way to do it because I don't know another way and I am new at using threads?

Are you using Thread directly because you have to (e.g., it's an assignment or something), or is this just exploration? If you have the freedom to explore alternatives, you might be interested in BackgroundWorker.

You should never really join two threads like that, the possibility of deadlock in those kinds of scenarios tends to become huge very quickly.

Events would be a better way to go.

I need to speed up the app..this seems to help.I mean without what I did here..it took double the time(only using a for loop).
how would the background worker be more helpful?

To extend on BackgroundWorker that gusano79 suggested you can update each row of the DataTable in the DoWork method (foreach row do a separate thread with Verificare_Online_single) and on RunWorkerCompleted do the update do the database using the DataTable.

This is what I do now:

   public void Thread1(DataTable dt2,DataTable dt,int x,int y) 
        {   
            string cui = "";
            string cod_asis = "";
            for (int i = x; i < y; i++)//dt.Rows.Count
            {
                var currentRow = dt.Rows[i];
                cui = currentRow["cui"].ToString().Trim();
                cod_asis = currentRow["cod_asis"].ToString().Trim();
                cui = Main_App.Clase.VerificareCIF.FormatezCIF(cui);
              // start a new thread for every entry

                ThreadStart job= new ThreadStart(() => Verificare_Online_single(cui, cod_asis, dt2.Rows[i], dt.Rows[i], "cif4.php", i));
                Thread thread = new Thread(job);
                thread.Start();

              // Verificare_Online_single(cui, cod_asis, dt2.Rows[i], dt.Rows[i], "cif4.php", i) ;//this writes in the datatable

            }
            //stop1 = true;

        }

         private DataTable Parcurgere_Verificare_Online()
        {


            DataTable dt = (DataTable)gridControl.DataSource;
            DataTable dt2 = CreateTable(dt.Rows.Count);//tbl;

            foreach (System.Data.DataColumn col in dt.Columns) col.ReadOnly = false;
            foreach (System.Data.DataColumn col in dt2.Columns) col.ReadOnly = false;

            Thread1(dt2, dt, 0, dt.Rows.Count);

            return dt2;

        }

            private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            splashScreenManager2.CloseWaitForm();
            MessageBox.Show("Date validate si adaugate");
            gridControl.DataSource = e.Result;
            DataTable dt = (DataTable)e.Result;
            bulkIN(dt);// procedure for bulk update
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            e.Result = Parcurgere_Verificare_Online();
        }

        //this is where it starts

        private void verificaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            gridControl.ForceInitialize();

            if (gridView1.RowCount > 0)
            {
                //IauDateAnaf_local();

                splashScreenManager2.ShowWaitForm();
                splashScreenManager2.SetWaitFormDescription("Verific Date");

                backgroundWorker1.RunWorkerAsync();
                backgroundWorker1.RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted;

            }
            else
            {
                MessageBox.Show("Nu ati importat date pentru verificare!", "Eroare", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

I'm getting: Exception of type 'System.OutOfMemoryException' was thrown...but am I on the right paht?

This article has been dead for over six months. Start a new discussion instead.