首页 新闻 会员 周边

关于winform 线程加载控件数据的报错,,demo代码附上 哪位老哥指点一番。。

0
悬赏园豆:50 [已解决问题] 解决于 2016-11-07 10:51
        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里面,然后各种问题就出来了。。
JO的美好时光的主页 JO的美好时光 | 初学一级 | 园豆:14
提问于:2016-11-04 16:34
< >
分享
最佳答案
0

了解一下 BackgroundWorker

收获园豆:50
Yu | 专家六级 |园豆:12980 | 2016-11-05 09:12

这个东西在项目中也有使用,但是解决不了我目前的问题。。

JO的美好时光 | 园豆:14 (初学一级) | 2016-11-05 11:06
其他回答(3)
0

看着口气好像又是你。Invoke,这种是链表式的变化即使当时不出错都不代表未来不出错。

花飘水流兮 | 园豆:13560 (专家六级) | 2016-11-04 17:24
0

加上
this.begininvoke((methodinvoker)delegate
{
界面控制
}
)

勤劳致富 | 园豆:204 (菜鸟二级) | 2016-11-04 18:56

加在哪儿?

支持(0) 反对(0) JO的美好时光 | 园豆:14 (初学一级) | 2016-11-05 09:10
0
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;
        }
    }
}

 

JO的美好时光 | 园豆:14 (初学一级) | 2016-11-07 10:51
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册