1.在处理较慢的代码前面加DoEvents()
Application.DoEvents();
2.使用多线程
Thread th = Thread.CurrentThread;th.Name = "MainThread";Console.WriteLine("This is {0}", th.Name);Console.ReadKey();
3.使用委托异步,对于有参的,
//调用Application.DoEvents();lock (SequenceLock){Run_delegate_zi run = new Run_delegate_zi(Run_zi);IAsyncResult result = run.BeginInvoke(tbname, i, null, null);//有返回值可去掉下面代码//string result1 = run.EndInvoke(result);}//顶一个Lockprivate static readonly object SequenceLock = new object();//定义委托及方法名称private delegate void Run_delegate_zi( string tbname, int i);public void Run_zi(string tbname, int i)//无返回值{//...业务代码}//4.解决线程间操作无效//在窗体Load方法里写System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
如果觉得《C# 防止Winform窗体假死》对你有帮助,请点赞、收藏,并留下你的观点哦!