2017-09-27 1 views
0

유튜브 다운 로더를 만들면서 WebClient.DownloadFileAsync을 사용하고 있습니다. 문제가 있습니다. C#에서 DownloadFileAsync 이후에 다음 코드를 실행할 수 없습니까?

WebClient client = new WebClient(); 
Process.Text("", "Downloading video data...", "new"); 
client.DownloadFileAsync(new Uri(this.VidLink), this.path + "\\tempVid"); // Line3 
Process.Text("", "Downloading audio data...", "old"); 
client.DownloadFileAsync(new Uri(this.AudLink), this.path + "\\tempAud"); // Line5 

FFMpegConverter merge = new FFMpegConverter(); 
merge.Invoke(String.Format("-i \"{0}\\tempVid\" -i \"{1}\\tempAud\" -c copy \"{2}{3}\"", this.path, this.path, dir, filename)); // Line8 
merge.Stop(); 
Process.Text("", "Video merging complete", "new"); 

Process

내가 사용하고 다른 클래스이며, 그래서 그것에 대해 신경 쓰지, 아주 잘 작동합니다. 하지만 문제가되는 곳은 3 호선을 실행 한 후입니다. 3 호선과 4 호선은 잘 실행되고 5 호선은 실행되지 않습니다. DownloadFileAsync 대신 DownloadFile을 사용하면 코드가 잘 작동하므로 this.AudLink은 문제가되지 않습니다. 또한 Line 5를 삭제하면 Line 5도 잘 작동합니다.

마찬가지로 Line 3을 삭제하면 Line 5가 잘 처리됩니다. Line 8은 실행되지 않습니다. 그렇다면이 코드의 문제점은 무엇입니까? client 등으로 사용되는 프로세스를 죽여야합니까?

++) 비디오 데이터를 다운로드하는 동안 youtube-dl을 사용하지 않으므로 대신 youtube-dl을 사용하지 마십시오.

+1

당신은해야을 await 수'당신의 비동기 호출을 await'ing, 또는 완료시기를 결정하는 다른 방법을 사용하여. – Jamiec

+0

@Jamiec 코드 스 니펫이 불완전하거나 올바르지 않다면 같은 것을 궁금해하고 있습니다 .. 또는 기다릴 수있는 모든 메서드는 .Result를 호출 할 수 있지만 그다지 조언하지는 않습니다. – rmjoia

+0

은 webClient의 두 가지 객체를 기다리거나 시도하거나 다른 작업에서 수행하고 Task1을 사용합니다. 다른 사람과 계속하십시오. – Amit

답변

1

best practices for async programming을 읽기 시작하고 신조 중 하나가 "모두 비동기"임을 유의해야합니다.

코드에 적용된 코드가 포함 된 메소드/클래스는 모두 async이어야합니다. 그 시점에서, 당신은 당신의 비동기 다운로드

private async Task DoMyDownloading() 
{ 
    WebClient client = new WebClient(); 
    Process.Text("", "Downloading video data...", "new"); 
    await client.DownloadFileAsync(new Uri(this.VidLink), this.path + "\\tempVid"); // Line3 
    Process.Text("", "Downloading audio data...", "old"); 
    await client.DownloadFileAsync(new Uri(this.AudLink), this.path + "\\tempAud"); // Line5 

    FFMpegConverter merge = new FFMpegConverter(); 
    merge.Invoke(String.Format("-i \"{0}\\tempVid\" -i \"{1}\\tempAud\" -c copy \"{2}{3}\"", this.path, this.path, dir, filename)); // Line8 
    merge.Stop(); 
    Process.Text("", "Video merging complete", "new"); 
} 
+0

'AsyncCompletedEvent'가 발생했을 때'client' 객체에 사용하고 오디오 데이터를 다운로드하여 문제를 해결했습니다. 그래도 조언 해 주셔서 감사합니다. 내가 읽도록 제안한 문서는 매우 유용했습니다. –

관련 문제