使用后台工作程序的函数调用

问题描述 投票:-1回答:1

我只有一个函数InitializeList()。现在,我从三个背景工作者的DoWork事件中调用此函数。那么,从多个Backgroundworker调用单个函数是否并行运行?还是我必须创建三个不同的函数并将其传递给不同的后台工作程序以并行运行?

有关其他信息,此功能确实使用Windows窗体上不同控件的更新。

private void InitializeList()
    {
        try
        {
            _testMaster1 = new TestMasters();
            string query = string.Empty;
            query = "SELECT TestMasterFVT01.* FROM TestMasterFVT01 WHERE ((TestMasterFVT01.ThreadNO)<>0) ORDER BY TestMasterFVT01.TestID, TestMasterFVT01.SubTestID;";

            _testMaster1 = Singleton.Instance.GetData(query);
            mainListView1.Items.Clear();
            mainListView1.Items.AddRange(Singleton.Instance.ListViewItemCollection.ToArray());

            _itemCount1 = _testMaster1.Count;

            if (_itemCount1 == 0)
            {
                MessageBox.Show("Please check Model Setting");
                Environment.Exit(0);
            }
            progressBar1.Value = 0;
            textBoxBoardName1.Select();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Please check Model Setting");
            Environment.Exit(0);
            Logger.Error(ex);
        }
    }


void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
InitializeList();
}

void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
        {
InitializeList();
}

void backgroundWorker3_DoWork(object sender, DoWorkEventArgs e)
        {
InitializeList();
}
c# winforms backgroundworker
1个回答
-1
投票

从多个Backgroundworker调用单个函数并行运行。如果此单个函数确实使用了Windows窗体上不同控件的更新,则可以从三个后台工作程序中调用该函数


0
投票

您可以使用“ Parallel.Invoke”获得结果。在下面的链接中找到更多信息。https://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/how-to-use-parallel-invoke-to-execute-parallel-operations

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.