2009-10-03 3 views
0

다른 스레드의 FTP 서버에서 파일을 다운로드하고 싶습니다. 문제는이 스레드가 내 응용 프로그램이 정지되었음을 의미합니다. 여기에 코드가 있는데, 내가 뭘 잘못하고 있니? 어떤 도움에 의해합니다 감사합니다 :)MyThread.Join()은 전체 응용 프로그램을 차단합니다. 왜?

(물론 내가 'ReadBytesThread'종료 스레드 때까지 반복 중지합니다.) 내가 새 스레드 만들기 : 당신이 차에 DownloadFiles를 호출하고

DownloadThread = new Thread(new ThreadStart(DownloadFiles)); 
    DownloadThread.Start(); 


    private void DownloadFiles() 
    { 
     if (DownloadListView.InvokeRequired) 
     { 
      MyDownloadDeleg = new DownloadDelegate(Download); 
      DownloadListView.Invoke(MyDownloadDeleg); 
     } 
    } 

    private void Download() 
    { 
     foreach (DownloadingFile df in DownloadingFileList) 
     { 
      if (df.Size != "<DIR>") //don't download a directory 
      { 
       ReadBytesThread = new Thread(() => { 
                FileData = sendPassiveFTPcmd("RETR " + df.Path + "/" + df.Name + "\r\n"); 
                FileStream fs = new FileStream(@"C:\Downloads\" + df.Name, FileMode.Append); 
                fs.Write(FileData, 0, FileData.Length); 
                fs.Close(); 
                }); 
       ReadBytesThread.Start(); 
    (here->) ReadBytesThread.Join(); 

       MessageBox.Show("Downloaded"); 
      } 

     } 
    } 

답변

5

을 이 함수는 DownloadListView.Invoke을 통해 UI 스레드에서 Download()을 호출합니다. -> 메인 스레드에서 다운로드가 이루어지기 때문에 애플리케이션이 정지합니다.

당신이이 방법을 시도 할 수 있습니다

:

DownloadThread = new Thread(new ThreadStart(DownloadFiles)); 
DownloadThread.Start(); 

private void DownloadFiles() 
{ 
    foreach (DownloadingFile df in DownloadingFileList) 
    { 
     if (df.Size != "<DIR>") //don't download a directory 
     { 
      ReadBytesThread = new Thread(() => { 
       FileData = sendPassiveFTPcmd("RETR " + df.Path + "/" + df.Name + "\r\n"); 
       FileStream fs = new FileStream(@"C:\Downloads\" + df.Name, 
              FileMode.Append); 
       fs.Write(FileData, 0, FileData.Length); 
       fs.Close(); 
               }); 
      ReadBytesThread.Start(); 
      ReadBytesThread.Join(); 

      if (DownloadListView.InvokeRequired) 
      { 
       DownloadListView.Invoke(new MethodInvoker(delegate(){ 
        MessageBox.Show("Downloaded"); 
       })); 
      } 

     } 
    }   
} 
관련 문제