public Form1() { InitializeComponent(); System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false; } #region 加载效果附加 /// <summary> /// 加载控件 /// </summary> public bool LoadState { set { if (value) { panel1.Visible = true; } else { panel1.Visible = false; circularProgress1.Value = 0; } } } System.Windows.Forms.Timer tcir = new System.Windows.Forms.Timer(); private void Init_Tcir() { tcir = new System.Windows.Forms.Timer(); tcir.Enabled = true; tcir.Interval = 10; tcir.Tick += Tcir_Tick; } private void Tcir_Tick(object sender, EventArgs e) { if (circularProgress1.Value == 100) { circularProgress1.Value = 0; } else { circularProgress1.Value += 2; } } #endregion private void Form1_Load(object sender, EventArgs e) { Init_Tcir(); LoadData(); } private void LoadData() { Thread t = new Thread(new ThreadStart(GetData)); t.Start(); } public void GetData() { LoadState = true; #region 执行 Thread.Sleep(2000); dataGridViewX1.DataSource = null; List<string> a = new List<string>() { "asdasd", "asdasd", "asdasd", "asdasd", "asdasd", "asdasd" }; dataGridViewX1.DataSource = a;//这里是一个获取数据源的方法,因为的调用接口,网络差时会有1-2秒延迟 #endregion LoadState = false; }
外面无非就是一个
dataGridViewX1,一个被
panel1 包裹的 circularProgress,问题出在,当加载圈圈的时候,把鼠标放在
dataGridView里面,然后各种问题就出来了。。
了解一下 BackgroundWorker
这个东西在项目中也有使用,但是解决不了我目前的问题。。
看着口气好像又是你。Invoke,这种是链表式的变化即使当时不出错都不代表未来不出错。
加上
this.begininvoke((methodinvoker)delegate
{
界面控制
}
)
加在哪儿?
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false; } #region 加载效果附加 /// <summary> /// 加载控件 /// </summary> public bool LoadState { set { if (value) { panel1.Visible = true; } else { panel1.Visible = false; circularProgress1.Value = 0; } } } System.Windows.Forms.Timer tcir = new System.Windows.Forms.Timer(); private void Init_Tcir() { tcir = new System.Windows.Forms.Timer(); tcir.Enabled = true; tcir.Interval = 10; tcir.Tick += Tcir_Tick; } private void Tcir_Tick(object sender, EventArgs e) { if (circularProgress1.Value == 100) { circularProgress1.Value = 0; } else { circularProgress1.Value += 2; } } #endregion private void Form1_Load(object sender, EventArgs e) { Init_Tcir(); LoadState = true; backgroundWorker1.RunWorkerAsync(); } List<string> a = null; private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { Thread.Sleep(2000);//这里是一个获取数据源的方法,因为的调用接口,网络差时会有1-2秒延迟 a = new List<string>() { "asdasd", "asdasd", "asdasd", "asdasd", "asdasd", "asdasd" }; } private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { dataGridViewX1.DataSource = null; dataGridViewX1.DataSource = a; LoadState = false; } } }