首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

跨线程操作控件有关问题 InvokeRequired

2012-09-04 
跨线程操作控件问题 InvokeRequired各位高手帮忙看下以下代码,多线程跨类对控件赋值控件没有显示值呢?在同

跨线程操作控件问题 InvokeRequired
各位高手帮忙看下以下代码,多线程跨类对控件赋值控件没有显示值呢?在同一类里面控件就能显示,帮帮忙看下问题出现在哪,程序没有报错

C# code
      public partial class PhotoMain : Form    {/开始执行 private void button_ceshi_Click(object sender, EventArgs e)        {            GETI premierLeague = new GETI();            Thread threads = new Thread(delegate() { premierLeague.sss(); });            threads.Name = "PremierLeague";            threadlist.Add(threads);            threads.Start();        }        delegate void PhotoSta(string PhotoSum, string ImgSum, string CJ);        /// <summary>        /// 更新状态栏        /// </summary>        public void ThisKeyWords(string PhotoSum, string ImgSum, string CJ)        {            lock (thisLock)            {                try                {                    if (this.statusBar_Photo.InvokeRequired)                    {                        PhotoSta d = new PhotoSta(ThisKeyWords);                        this.Invoke(d, new object[] { PhotoSum, ImgSum, CJ });                    }                    else                    {                        if (PhotoSum != null)                        {                            this.statusBar_Photo.Panels[0].Text = PhotoSum;                        }                        if (ImgSum != null)                        {                            this.statusBar_Photo.Panels[1].Text = ImgSum;                        }                        if (CJ != null)                        {                            this.statusBar_Photo.Panels[2].Text = CJ;                                                   }                                           }                }                catch (Exception ex) { error.LogError("Photo更新状态栏出错:", ex); }            }        }} public class GETI    {        public void sss()        {            PhotoMain ww = new PhotoMain();            ww.ThisKeyWords(null, null, "显示停止!");        }    }


[解决办法]
PhotoMain ww = new PhotoMain();

你这里不是 new 了一个新画面么?不是当前的。

把当前的 Form 传进去,更新可以。

public void sss(PhotoMain photoMain)
{
photoMain.ThisKeyWords(null, null, "显示停止!");
}
[解决办法]
private void button_ceshi_Click(object sender, EventArgs e)
{
GETI premierLeague = new GETI();
Thread threads = new Thread(delegate() { premierLeague.sss(this); });
threads.Name = "PremierLeague";
threadlist.Add(threads);
threads.Start();
}

热点排行