简体   繁体   English

C#WM6 Compact Framework跨线程通信问题

[英]C# WM6 Compact Framework Cross Thread Communication Problem

I'm having a problem updating a control on my ui from a thread created using 我在使用创建的线程更新ui上的控件时遇到问题

ThreadPool.QueueUserWorkItem

Inside this thread i am calling 在此线程中,我正在调用

addControlToPanel(li);

As shown here 如图所示

private delegate void addControlToPanelDelegate(ListItem li);
private void addControlToPanel(ListItem li)
{
    if (panel1.InvokeRequired)
    {
        addControlToPanelDelegate d = new addControlToPanelDelegate(addControlToPanel);
        panel1.Invoke(d, new object[] { li });
    }
    else
    {
        panel1.Controls.Add(li);
    }
}

On first entry to addControlToPanel() panel1.InvokeRequired == true so a delegate is instantiated and then invoked, now on this entry into addControlToPanel(), panel1.InvokeRequired == false so I add the control to the panel. 在addControlToPanel()的第一个条目上panel1.InvokeRequired == true,因此实例化然后调用了一个委托,现在在addControlToPanel()的该条目上,panel1.InvokeRequired == false,所以我将控件添加到面板中。

My problem is that even after invoking against the control and panel1.InvokeRequired being false I get an error on the 我的问题是,即使在调用控件和panel1.InvokeRequired为false后,我也会在

panel1.Controls.Add(li); 

line stating the usual 说明通常的行

Control.Invoke must be used to interact with controls created on a separate thread. 必须使用Control.Invoke与在单独线程上创建的控件进行交互。

Can anybody spot the problem? 有人可以发现问题吗? Using invoke to access controls on another thread is something i have done many times before but this one has me stumped! 我之前已经做过很多次使用invoke来访问另一个线程上的控件的操作,但是这让我很困惑!

TIA TIA

OneSHOT OneSHOT

It seems the problem was passing a control on the invoke (ListItem is a form control that i have created) instead i refactored the code so that rather than creating the control and passing it into the 似乎问题出在传递调用时传递了一个控件(ListItem是我创建的表单控件),而不是我重构了代码,而不是创建控件并将其传递给

addControlToPanel()

method, i pass all the info needed to create the control inside the method after invoking as so 方法,因此我在调用方法后传递了在方法内部创建控件所需的所有信息

private delegate void addControlToPanelDelegate(string picname, string thumburl, PicasaEntry entry, Int32 top, EventHandler clickevent);
private void addControlToPanel(string picname, string thumburl, PicasaEntry entry, Int32 Ordinal,EventHandler clickevent)
{
    if (panel1.InvokeRequired)
    {
        addControlToPanelDelegate d = new addControlToPanelDelegate(addControlToPanel);
        this.Invoke(d, new object[] { picname, thumburl, entry, Ordinal, clickevent });
        //panel1.Invoke(d, new object[] { li });
    }
    else
    {
        ListItem li = new ListItem(picname, thumburl, entry);
        li.Top = Ordinal * li.Height;
        li.Click += clickevent;
        panel1.Controls.Add( li);
    }
}

Cheers 干杯

OneSHOT OneSHOT

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM