简体   繁体   English

如何在多线程.NET应用程序中的单个表单上更新多个控件?

[英]How do I update multiple controls on a single form in a multithreaded .NET application?

I have a form that has six instances of a UserControl spread across three TabPages. 我有一个窗体,其中有六个横跨三个TabPage的UserControl实例。 Each UserControl contains a CheckedListBox that can contain a relatively large number of items (200k+). 每个UserControl都包含一个CheckedListBox,它可以包含相对大量的项目(200k +)。 Retrieving and setting the data is done by calling into UpdateRecords in the below code: 通过调用以下代码中的UpdateRecords来获取和设置数据:

private void UpdateRecords()
{
    List<Records> oRecords = ServerDataAccess.GetRecords(m_sConnectionString);

    Thread LoadRecords1 = new Thread(() => UserControl1.Records = oRecords);
    LoadRecords1.Start();

    Thread LoadRecords2 = new Thread(() => UserControl2.Records = oRecords);
    LoadRecords2.Start();

    Thread LoadRecords3 = new Thread(() => UserControl3.Records = oRecords);
    LoadRecords3.Start();
}

public List<Record> Records
{
    set
    {
        //Make deep copy of records so changes to records in one UserControl do not affect others
        AddRecordsToCheckedListBox(value.AsParallel().Select(oRecord => oRecord.Clone()).OrderBy(oRecord => oRecord.Name).ToArray());
    }
}

delegate void AddRecordsToCheckedListBoxDelegate(Record[] Records);
private void AddRecordsToCheckedListBox(Record[] Records)
{
    if (InvokeRequired)
    {
        Invoke(new AddRecordsToCheckedListBoxDelegate(AddRecordsToCheckedListBox), new object[] { Records });
    }
    else
    {
        clbRecords.SuspendLayout();
        clbRecords.Items.AddRange(Records);
        clbRecords.ResumeLayout();
    }
}

While creating the copies of the arrays, CPU usage spikes to 100%, as expected, but once the threads get to the point where they all start updating the controls with the data arrays, CPU usage is pegged at 50% (dual core system) due to needing to invoke the call, creating a bottleneck. 在创建阵列副本时,CPU使用率达到了预期的100%,但是一旦线程达到它们都开始使用数据阵列更新控件的地步,CPU使用率将固定为50%(双核心系统)由于需要调用该呼叫,因此造成了瓶颈。

Currently, loading data takes approximately 3 minutes, and benchmarking indicated that at a minimum, about 2:00-2:30 of that time is spent executing clbRecords.Items.AddRange(Records); 当前,加载数据大约需要3分钟,而基准测试表明,至少有2:00-2:30的时间花费在执行clbRecords.Items.AddRange(Records); in AddRecordsToCheckedListBox. 在AddRecordsToCheckedListBox中。 Each UserControl is functionally isolated from each other, so changes to one have no impact to any of the others, they do, however, need to remain on the same form. 每个UserControl在功能上都是相互隔离的,因此对一个用户的更改不会对其他任何控件产生影响,但是确实需要保持相同的形式。

Is there a way to load the array of records into each of these controls that takes advantage of having multiple cores available to reduce the bottleneck? 有没有一种方法可以利用多个核来减少瓶颈,从而将记录数组加载到每个控件中?

Updating the controls will be only on the UI thread, that will only use 1 core. 仅在UI线程上更新控件,而UI线程仅使用1个内核。

And apart from that, to achieve any efficiency in your program you should not try parallellism, but reducing your data. 除此之外,为了提高程序效率,您不应尝试并行处理,而应减少数据量。 Eg not fill complete records, but only its Id, Name. 例如,不填写完整记录,仅填写其ID,名称。 That should be sufficient for the UI. 这对于UI来说应该足够了。

Probably you will also need paging, because 200k items in a checkedlistbox is not really user friendly. 可能还需要分页,因为核对清单框中的200k项实际上并不是用户友好的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我应该创建多个ASP.Net ASCX控件还是创建多个控件的单个控件? - Should I create multiple ASP.Net ASCX Controls or a single Control to do multiple things 如何将单个ASP .NET应用程序配置为托管在多个域上? - How do I configure a single ASP .NET application to be hosted on multiple domains? 如何在多线程应用程序中衡量负载平衡 - How do I measure load balancing in a multithreaded application 如何在多线程WPF应用程序中显示更改的图像? - How do I display changing images in a multithreaded WPF application? 如何为多个用户控件使用单个表单按钮 - How to use a single form button for multiple user controls 如何使用 BackgroundWorker 更新 Form1 上的多个控件? - How do I update several controls on my Form1 using BackgroundWorker? 多线程应用程序更新数据库 - Multithreaded application to update database 如何使用 window 在 Windows 表单中进行多个控件自动调整大小? - How do I make multiple controls in a Windows Form automatically resize with the window? 如何使用Windows窗体中的一个按钮控制多个用户控件? - How do I control multiple user controls with one button in Windows form? 以单一形式在多个容器之间移动控件 - Move controls between multiple containers in a single form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM