当在标签上同时下载东西时,它应该写下载c#

当在标签上同时下载东西时,它应该写下载c#,c#,multithreading,download,label,C#,Multithreading,Download,Label,嗨,ppl,我在用C#下载文件时试图让它写下载,我尝试了线程方法,但下载完成后它仍在写代码如下 public void yap(object o) { (o as Label).Text ="DOWNLOADING"; } private void button1_Click(object sender, EventArgs e) { CheckForIllegalCrossThreadCalls = false;

嗨,ppl,我在用C#下载文件时试图让它写下载,我尝试了线程方法,但下载完成后它仍在写代码如下

  public void yap(object o)
    {

      (o as Label).Text ="DOWNLOADING";

    }

    private void button1_Click(object sender, EventArgs e)
    {
        CheckForIllegalCrossThreadCalls = false;
        ParameterizedThreadStart p = new ParameterizedThreadStart(yap);
        Thread t = new Thread(p);
        t.Start(label2);
        string yol = Environment.CurrentDirectory;
        FtpWebRequest FTP;
        try
        {

            FileStream SR = new FileStream(yol + "\\list.gz", FileMode.Create);

            FTP = (FtpWebRequest)FtpWebRequest.Create
  (new  Uri("ftp://"+textBox1.Text+"/" + "/usr/valapp/etc/list.gz"));

            FTP.Credentials = new NetworkCredential("username", "password");

            FTP.Method = WebRequestMethods.Ftp.DownloadFile;

            FTP.UseBinary = true;

            FtpWebResponse response = (FtpWebResponse)FTP.GetResponse();

            Stream ftpStream = response.GetResponseStream();

            long cl = response.ContentLength;

            int bufferSize = 2048;
            int readCount;

            byte[] buffer = new byte[bufferSize];
            readCount = ftpStream.Read(buffer, 0, bufferSize);
            while (readCount > 0)
            {
                SR.Write(buffer, 0, readCount);
                readCount = ftpStream.Read(buffer, 0, bufferSize);
            }
            ftpStream.Close();
            SR.Close();
            response.Close();
            MessageBox.Show("File Downloaded!");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "ERROR");
        }
    }

}
}  

正如我所说的,代码正在运行,但我只想同时或首先编写下载。谢谢。

对于windows应用程序,我认为最好使用
BackgroundWorker
Dispatcher.Invoke()
更新标签文本

private void button1_Click(object sender, EventArgs e)
{
      //Update label text here..

      BackgroundWorker _backgroundWorker = new BackgroundWorker();
      _backgroundWorker.DoWork += _backgroundWorker_DoWork;
      _backgroundWorker.RunWorkerAsync();
}

void _backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
        Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background,
            new Action(delegate()
        {
            //Download code here..
        }));
}

设置
CheckForIllegalCrossThreadCalls
是一个非常糟糕的主意。它并不能保证你的代码安全——它只是阻止你发现你的代码被破坏了。这与实际情况无关,因为您目前正在UI线程上执行所有操作,这就是UI冻结的原因。谢谢。那么您建议我怎么做?要么使用异步API,要么在后台线程中执行下载,例如使用
BackgroundWorker
。不过,我之前的评论是错的——你并不是在UI线程中做所有事情。你在另一个线程中做的一件事是。。。更新用户界面。不要这样做。谢谢,但我不知道javascript。你能给我我需要的代码吗?是的,这是一个windows窗体应用程序是web应用程序还是windows应用程序?我试过了,发现了这些错误;1-名称Dispatcher在当前上下文中不存在2-命名空间“System.Windows”中不存在命名空间“Threading”类型。我缺少的内容:/n尝试添加对
WindowsBase
dll的引用