并行函数调用和线程问题

时间:2015-03-25 13:58:20

标签: c# multithreading

很抱歉在这里发布一个与代码相关的问题。我正在调试一个由一位开发人员长时间写回来的代码。问题是两个函数被称为并行使用线程,一个函数在末尾设置变量true而另一个函数只是循环,直到变量设置为true。变量值未设置为true。在这里,我发布了一个代码片段,并告诉我代码中的错误是哪个变量没有被第一个函数设置为true。

当用户点击按钮时,则调用两个函数

       private void UPDATE_Click(object sender, System.EventArgs e)
        {
            SavedFirst();
            AddCheckThread();
        }

public void SavedFirst()
        {
                IsOpen = true;              
                System.Threading.Thread loadT = new System.Threading.Thread(new System.Threading.ThreadStart(SaveAll));
                loadT.Start();
                IsOpen = false;
        }

        private void AddCheckThread()
        {
            if (!ALLFlag)
            {
                loadingThread = new System.Threading.Thread(new System.Threading.ThreadStart(addCheck));
                loadingThread.Priority = System.Threading.ThreadPriority.Lowest;
                loadingThread.Start();
            }
        }

        private void SaveAll()
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new MethodInvoker(delegate
                {
                    ALLFlag = false;
                    if (!ALLFlag)
                    {
                        loadingThread = new System.Threading.Thread(new System.Threading.ThreadStart(AddProducts));
                        loadingThread.Priority = System.Threading.ThreadPriority.Lowest;
                        loadingThread.Start();
                    }

                }));
                return;
            }
        }

        private void AddProducts()
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new MethodInvoker(delegate
                {
                    ALLFlag = false;
                    if (System.Windows.Forms.MessageBox.Show(this, "Would you like to add the details into the Database?", "Add?", System.Windows.Forms.MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                    {
                        if (comboOUR.SelectedItem == null || comboCountry.SelectedItem == null || comboSelleble.SelectedItem == null || txtOereff.Text == "" || txtUKPrice.Text == "" || txtUSPrice.Text == "" || txtSUrCharge.Text == "" || txtOURUS.Text == "" || txtOURUK.Text == "")
                        {
                            FormValidation();
                        }
                        else
                        {
                            Gather_Data();
                            bool isInserted = false;
                            if (System.Windows.Forms.MessageBox.Show(this, "Would you like to add the details into \"Detailed-Product\" Too?", "Add?", System.Windows.Forms.MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                            {
                                isInserted = bbaProduct.BBASaveProduct();
                                if (isInserted == true)
                                {
                                    isInserted = false;
                                    isInserted = bbaProduct.InsertInProductTable(BBAProduct.DetailProductID);
                                    if (isInserted == true)
                                    {
                                        System.Windows.Forms.MessageBox.Show(this, "Product Successfully Added ", "Add");
                                    }
                                    else
                                    {
                                        System.Windows.Forms.MessageBox.Show(this, "Error Occurred !! Not Added Into the Database ", "Add");
                                    }
                                }
                                else
                                {
                                    System.Windows.Forms.MessageBox.Show(this, "Error Occurred !! Not Added Into the Database ", "Add");
                                }
                            }
                            else
                            {
                                isInserted = bbaProduct.InsertInProductTable(0);
                                if (isInserted == true)
                                {
                                    System.Windows.Forms.MessageBox.Show(this, "Successfully Inserted Into the database", "Add");
                                }
                                else
                                {
                                    System.Windows.Forms.MessageBox.Show(this, "Error Occurred !! Not Added Into the Database", "Add");
                                }

                            }
                        }
                    }
                    else
                    {
                        System.Windows.Forms.MessageBox.Show(this, "Process Cancelled By The User", "Add");
                    }
                    ALLFlag = true;
                }));
                return;
            }
        }

        #region Add Check

        private void addCheck()
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new MethodInvoker(delegate
                {
                    this.Opacity = 0.8;

                    axShockwaveFlash1.Visible = true;
                    axShockwaveFlash1.Play();
                    while (!ALLFlag)
                    {

                        int x = 0;
                    }
                    axShockwaveFlash1.Visible = false;
                    axShockwaveFlash1.Visible = false;
                    axShockwaveFlash1.StopPlay();
                    this.Opacity = 1;
                    ALLFlag = false;
                    loadingThread.Abort();
                }));
                return;
            }
        }

帮助我找出哪个ALLFlag值未设置为true的错误。感谢

1 个答案:

答案 0 :(得分:0)

首先尝试了解延迟代码试图完成的内容。

我认为这是一种非常常见的情况,其中一个线程正在等待另一个线程完成某项任务。你的先行者选择了最天真但非常错误的模式来处理这个问题:

while (!ALLFlag)
{
  int x = 0;
} 

这是她试图让一个线程暂停工作直到另一个线程设置标志的代码所在。

最先进的解决方案是使用EventWaitHandle或其中一种变体。


另一方面,除了我看你的代码之外,我认为你需要花费大量时间来理解你想要并行完成哪些任务,谁启动它们以及何时启动它们,以及它们之间的共享资源和同步点。 我不认为这一个标志是该代码的单个问题。