c#几个后台工人同样的方法

时间:2016-07-12 06:10:56

标签: c# backgroundworker

我有问题,当2-8个背景工作者访问相同的方法时出现问题。 所以这是一个例子:

int threadnumber = 0;
int retries[] = new int[8];

private XmlDocument GetXML(string ApiUrl, int threadnumber)
        {
            var mySourceDoc = new XmlDocument();
            try
            {
                var httpRequest = (HttpWebRequest)WebRequest.Create(ApiUrl);
                httpRequest.Timeout = 50000;
                if (cb_Proxy.Checked == true)
                {
                    WebProxy germanserver = new WebProxy();
                    Uri newUri = new Uri("http://" + txt_Proxy.Text);
                    germanserver.Address = newUri;
                    httpRequest.Proxy = germanserver;
                }
                var response = (HttpWebResponse)httpRequest.GetResponse();
                var receiveStream = response.GetResponseStream();
                receiveStream.ReadTimeout = 150000;
                mySourceDoc.Load(receiveStream);
                receiveStream.Close();
                retries[threadnumber-1] = 1;
            }
            catch (Exception ex)
            {
                //DialogResult MessBox = MessageBox.Show("Der Server ist nicht erreichbar oder es ist ein anderes Problem mit dem Server aufgetreten. Es wird automatisch 3 mal erneut versucht. Fehlermeldung anzeigen?", "Fehler", MessageBoxButtons.YesNo, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                //if (MessBox == DialogResult.Yes) MessageBox.Show(ex.Message);
                retries[threadnumber - 1]++;
                consoletext = "Fehler: " + ex.Message + "; Neuer Versuch " + retries[threadnumber - 1] + " von Url " + ApiUrl + Environment.NewLine;
                if (threadnumber == 1) workingHelper1.ReportProgress(counterhelper[0]);
                if (threadnumber == 2) workingHelper2.ReportProgress(counterhelper[1]);
                if (threadnumber == 3) workingHelper3.ReportProgress(counterhelper[2]);
                if (threadnumber == 4) workingHelper4.ReportProgress(counterhelper[3]);
                if (threadnumber == 5) workingHelper5.ReportProgress(counterhelper[4]);
                if (threadnumber == 6) workingHelper6.ReportProgress(counterhelper[5]);
                if (threadnumber == 7) workingHelper7.ReportProgress(counterhelper[6]);
                if (threadnumber == 8) workingHelper8.ReportProgress(counterhelper[7]);
                Thread.Sleep(3000);
            }
            finally
            {
                if (retries[threadnumber - 1] > 1 && retries[threadnumber - 1] <= 3) GetXML(ApiUrl, threadnumber);
                if (retries[threadnumber - 1] >= 4) retries[threadnumber] = 1;
            }
            return mySourceDoc;
}

backgroundworkers使用生成的url字符串和线程编号1-8调用方法getxml。 我的问题是,有时候重试达到5或更高。所以我认为背景工作者有时会访问相同的变量并计算它?

2 个答案:

答案 0 :(得分:0)

允许多个线程同时操作同一个对象通常不是一个好主意。这肯定会导致被操纵对象状态的冲突。

考虑在访问和操作共享变量(即lockthreadnumber)的行上使用retries

还要考虑调用方法的方式,因为:

  1. 似乎每个工作线程需要retries数组中的2个条目(即threadnumberthreadnumber - 1)因此,您无法调用GetXML具有连续threadnumber值的方法,否则您将在retries数组中出现冲突。或者这可能是您的代码中导致此问题的错误(引用如下)。

      

    if(retries [threadnumber - 1]&gt; = 4)重试[threadnumber] = 1;

  2. 考虑调用GetXML方法的代码,并确保它不会为多个工作人员分配相同的threadnumber

答案 1 :(得分:0)

似乎这是一个逻辑上的失败。现在我已经调试了每个线程的实际变量内容。所以似乎一切都好,没有变量可以被覆盖。

因此我编辑了代码,以便在第三次重试后方法中止。

private XmlDocument GetXML(string ApiUrl, int threadnumber)
    {
        object thislock = new object();
        lock (thislock)
        {
            var mySourceDoc = new XmlDocument();
            try
            {
                var httpRequest = (HttpWebRequest)WebRequest.Create(ApiUrl);
                httpRequest.Timeout = 50000;
                if (cb_Proxy.Checked == true)
                {
                    WebProxy proxy = new WebProxy();
                    Uri newUri = new Uri("http://" + txt_Proxy.Text);
                    proxy.Address = newUri;
                    httpRequest.Proxy = proxy;
                }
                var response = (HttpWebResponse)httpRequest.GetResponse();
                var receiveStream = response.GetResponseStream();
                receiveStream.ReadTimeout = 150000;
                mySourceDoc.Load(receiveStream);
                receiveStream.Close();
                retries[threadnumber - 1] = 1;
            }
            catch (Exception ex)
            {
                retries[threadnumber - 1]++;
                if (retries[threadnumber - 1] == 4)
                {
                    consoletext = "Endgültiger Abbruch von Url " + ApiUrl + Environment.NewLine;
                    writeLog("Worker: " + threadnumber + " Endgültiger Abbruch von Url " + ApiUrl);
                }
                else
                {
                    consoletext = "Fehler: " + ex.Message + "; Neuer Versuch " + retries[threadnumber - 1] + " von Url " + ApiUrl + Environment.NewLine;
                    writeLog(ex.Message + "; Versuch: " + retries[threadnumber - 1] + " Thread: " + threadnumber + " URL: " + ApiUrl);
                }
                //DialogResult MessBox = MessageBox.Show("Der Server ist nicht erreichbar oder es ist ein anderes Problem mit dem Server aufgetreten. Es wird automatisch 3 mal erneut versucht. Fehlermeldung anzeigen?", "Fehler", MessageBoxButtons.YesNo, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                //if (MessBox == DialogResult.Yes) MessageBox.Show(ex.Message);

                if (threadnumber == 1) workingHelper1.ReportProgress(counterhelper[0]);
                if (threadnumber == 2) workingHelper2.ReportProgress(counterhelper[1]);
                if (threadnumber == 3) workingHelper3.ReportProgress(counterhelper[2]);
                if (threadnumber == 4) workingHelper4.ReportProgress(counterhelper[3]);
                if (threadnumber == 5) workingHelper5.ReportProgress(counterhelper[4]);
                if (threadnumber == 6) workingHelper6.ReportProgress(counterhelper[5]);
                if (threadnumber == 7) workingHelper7.ReportProgress(counterhelper[6]);
                if (threadnumber == 8) workingHelper8.ReportProgress(counterhelper[7]);
                Thread.Sleep(3000);
            }
            finally
            {
                if (retries[threadnumber - 1] > 1 && retries[threadnumber - 1] <= 3) GetXML(ApiUrl, threadnumber);
                //if (retries[threadnumber - 1] >= 4) retries[threadnumber - 1] = 1;
                else retries[threadnumber - 1] = 1;
            }
            return mySourceDoc;
        }
    }