.NET WINFORM 多线程编程

1,创建委托:

  private delegate void CustomPropertyLoad1(List<CustomProperty> properties, FlowLayoutPanel container);

  2个参数用来传递方法中用到的界面控件。

2.   Thread LoadCustomPropertyThread;

        private Thread LoadCustomProperties(List<CustomProperty> properties)
{
if (properties != null)
{

var thread
= new Thread(new ThreadStart(delegate()
{
try
{
var count
= 0;
do
{
if (this.InvokeRequired)
{
this.Invoke(new CustomPropertyLoad1(LoadCustomPropertiesUI), this.editproperties, this.flowLayoutPanel1);
break;
}
Thread.Sleep(
100);
count
++;
}
while (count < 10);
}
catch
{

}
}));
thread.IsBackground
= true;
thread.Start();
return thread;
}
else
{
this.flowLayoutPanel1.Controls.Clear();
}
return null;
}

  

        private void LoadCustomPropertiesUI(List<CustomProperty> p, FlowLayoutPanel container)
{
Application.DoEvents();
container.SuspendLayout();
var count
= p.Count();
for (var i = 0; i < count; i++)//增量添加自定义属性
{
var customPropertyPanel
= new CustomPropertyPanel();
customPropertyPanel.Property
= p[i];
customPropertyPanel.Width
= container.Width - 20;
container.Controls.Add(customPropertyPanel);

}
container.ResumeLayout();
}

  

LoadCustomPropertyThread = LoadCustomProperties(this.editproperties);

原文地址:https://www.cnblogs.com/netact/p/2127765.html