可能重複:
C# Downloader: should I use Threads, BackgroundWorker or ThreadPool?
C# , how reach something in current thread that created in other thread?如何訪問Form.cs方法在其他類當出現多線程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 ::某些代碼丟失,但我認爲相關信息是有