Make a study record.
There are two requirements:
1. Click the button to load the data asynchronously without stuttering the UI.
2. Load the acquired data onto the GridView.
For demand, the code is as follows:
Public Delegate voidshowdatatabledelegate (DataTable DT); Private Async voidButton1_Click (Objectsender, EventArgs e) { //Way1.okThread T =NewThread (showdatatable); T.isbackground=true; T.start (); ////way2.ok.in. NET 4.5 //await Task.run (() = Showdatatable ()); }
/// <summary> ///If INITDT is placed in the BINDDT, it will also cause UI Kaka. Because the delegate method in invoke blocks the UI thread/// </summary> Private voidshowdatatable () {DataTable dt= Initdt ();//time-consuming Operation//This . BeginInvoke (New Showdatatabledelegate (BINDDT), New Object[]{dt}); This. Invoke (NewShowdatatabledelegate (BINDDT),New Object[] {dt}); }
Private voidBINDDT (DataTable dt) { This. Datagridview1.datasource =DT; }
WinForm loading data to the interface asynchronously