并行函数调用和线程问题

本文关键字:问题 线程 函数调用 并行 | 更新日期: 2025-01-25 12:36:30

很抱歉在这里发布一个与代码有关的问题。我正在调试一个代码,这个代码是一个开发人员很久以前写的。问题是两个函数是使用线程并行调用的,一个函数在末尾设置一个变量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的错误所在。感谢

并行函数调用和线程问题

首先要了解lagacy代码想要实现什么。

我认为这是一种非常常见的情况,一个线程正在等待另一个线程来完成一些任务。你的前辈选择了最天真但非常错误的模式来处理:

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

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

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


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