如何在线程中处理continue语句

时间:2014-10-31 12:04:46

标签: c# multithreading

下面的代码用于将数据从本地系统移动到外部硬盘驱动器。客户的要求是使用线程来加速页面/分钟的复制。

我面临一个问题。我在while循环下使用了continue语句,这在线程下是不可接受的。有人可以建议替代相同的。

感谢。

    private void PromoteMain(TextWriter streamWriter)
    {
        string driveLetterString;
        driveLetterString = GetExternalDrive();
        if (!string.IsNullOrEmpty(driveLetterString))
        {

            DataSet dataSet;
            DateTime startSnooze;
            DataRow row;
            string imageIdString;
            string reelIdString;
            string destImagePathString;
            string[] strArrays;
            int i;
            string fileName;
            string[] strArrays3;
            string[] strArrays4;
            bool flag;
            string[] strArrays5;
            bool flag1;
            string[] strArrays6;
            bool flag2;
            string[] files;
            TimeSpan timeSpan1;
            IEnumerator iEnumerator1;
            string[] stringArray1;
            IDisposable iDisposable1;
            dataSet = this.GetImagesFromDatabase();
            if (((dataSet != null) && (dataSet.Tables.Count != 0)) && (dataSet.Tables[0].Rows.Count != 0))
            {
                iEnumerator1 = dataSet.Tables[0].Rows.GetEnumerator();
                try
                {
                    #region goto L_05A9
                    //goto L_05A9;
                    while (iEnumerator1.MoveNext())
                    {
                        driveLetterString = GetExternalDrive();
                        new Thread(() =>
                        {
                            if (!string.IsNullOrEmpty(driveLetterString))
                            {
                                row = ((DataRow)iEnumerator1.Current);
                                imageIdString = row["imageid"].ToString();
                                reelIdString = row["reelid"].ToString();
                                destImagePathString = row["destimagepath"].ToString();
                                if (!Directory.Exists(destImagePathString))
                                {
                                    if (this.stringBuilderFail.Length > 0)
                                    {
                                        StringBuilder stringBuilder7 = this.stringBuilderFail.Append(",");
                                    }
                                    StringBuilder stringBuilder8 = this.stringBuilderFail.Append(imageIdString);
                                    this._lblFailed.Text = Convert.ToString(((int)(Convert.ToInt32(this._lblFailed.Text) + 1)));
                                    if (this.stringBuilderFail.Length > 7000)
                                    {
                                        this.UpdateImagesInDatabase(this.stringBuilderFail.ToString(), "5");
                                        StringBuilder stringBuilder9 = this.stringBuilderFail.Remove(0, this.stringBuilderFail.Length);
                                    }
                                    this.RefreshFormView();
                                    continue;
                                }
                                if (!Directory.Exists((driveLetterString + "newspaper\\" + reelIdString + "\\")))
                                {
                                    DirectoryInfo directoryInfo1 = Directory.CreateDirectory((driveLetterString + "newspaper\\" + reelIdString + "\\"));
                                }
                                strArrays = new string[] { ".pdf", ".html", "_clean.html", "_lx.jpg" };
                                for (i = 0; (i < strArrays.Length); i++)
                                {
                                    fileName = strArrays[i];
                                    try
                                    {
                                        if (i != 2)
                                        {
                                            strArrays3 = new string[] { driveLetterString, "newspaper\\", reelIdString, "\\", imageIdString, fileName };
                                            File.Copy((destImagePathString + imageIdString + fileName), string.Concat(strArrays3));
                                        }
                                    }
                                    catch
                                    {
                                    }
                                }
                                strArrays4 = new string[] { driveLetterString, "newspaper\\", reelIdString, "\\", imageIdString, ".pdf" };
                                flag = File.Exists(string.Concat(strArrays4));
                                strArrays5 = new string[] { driveLetterString, "newspaper\\", reelIdString, "\\", imageIdString, "_lx.jpg" };
                                flag1 = File.Exists(string.Concat(strArrays5));
                                strArrays6 = new string[] { driveLetterString, "newspaper\\", reelIdString, "\\", imageIdString, ".html" };
                                flag2 = File.Exists(string.Concat(strArrays6));
                                if ((flag && flag1) && flag2)
                                {
                                    streamWriter.WriteLine(imageIdString);
                                    if (this.stringBuilderSuccess.Length > 0)
                                    {
                                        StringBuilder stringBuilder3 = this.stringBuilderSuccess.Append(",");
                                    }
                                    StringBuilder stringBuilder4 = this.stringBuilderSuccess.Append(imageIdString);
                                    this._lblPromoted.Text = Convert.ToString(((int)(Convert.ToInt32(this._lblPromoted.Text) + 1)));
                                    #region goto L_0453
                                    L_0453();
                                    continue;
                                    #endregion
                                }
                                files = Directory.GetFiles((driveLetterString + "newspaper\\" + reelIdString + "\\"), (imageIdString + "*.*"));
                                stringArray1 = files;
                                foreach (string str6 in stringArray1)
                                {
                                    try
                                    {
                                        File.Delete(str6);
                                    }
                                    catch
                                    {
                                    }
                                }
                                if (this.stringBuilderFail.Length > 0)
                                {
                                    StringBuilder stringBuilder1 = this.stringBuilderFail.Append(",");
                                }
                                StringBuilder stringBuilder2 = this.stringBuilderFail.Append(imageIdString);
                                this._lblFailed.Text = Convert.ToString(((int)(Convert.ToInt32(this._lblFailed.Text) + 1)));
                                #region goto L_0453
                                L_0453();
                                continue;
                                #endregion
                            }
                            else
                            {
                                break;
                            };
                        }).Start();
                    }
                    #endregion
                }
                finally
                {
                    iDisposable1 = (iEnumerator1 as IDisposable);
                    if (iDisposable1 != null)
                    {
                        iDisposable1.Dispose();
                    }
                }
                if (this.stringBuilderSuccess.Length > 0)
                {
                    this.UpdateImagesInDatabase(this.stringBuilderSuccess.ToString(), "3");
                    StringBuilder stringBuilder10 = this.stringBuilderSuccess.Remove(0, this.stringBuilderSuccess.Length);
                }
                if (this.stringBuilderFail.Length > 0)
                {
                    this.UpdateImagesInDatabase(this.stringBuilderFail.ToString(), "5");
                    StringBuilder stringBuilder11 = this.stringBuilderFail.Remove(0, this.stringBuilderFail.Length);
                }
                this.RefreshFormView();
                #region goto L_0671
                PromoteMain(streamWriter);
                #endregion
            }
            else
            {
                startSnooze = DateTime.Now;
                timeSpan1 = DateTime.Now.Subtract(startSnooze);
                while (timeSpan1.TotalSeconds < 300D)
                {
                    timeSpan1 = DateTime.Now.Subtract(startSnooze);
                    Thread.Sleep(125);
                    Application.DoEvents();

                }
                PromoteMain(streamWriter);
            }
        }
        //else
        //{
        //    MessageBox.Show("There is no space in selected drives");
        //    //there is no space in any external drive
        //}
    }

    private void L_0453()
    {
        if (this.stringBuilderSuccess.Length > 7000)
        {
            this.UpdateImagesInDatabase(this.stringBuilderSuccess.ToString(), "3");
            StringBuilder stringBuilder5 = this.stringBuilderSuccess.Remove(0, this.stringBuilderSuccess.Length);
        }
        if (this.stringBuilderFail.Length > 7000)
        {
            this.UpdateImagesInDatabase(this.stringBuilderFail.ToString(), "5");
            StringBuilder stringBuilder6 = this.stringBuilderFail.Remove(0, this.stringBuilderFail.Length);
        }
        this.RefreshFormView();
    }

1 个答案:

答案 0 :(得分:0)

用return返回continue语句。这将完成线程的执行。现在我不知道这个继续声明是如何结束的,但是由于操作并行运行,你实际上无法继续。你也应该提取这一行

row = ((DataRow)iEnumerator1.Current);

或者您冒险让枚举器在执行之前从另一个线程更改。您可能还想尝试使用Parallel.Foreach来减少可能的错误并获得更有效的执行。更好的方法是使用异步IO与异步IO(而不是使用线程的方法),并使用Task.WaitAll等待它们完成。