首页新闻找找看学习计划

c#绝顶高手进

0
悬赏园豆:5 [已解决问题] 解决于 2013-01-02 20:12

namespace WindowsFormsApplication1
{
    public partial class Myform : Form
    {
        public Myform()
        {
            InitializeComponent();
            Initial();
        }

        public UiBindList<OBJ> _list { get; set; }

        private void Initial()
        {
            _list = new UiBindList<OBJ> { SynchronizationContexts = SynchronizationContext.Current };
            dataGridView1.DataBindings.Add("DataSource", this, "_list", false, DataSourceUpdateMode.OnPropertyChanged);

            new Thread(() =>
            {
                while (true)
                {
                    Thread.Sleep(1000);
                    _list.Add(new OBJ { Name = "C#" });
                }
            })
            {
                IsBackground = true,
            }
            .Start();
        }
    }

    public class UiBindList<T> : BindingList<T>
    {
        public SynchronizationContext SynchronizationContexts { get; set; }

        public void Excute(Action action, object state = null)
        {
            if (SynchronizationContexts == null)
                action();
            else
                SynchronizationContexts.Post(p => action(), state);
        }

        public new void Add(T item)
        {
            Excute(() => base.Add(item));
        }

        public new void Remove(T item)
        {
            Excute(() => base.Remove(item));
        }
    }

    public class OBJ { public string Name { get; set; } }

在Add方式中 报错!!对象的当前状态使该操作无效。

彬彬@科比的主页 彬彬@科比 | 初学一级 | 园豆:43
提问于:2012-12-30 21:52
< >
分享
最佳答案
0
 public new void Add(T item)
        {
            Excute(() => Add(item));
        }
 
        public new void Remove(T item)
        {
            Excute(() => Remove(item));
        }
这两个方法去掉base.的引用就好了,改为上面的调用
收获园豆:5
RyanCheng | 菜鸟二级 |园豆:474 | 2012-12-31 10:53

这样虽然不报错了,但是窗口会一直处于卡死状态,如果不考虑使用SynchronizationContext的话,可以考虑使用下面的代码

 public List<OBJ> _list = new List<OBJ>();
 
        private void Initial()
        {
            Thread th = new Thread(() =>
                                       {
                                           while (true)
                                           {
                                               Thread.Sleep(1000);
                                               _list.Add(new OBJ { Name = "C#" });
                                               this.Invoke(new Action(() =>
                                                                          {
                                                                              dataGridView1.DataSource = null;
                                                                              dataGridView1.DataSource = _list;
                                                                          }));
                                           }
                                       });
 
            th.Start();
        }
RyanCheng | 园豆:474 (菜鸟二级) | 2012-12-31 11:07

@RyanCheng: 这种方式我会..想用下其它的方式而已。。

彬彬@科比 | 园豆:43 (初学一级) | 2012-12-31 18:19
其他回答(2)
0

_list.Add(new OBJ(){Name="C#"}); 是不是这里少写了小括号?

jone_e | 园豆:1410 (小虾三级) | 2012-12-30 23:21

c#3.0就可以这么写了。对象自动化。。没有问题的。

支持(0) 反对(0) 彬彬@科比 | 园豆:43 (初学一级) | 2012-12-31 18:20
0

while (true)
                {
                    Thread.Sleep(1000);
                    _list.Add(new OBJ { Name = "C#" });
                }

_list.Add(new OBJ { Name = "C#" });这句没有写括号,_list.Add(new OBJ(){Name="C#"});

妍珊 | 园豆:1169 (小虾三级) | 2012-12-31 10:09
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册