线程安全C#不工作
当我运行以下运行正常但不符合预期的代码时,假定它在安全线程上运行,但所有组件都被冻结,直到它完成线程的运行,它不应该在新线程上运行,因此您可以使用其他控件线程安全C#不工作,c#,multithreading,C#,Multithreading,当我运行以下运行正常但不符合预期的代码时,假定它在安全线程上运行,但所有组件都被冻结,直到它完成线程的运行,它不应该在新线程上运行,因此您可以使用其他控件 using System; using System.Drawing; using System.Windows.Forms; using System.Threading; public class MyFormControl : Form { public delegate void AddListItem();
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
public class MyFormControl : Form
{
public delegate void AddListItem();
public AddListItem myDelegate;
private Button myButton;
private Thread myThread;
private ListBox myListBox;
public MyFormControl()
{
myButton = new Button();
myListBox = new ListBox();
myButton.Location = new Point(72, 160);
myButton.Size = new Size(152, 32);
myButton.TabIndex = 1;
myButton.Text = "Add items in list box";
myButton.Click += new EventHandler(Button_Click);
myListBox.Location = new Point(48, 32);
myListBox.Name = "myListBox";
myListBox.Size = new Size(200, 95);
myListBox.TabIndex = 2;
ClientSize = new Size(292, 273);
Controls.AddRange(new Control[] {myListBox,myButton});
Text = " 'Control_Invoke' example";
myDelegate = new AddListItem(AddListItemMethod);
}
static void Main()
{
MyFormControl myForm = new MyFormControl();
myForm.ShowDialog();
}
public void AddListItemMethod()
{
String myItem;
for(int i=1;i<6;i++)
{
myItem = "MyListItem" + i.ToString();
myListBox.Items.Add(myItem);
myListBox.Update();
Thread.Sleep(300);
}
}
private void Button_Click(object sender, EventArgs e)
{
myThread = new Thread(new ThreadStart(ThreadFunction));
myThread.Start();
}
private void ThreadFunction()
{
MyThreadClass myThreadClassObject = new MyThreadClass(this);
myThreadClassObject.Run();
}
}
// The following code assumes a 'ListBox' and a 'Button' control are added to a form,
// containing a delegate which encapsulates a method that adds items to the listbox.
public class MyThreadClass
{
MyFormControl myFormControl1;
public MyThreadClass(MyFormControl myForm)
{
myFormControl1 = myForm;
}
public void Run()
{
// Execute the specified delegate on the thread that owns
// 'myFormControl1' control's underlying window handle.
myFormControl1.Invoke(myFormControl1.myDelegate);
}
}
使用系统;
使用系统图;
使用System.Windows.Forms;
使用系统线程;
公共类MyFormControl:窗体
{
公共委托void AddListItem();
公共addListItemMyDelegate;
私人按钮myButton;
私有线程读取;
私有列表框myListBox;
公共MyFormControl()
{
myButton=新按钮();
myListBox=新列表框();
myButton.Location=新点(72160);
myButton.Size=新尺寸(152,32);
myButton.TabIndex=1;
myButton.Text=“在列表框中添加项目”;
myButton.Click+=新建事件处理程序(按钮单击);
myListBox.Location=新点(48,32);
myListBox.Name=“myListBox”;
myListBox.Size=新尺寸(200,95);
myListBox.TabIndex=2;
ClientSize=新尺寸(292273);
AddRange(新控件[]{myListBox,myButton});
Text=“'Control_Invoke'示例”;
myDelegate=新的AddListItem(AddListItemMethod);
}
静态void Main()
{
MyFormControl myForm=新建MyFormControl();
myForm.ShowDialog();
}
public void AddListItemMethod()
{
字符串myItem;
对于(int i=1;i
假定它在安全线程上运行,但所有组件都被冻结
直到它运行完线程
当您在控件上调用某个委托时,该委托将在UI线程上运行。即,此代码将在UI线程上运行:
public void AddListItemMethod()
{
String myItem;
for(int i=1;i<6;i++)
{
myItem = "MyListItem" + i.ToString();
myListBox.Items.Add(myItem);
myListBox.Update();
Thread.Sleep(300); // freeze UI thread
}
}
问题是Invoke
在UI线程上运行委托,因此您只是创建一个线程,告诉UI线程完成所有工作。相反,您可以使用async
和wait
以及任务。延迟来简化代码
private async void Button_Click(object sender, EventArgs e)
{
String myItem;
for(int i=1;i<6;i++)
{
myItem = "MyListItem" + i.ToString();
myListBox.Items.Add(myItem);
myListBox.Update();
await Task.Delay(300);
}
}
private async void按钮\u单击(对象发送方,事件参数e)
{
字符串myItem;
对于(int i=1;与大多数代码一样,iLooks在UI线程上调用的委托中。您应该仅在需要更新控件时调用,而不是在循环中执行休眠操作。如果将此设置为异步,并使用任务。延迟,则可以避免额外的线程。您自己的注释会这样说all:“在拥有'myFormControl1'控件的底层窗口句柄的线程上执行指定的委托。”那个线程就是UI线程。谢谢,它工作得很好!我不知道怎么做,但它应该像线程一样工作?当我在后台点击按钮做一些事情时,我可以实例化另一个类吗?我仍然可以毫无问题地使用主UI吗?如果是这样的话,那就太棒了!基本上这段代码被编译成异步状态它会在每次等待时中断代码的运行,在300毫秒内UI线程可以自由地做其他事情。我理解,所以如果行{myListBox.Items.Add(myItem);}大约需要1分钟来完成UI,它将以任何方式冻结?如果我需要windows读取日志文件并更新文本框,所有UI都将冻结,直到日志停止。您可以使用async
方法读取文件(大多数.Net流具有常规同步方法的异步版本)事实上,IO是使用async
private async void Button_Click(object sender, EventArgs e)
{
String myItem;
for(int i=1;i<6;i++)
{
myItem = "MyListItem" + i.ToString();
myListBox.Items.Add(myItem);
myListBox.Update();
await Task.Delay(300);
}
}