zoukankan      html  css  js  c++  java
  • winform 防止主界面卡死

    总结网络上的解决方案:新线程=> 委托=> 主界面的异步更新方法(IAsyncResult BeginInvoke(Delegate method)),一句话就是通过委托调用另一个线程的异步方法.

    验证方法:当线程执行时,拖拽主窗体,没有卡死迹象.

     1 using System;
     2 using System.Collections.Generic;
     3 using System.ComponentModel;
     4 using System.Data;
     5 using System.Drawing;
     6 using System.Linq;
     7 using System.Text;
     8 using System.Threading;
     9 using System.Threading.Tasks;
    10 using System.Windows.Forms;
    11 
    12 namespace WinFormInvoke
    13 {
    14     public partial class frMain : Form
    15     {
    16         Invoker Invoker;
    17 
    18         public frMain()
    19         {
    20             InitializeComponent();
    21         } 
    22 
    23         private void frMain_Load(object sender, EventArgs e)
    24         {
    25             Invoker = new Invoker(AsynUpdateTxtMethod);
    26         }
    27 
    28         public void UpdateTxtMethod(string msg)
    29         {
    30             tbResult.AppendText(msg + "
    ");
    31             tbResult.ScrollToCaret();
    32         }
    33 
    34         public void AsynUpdateTxtMethod(string msg)
    35         {
    36             if (tbResult.InvokeRequired)
    37             {
    38                 this.BeginInvoke(new Invoker.CallbackFunc(UpdateTxtMethod), msg);
    39             }
    40             else
    41             {
    42                 UpdateTxtMethod(msg);
    43             }
    44         } 
    45 
    46         private void btnStart_Click(object sender, EventArgs e)
    47         {
    48             Invoker.DoThread(int.Parse(tbAmount.Text.Trim()));
    49         } 
    50     }
    51 }
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading;
     6 using System.Threading.Tasks;
     7 
     8 namespace WinFormInvoke
     9 {
    10    public class Invoker
    11    {
    12         public delegate void CallbackFunc(string msg);
    13         public CallbackFunc AsynCallback;
    14                  
    15         public Invoker(CallbackFunc callback)
    16         {
    17             AsynCallback = callback;
    18         }
    19 
    20         public virtual void TheadMethod(int amount)
    21         {
    22             this.AsynCallback("线程开始");
    23             for (int i = 0; i < amount; i++)
    24             {
    25                 this.AsynCallback(i.ToString());               
    26                 Thread.Sleep(1000);
    27             }
    28 
    29             this.AsynCallback("线程结束");
    30         }
    31 
    32         public virtual void DoThread(int amount)
    33         {
    34             Thread t = new Thread(new ThreadStart(delegate
    35             {
    36                 TheadMethod(amount);
    37             }));
    38 
    39             t.Start();
    40         }
    41     }
    42 }
    Invoker

    Code:  WinFormInvoke.zip

  • 相关阅读:
    greendao的基本操作
    编写clearedit的安卓控件
    获得edittext的图片大小
    设置edittext的样式
    安卓topbar编码实战
    安卓titlebar的组合控件使用
    安卓中使用iconfont
    async的用法
    fastjson解析服务端返回的数据
    安卓数据持久化
  • 原文地址:https://www.cnblogs.com/AspDotNetMVC/p/5693440.html
Copyright © 2011-2022 走看看