我有一个CopyFile和目录项目。但是当我开始复制Gui时,它会冻结。文件复制我无能为力。所以我在BackgroundWorker Component找到了我的解决方案。但我也有这个组件的问题。
有3个单选按钮和命令按钮。当我单击命令按钮时,它会检查是否已选中radiobutton1
,或者是否选中了radiobutton2
,或者是否选中了radiobutton3
。如果检查radiobutton1
,做了很多事情,或者如果radiobutton2
检查了,那么做另外的事情等等。有3个背光工作者用于3个无线电按钮。当我检查radiobutton1
时,backgroundworker1
dowork事件正在运行。当我检查radiobutton2
时,backgroundworker2
dowork事件正在运行。等...
我的问题是当我选中radiobutton1
并单击“命令”按钮时。从backgroundworker1
开始做工作事件,但它也会继续控制是否选中radiobutton2
。它没有停止,所以我得到错误。我的代码如下:
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();
}
如果radiobutton1的检查是否正确,如果检查是真的,它的开始bgworkers是否正常工作,如果radiobutton2检查是否继续,如果radiobutton3检查或者不检查,当它看到radiobutton1检查为真时,它不会停止。
是的,我想停止控制另一个radiobutton的chechked true或not。如果 radiobutton1的chechked是真的只做背景工作者的事件并停止。
答案 0 :(得分:2)
我不太确定您的问题究竟是什么,但在您的代码中radioButton1
是
然后检查后台工作人员会做的事情,否则它不会。
这不是你想要的吗?
我没有看到你提到的其他单选按钮的代码。
啊,所以你想在检查antoher单选按钮时停止后台工作人员?
在dowork()
方法中,您必须检查是否bg.CancellationPending == true
并退出方法。
初始化BG工作人员后,您还必须将WorkerSupportsCancellation
设置为true。