可能重复:
C# 下载器:我应该使用线程、BackgroundWorker 还是 ThreadPool?
C#,如何在当前线程中找到在其他线程中创建的东西?
所以我有以下代码
下载器.cs
class Downloader
{
private WebClient wc = new WebClient();
public void saveImages(string picUrl, string path)
{
this.wc.DownloadFile(picUrl, path + p);
Form1.Instance.log = picUrl + " is downloaded to folder " + path + ".";
}
}
Form1.cs / Windows 窗体
public partial class Form1 : Form
{
static Form1 instance;
public static Form1 Instance { get { return instance; } }
protected override void OnShown(EventArgs e)
{
base.OnShown(e);
instance = this;
}
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
instance = null;
}
public string log
{
set { logbox.AppendText(value); } // Logbox is Rich Text Box
}
private void ThreadJob()
{
Downloader r = new Downloader();
r.saveImages("http://c.org/car.jpg","c:/temp/");
}
private void download_Click(object sender, EventArgs e)
{
ThreadStart job = new ThreadStart(ThreadJob);
Thread thread = new Thread(job);
CheckForIllegalCrossThreadCalls = false;
thread.Start();
}
}
我需要Form1.Instance.log = picUrl + " is downloaded to folder " + path + ".";
在没有CheckForIllegalCrossThreadCalls
设置的情况下开始工作,false
因为我听说这是做事的坏方法。
PS:: 部分代码缺失但我认为相关信息是有的