PDA

View Full Version : سوال: خطای This BackgroundWorker is currently busy در استفاده از BackGroundWorker



MehdiElexal
پنج شنبه 07 فروردین 1393, 13:50 عصر
با سلام خدمت همه دوستان
من تایپیک های دیگه رو هم دیدم اما مشکلم حل نشد و این اررور رو دارم
This BackgroundWorker is currently busy and cannot run multiple tasks concurrently.
---
اینم کدم کسی میدونه مشکل از کجاست ؟
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Threading;
namespace BMS
{
public partial class frmSendMessage : Form
{
BackgroundWorker backgroundworker;
long HighestPercentage = 0;
int count;

DataSet ds = new DataSet();
OleDbConnection Con = new OleDbConnection();
DataRow newRow;
OleDbCommandBuilder cb = new OleDbCommandBuilder();

string[] UserMobil = new string[Form1.MaxVahedSystem];
int i;
string KeyPad, BufferKeyPad;
int OpacityShow = 0,MaxChar=320;
public static int MenuNumberService = 0;

public frmSendMessage()
{
InitializeComponent();
InitializeBackgroundworker();
}

private void frmSendMessage_Load(object sender, EventArgs e)
{
InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(System.Globalization.Cul tureInfo.CreateSpecificCulture("FA-fa"));
timer1.Enabled = true;
KeyPad = "";
Form1.KeyPadFormShared = "";
BufferKeyPad = "";
LoadDataBase();
}


public void LoadDataBase()
{
dataGridView1.DataBindings.Clear();
string strSql, strCon;
strCon = "Provider=Microsoft.jet.OLEDB.4.0;" + "Data Source=Database1.mdb";
OleDbConnection Con = new OleDbConnection(strCon);
Con.Open();


strSql = "Select * From Malekin";
OleDbDataAdapter da = new OleDbDataAdapter(strSql, Con);
DataSet ds = new DataSet();
da.Fill(ds, "Database1");
dataGridView1.DataBindings.Add(new Binding("datasource", ds, "Database1"));
Con.Close();




for (int i = 0; i < Form1.MaxVahedSystem; i++)
{
UserMobil[i] = (dataGridView1.Rows[i].Cells[2].Value.ToString());

listBox1.Items.Add(UserMobil[i].ToString());
}

}

void KeypadSerialRecived()
{
if (Form1.KeyPadFormShared != "")
{
KeyPad = Form1.KeyPadFormShared.ToString();
BufferKeyPad = BufferKeyPad + KeyPad;


if (KeyPad == "#" && txtNote.Text!="")//True
{
label2.Visible = true;
label4.Visible = true;
lblWarning.Visible = false;

//btnCancel.Enabled = true;
//progressBar1.Maximum = (int)Math.Pow(2, Convert.ToDouble(numericUpDown1.Value));
backgroundworker.RunWorkerAsync((int)Form1.MaxVahe dSystem);


}
else if (KeyPad == "#" && txtNote.Text == "")
{
KeyPad = "";
Form1.KeyPadFormShared = "";
BufferKeyPad = "";
lblWarning.Visible = true;
label2.Visible = false;
label4.Visible = false;
}

else if (KeyPad == "*")
{
Form1.ErrorForm = 0;
frmManager FormManager = new frmManager();
FormManager.Show();
KeyPad = "";
//HelperKeyBoard = "";
MenuNumberService = 0;
Close();


}

}

}

private void timer1_Tick(object sender, EventArgs e)
{
label4.Text = Form1.MaxVahedSystem.ToString() + "/" + count.ToString();
KeypadSerialRecived();
MaxChar = (70 - System.Convert.ToInt16(txtNote.Text.Length));
lblChar.Text = MaxChar.ToString();

if (MaxChar > 40)
{
lblChar.ForeColor = Color.LimeGreen;
label3.ForeColor = Color.LimeGreen;
}
else if (MaxChar <= 40 && MaxChar > 20)
{
lblChar.ForeColor = Color.Orange;
label3.ForeColor = Color.Orange;
}

else if (MaxChar < 20)
{
lblChar.ForeColor = Color.Red;
label3.ForeColor = Color.Red;
}

}

private void timer2_Tick(object sender, EventArgs e)
{
if (OpacityShow < 100)
{
this.Opacity += 0.4;
Application.DoEvents();
OpacityShow++;
}
}

private void txtNote_TextChanged(object sender, EventArgs e)
{
lblWarning.Visible = false;
}

private void timer2_Tick_1(object sender, EventArgs e)
{
timer2.Enabled = false;

}

//////
private void InitializeBackgroundworker()
{
backgroundworker = new BackgroundWorker();
backgroundworker.WorkerReportsProgress = true;
backgroundworker.WorkerSupportsCancellation = true;
backgroundworker.DoWork += new DoWorkEventHandler(backgroundworker_DoWork);
backgroundworker.ProgressChanged += new ProgressChangedEventHandler(backgroundworker_Progr essChanged);
backgroundworker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundworker_Ru nWorkerCompleted);
}

void backgroundworker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Error != null)
{
//lblResult.Text = e.Error.Message;
}
else if (e.Cancelled)
{
//lblResult.Text = "Cancelled";
}
else
{

}
//lblResult.Text = e.Result.ToString();
}

void backgroundworker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// progressBar1.Value = e.ProgressPercentage;
}

void backgroundworker_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker bg = sender as BackgroundWorker;
e.Result = ComputeFib((int)e.Argument, bg, e);


}

private long ComputeFib(int n, BackgroundWorker bg, DoWorkEventArgs e)
{
long Result = -1;

for (int i = 0; i < Form1.MaxVahedSystem; i++)
{

//SIM900Module.SIM900ModuleClass.SendMessagePDU(User Mobil[i], txtNote.Text, 2000);
count = i;
Thread.Sleep(2000);
Result = 1;

}


return Result;
}

}
}






از سریال یک سری عدد میاد که برنامه فرم های مختلف رو نشون میده
در این فرم میخوام به تعداد تعیین شده (فعلا 70 بار) اس ام اس بده
که هر مسیج حدود 6 ثانیه طول میکشه که اگر 70 تا شماره باشه 420 ثانیه به عبارتی 7 دقیقه طول میکشه که برنامه در حالت توقف میمونه
حالا از بک گراند ورکر میخوام استفاده کنم تا در این مدت برنامه دچار توقف نشه
با تشکر از شما دوستان عزیز :)