Winfrom 用户自定义控件 ItemControl 中定义 Image 控件 ,使用异步任务更新图片 路径,卡顿

Winfrom 用户自定义控件 ItemControl 中定义 Image 控件 ,使用异步任务更新图片 路径,卡顿

卡顿现象截图

img

显示暂无图片,过1秒时间,又重新恢复了图片

img

1.在 DialogShowAIItems 调用ItemControl 控件代码


ItemControl itemControl = new ItemControl();
itemControl.Name = item.ItemName;
itemControl.ItemName = item.ItemName;
itemControl.IsUseAI = true;
itemControl.BackColor = Color.White;
itemControl.ItemCode = item.ItemNo;
// 设置图片路径
itemControl.ImagePath = item.ItemImage;
itemControl.CostPrice = Convert.ToDecimal(item.ItemSalePrice);
itemControl.ImageHeight = 106;
itemControl.ImageWidth = 147;
itemControl.Size = new Size(167, 145);
itemControl.Location = new Point(8 * num + itemControl.Width * (num - 1), itemControl.Height * (num2 - 1) + 10 * (num2 - 1) + 15);
itemControl.Tag = item;
itemControl.Click += AIItemClick;
panelContent.Controls.Add(itemControl);

2.ItemControl 控件代码 使用异步任务 给 Image 控件异步刷新 绑定图片代码

    public virtual string ImagePath
        {
            get
            {
                return imagePath;
            }
            set
            {
                if (string.IsNullOrEmpty(value))
                {
                    _img = this.SkinImage(Resources.ResourceManager, (ItemControl f) => f._img) ?? Resources.NoPicture;
                    Invalidate();
                    return;
                }
                Task<Image> task = new Task<Image>(delegate(object path)
                {
                    Bitmap result = null;
                    try
                    {
                        string obj = path.ToString();
                        string text = obj.Substring(obj.LastIndexOf('/') + 1);
                        result = ((!File.Exists(ImgDefaultPath + "\\" + text)) ? ((this.SkinImage(Resources.ResourceManager, (ItemControl f) => f._img) as Bitmap) ?? Resources.NoPicture) : new Bitmap(ImgDefaultPath + "\\" + text));
                        imagePath = text;
                        return result;
                    }
                    catch
                    {
                        return result;
                    }
                }, value);
                task.ContinueWith(delegate(Task<Image> tsk)
                {
                    _img = tsk.Result;
                    if (base.IsHandleCreated)
                    {
                        Invoke((Action)delegate
                        {
                            Invalidate();
                        });
                    }
                });
                task.Start();
            }
        }

itemControl.ImagePath = item.ItemImage;

item对象哪来的?有可能 item.ItemImage值为空,调用set方法后由于为空直接return退出了,下面的线程并没有执行

@CSDN专家-showbo

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632