主线程在C#中锁定 [英] Main thread locking up in C#
问题描述
我面临的一个问题。我点击一个按钮时调用几个方法,虽然主线程锁定了,所以我创建类的实例(也就是 Form1中
)例如: Form1Object
,然后调用的方法为使按键: Form1Object.Check1
等
虽然线程仍然锁定(即GUI没有响应一段)反正有绕开这一点,任何例子是大大AP preciated。
在code的问题如下:
私人无效StartChecks_Click(对象发件人,EventArgs的)
{
Form1中Form1Object =新Form1中();
Form1Object.InitChecks();
}
公共无效InitChecks()
{
检查1();
CHECK2();
Check3();
Check4();
Check5();
Check6();
Check7();
}
您需要做的是启动并行线程做检查,这样你就不会锁定在主线程:
私人无效StartChecks_Click(对象发件人,EventArgs的)
{
Form1中Form1Object =新Form1中();
线程t =新主题(
O =>
{
Form1Object.InitChecks();
});
t.Start();
}
希望你不必实际检索这些计算什么,所以你可以火,忘掉它。
I am faced with a problem. I am clicking a button that is calling several methods, although the main thread is locking up, so I created an instance of my class (which is Form1
) e.g. Form1Object
and then the button called the methods as so: Form1Object.Check1
and so on.
Although the thread still locked up (i.e. the GUI became unresponsive for a period) Is there anyway of getting around this, any examples would be greatly appreciated.
The code in question is below:
private void StartChecks_Click(object sender, EventArgs e)
{
Form1 Form1Object = new Form1();
Form1Object.InitChecks();
}
public void InitChecks()
{
Check1();
Check2();
Check3();
Check4();
Check5();
Check6();
Check7();
}
What you need to do is start a parallel thread to do the check, so you won't lock up the main thread:
private void StartChecks_Click(object sender, EventArgs e)
{
Form1 Form1Object = new Form1();
Thread t = new Thread(
o =>
{
Form1Object.InitChecks();
});
t.Start();
}
Hopefully you don't need to actually retrieve anything from those calculations, so you can just fire and forget about it.
这篇关于主线程在C#中锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!