c# 跨线程访问窗体UI

定义个结构体用于存储线程中传递的参数信息

struct ImgInfo
{
    public string url;
    public string path;
};

 

参数传递到线程中 

ImgInfo img1;  //定义一个结构体,用于向线程中传递下载的参数
img1.url = s1;
img1.path = path;

Thread thread1 = new Thread(new ParameterizedThreadStart(DownImg));
thread1.Start(img1);

  

访问窗体中的UI控件,改成自己需要修改的窗体控件。

 private void DownImg(object img)
 {
    ImgInfo img1 = (ImgInfo)img;
    WebClient wc1 = new WebClient();
    if (L1.InvokeRequired) 
    {
       this.BeginInvoke(new Action<string>((x) => { L1.Items.Add(x.ToString()); }), "正在下载-->   "+img1.url);
    }
wc1.DownloadFileAsync(new Uri(img1.url), img1.path); }

  

posted @ 2019-04-27 08:58  wjbych  阅读(774)  评论(0编辑  收藏  举报