views:

661

answers:

4

Hi , i have got CopyFile and Directory project.But when i started to copy Gui is freezing.I cant do anything file copying.So i found my solution at BackgroundWorker Component.But i got a problem with this component too.There are 3 radio button and command button.When i clicked command button its checking if radiobutton1 checked or else radiobutton 2 checked or else radiobutton 3 checked.If radiobuton1 checked do a lot things or radiobutton 2 chechked do another things etc.There are 3 backgroundworker for 3 radiobutton.When i chechked radiobutton 1 backgroundworker 1 dowork event working.When i checked radiobutton2 backgroundworker 2 dowork event working. etc.... My problem is when i chechked radiobutton1 and click commmand button.Starting backgroundworker1 do work event but its also continue to control if radiobutton2 checked or not..Not stopping so iam getting errors.My codes are ;

private void button1_Click(object sender, EventArgs e) {

        if (radioButton1.Checked)
        {
            backgroundWorker1.RunWorkerAsync();

        }

        if (radioButton2.Checked)
        {

            StreamReader sr = new StreamReader(Application.StartupPath + @"\hakimler.txt");

            while ((satir = sr.ReadLine()) != null)
            {

                try
                {

                    bool copy = CopyDirectory(DosyaYolu.kaynak, @"\\" + satir + @"" + DosyaYolu.hedef, true);
                    if (copy)
                    {
                        kopya += 1;
                    }


                    else
                    {

                        sw.WriteLine(satir);


                    }
                }


                catch (Exception)
                {



                }

            }
            sw.Close();
            MessageBox.Show("İşlem tamamlandı", "İşlem Sonu", MessageBoxButtons.OK, MessageBoxIcon.Information);
            lblkopya.Text = "Başarıyla tamamlanan iş sayısı : " + kopya.ToString();
            return;


        }



        if (chkPersonel.Checked == true)
        {
            StreamReader sr = new StreamReader(Application.StartupPath + @"\personel.txt");

            while ((satir = sr.ReadLine()) != null)
            {

                try
                {
                    bool copy = CopyDirectory(DosyaYolu.kaynak, @"\\ab04500-" + satir + @"" + DosyaYolu.hedef, true);
                    //bool copy = CopyDirectory(Application.StartupPath + @"\TELEFON REHBERİ", @"\\" + satir + @"\c$\Documents and Settings\All Users\Start Menu", true);
                    if (copy)
                    {
                        kopya += 1;
                    }

                    else
                    {

                        sw.WriteLine(satir);


                    }



                }


                catch (Exception)
                {



                }

            }

            sw.Close();

            MessageBox.Show("İşlem tamamlandı", "İşlem Sonu", MessageBoxButtons.OK, MessageBoxIcon.Information);
            lblkopya.Text = "Başarıyla tamamlanan iş sayısı : " + kopya.ToString();
            return;
        }

        else
        {
            if (txtBilgisayar.Text == "" && txtDongu.Text == "")
            {
                MessageBox.Show("Boşlukları dolduralım bi zahmet :@", "Bilgisayar Kodlarını girelim lütfen!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            bilgisayar = Convert.ToInt32(txtBilgisayar.Text);
            dongu = Convert.ToInt32(txtDongu.Text);

            for (int i = bilgisayar; i <= dongu; i++)
            {
                try
                {
                    bool copy = CopyDirectory(DosyaYolu.kaynak, @"\\ab04500-" + bilgisayar + @"" + DosyaYolu.hedef, true);
                    if (copy)
                    {
                        kopya += 1;
                    }

                    else
                    {

                        sw.WriteLine(satir);


                    }

                }
                catch (Exception)
                {



                }

                if (i == dongu)
                {
                    sw.Close();
                    MessageBox.Show("İşlem tamamlandı", "İşlem Sonu", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    lblkopya.Text = "Başarıyla tamamlanan iş sayısı : " + kopya.ToString();

                }

                bilgisayar += 1;
            }

        }


 private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        sw = new StreamWriter(DateTime.Today.ToShortDateString().ToString() + "_ulasmayanlar.txt", true);
        StreamReader sr = new StreamReader(Application.StartupPath + @"\savcilar.txt");

        while ((satir = sr.ReadLine()) != null)
        {

            try
            {
                bool copy = CopyDirectory(DosyaYolu.kaynak, @"\\" + satir + @"" + DosyaYolu.hedef, true);
                //bool copy = CopyDirectory(Application.StartupPath + @"\TELEFON REHBERİ", @"\\" + satir + @"\c$\Documents and Settings\All Users\Start Menu", true);
                if (copy)
                {
                    kopya += 1;
                }

                else
                {

                    sw.WriteLine(satir);


                }



            }


            catch (Exception)
            {



            }

        }



    }

private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { sw.Close();

        MessageBox.Show("İşlem tamamlandı", "İşlem Sonu", MessageBoxButtons.OK, MessageBoxIcon.Information);
        lblkopya.Text = "Başarıyla tamamlanan iş sayısı : " + kopya.ToString();
    }
A: 

i'm not quite sure what your problem exactly is, but in your code if your radioButton1 is Checked then the Background worker will do it's thing, else it won't. isn't that what you want?

i don't see any code for other radiobuttons you mention.

Mladen Prajdic
A: 

its control if radiobutton1 chechked or not if its checked true its start bgworkers do work event and its going to continue if radiobutton2 checked or not if radiobutton3 checked or not blabla its not stopping when its see radiobutton1 checked true.

Ibrahim AKGUN
Ibrahim, please don't post an answer in response to somebody else's answer. It would be better to leave a comment on the answer you are responding to.
Sailing Judo
+2  A: 

aha so you want to stop the background workers when you check antoher radio button? in your dowork() method you'll have to check if bg.CancellationPending == true and exit the method if it is. also you have to set WorkerSupportsCancellation to true after you initialize the BG worker

Mladen Prajdic
A: 

yes i want to stop controlling the another radiobutton's chechked true or not.if radiobutton1's chechked is true only do backgroundworkers dowork event and STOP.

Ibrahim AKGUN