2 yifanzhou2012 yifanzhou2012 于 2016.04.18 17:57 提问

关于BackgroundWorker多线程同步问题

定义了一组 List bgws = new List();其中每一个bgws[i]的DoWork事件做的工作都是一样的,但是有两个参数随每个bgws[i]不同而不同,所以就如下分别写了很多DoWork0、DoWork1.。。。之类的来进行同步处理,有没有更简洁的办法,可以同时调用进行使用?
private void btnCopy_Click(object sender, EventArgs e)
{
for (int i = 0; i < srctable.Count; i++)
{
BackgroundWorker bgw = new BackgroundWorker();
bgws.Add(bgw);
bgws[i].WorkerReportsProgress = true;
bgws[i].WorkerSupportsCancellation = true;
//bgws[i].DoWork += new DoWorkEventHandler(DoWork);
//bgws[i].ProgressChanged += new ProgressChangedEventHandler(UpdateProgress);
//bgws[i].RunWorkerCompleted += new RunWorkerCompletedEventHandler(CompletedWork);
}
string[,] tName = new string[srctable.Count, 2];
//List tName = new List();
for (int i = 0; i < srctable.Count; i++)
{
tName[i, 0] = srctable[i];
tName[i, 1] = destable[i];

        }

        bgws[0].DoWork += new DoWorkEventHandler(DoWork0);
        bgws[0].ProgressChanged += new ProgressChangedEventHandler(UpdateProgress0);
        bgws[0].RunWorkerCompleted += new RunWorkerCompletedEventHandler(CompletedWork0);

        bgws[1].DoWork += new DoWorkEventHandler(DoWork1);
        bgws[1].ProgressChanged += new ProgressChangedEventHandler(UpdateProgress1);
        bgws[1].RunWorkerCompleted += new RunWorkerCompletedEventHandler(CompletedWork1);

        bgws[2].DoWork += new DoWorkEventHandler(DoWork2);
        bgws[2].ProgressChanged += new ProgressChangedEventHandler(UpdateProgress2);
        bgws[2].RunWorkerCompleted += new RunWorkerCompletedEventHandler(CompletedWork2);

        bgws[3].DoWork += new DoWorkEventHandler(DoWork3);
        bgws[3].ProgressChanged += new ProgressChangedEventHandler(UpdateProgress3);
        bgws[3].RunWorkerCompleted += new RunWorkerCompletedEventHandler(CompletedWork3);


        bgws[0].RunWorkerAsync();
        bgws[1].RunWorkerAsync();
        bgws[2].RunWorkerAsync();
        bgws[3].RunWorkerAsync();         
    }

1个回答

CSDNXIAOD
CSDNXIAOD   2016.04.18 18:03

多线程同步问题
多线程同步问题
关于多线程同步问题
----------------------biu~biu~biu~~~在下问答机器人小D,这是我依靠自己的聪明才智给出的答案,如果不正确,你来咬我啊!

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!