2014-01-12 3 views
-1

나는 루프를 돌면서 각 일정한 시간을 소비하는 데 필요한 웹 사이트를 List 개나 얻을 수 있습니다. 루핑은 비동기 적이어야합니다. 왜냐하면 각 웹 사이트 음악이 재생되기 때문입니다. 그게 핵심 포인트입니다. 그 시간 동안 음악을 듣고 다른 페이지를로드하고 음악을 듣는 등의 일입니다. 또한 양식을 사용자 작업에 사용할 수 있어야합니다. 이것은 다음과 같습니다 할비동기 적으로 루프하는 방법은 무엇입니까?

public void playSound(List<String> websites) 
{ 
    webBrowser.Navigate(Uri.EscapeDataString(websites[0])); 

    foreach (String website in websites.Skip(1)) 
    { 
     StartAsyncTimedWork(website); 
     // problem when calling more times 
    } 

} 

private System.Windows.Forms.Timer myTimer = new System.Windows.Forms.Timer(); 

private void StartAsyncTimedWork(String website) 
{ 
    myTimer.Interval = 7000; 
    myTimer.Tick += new EventHandler(myTimer_Tick); 
    myTimer.Start(); 
} 

private void myTimer_Tick(object sender, EventArgs e) 
{ 
    if (this.InvokeRequired) 
    { 
     this.BeginInvoke(new EventHandler(myTimer_Tick), sender, e); 
    } 
    else 
    { 
     lock (myTimer) 
     { 
      if (this.myTimer.Enabled) 
      { 

       this.myTimer.Stop(); 
       // here I should get my website which I need to search 
       // don't know how to pass that argument from StartAsyncTimedWork 


      } 
     } 
    } 
} 
+1

나는 당신이 무엇을하고 싶은지를 이해해야한다. 동일한 스레드에서 음악을 재생하고 싶지만 다른 스레드에서 로딩하고 싶습니까? 귀하의 TickQuestion에 따르면 : http://stackoverflow.com/questions/13256164/send-a-extra-argument-in-dispatchertimer-tick-event ... – Softwarehuset

+0

@Softwarehuset 시간이 항상 5 초라고 가정 해 봅시다. 그래서 각 웹 사이트에서 5 초씩 음악을 듣고 싶습니다. 브라우저가 웹 사이트를 탐색 할 때 음악이 자동으로 시작되므로 걱정할 필요가 없습니다. 즉, 어떻게 든 웹 사이트를 반복하고, 탐색하고, 5 초 동안 거기에 있어야하고, 다음 웹 사이트로 이동해야한다는 것을 의미합니다. 그러나 Thread.Sleep() 이후 루프에서 멈출 수 없습니다. 들려야한다. – Tommz

+0

이 질문은 분명하지 않습니다. 어떻게 평행으로 음악을 듣습니까? 백그라운드에서 웹 페이지/음악을로드하고 싶으므로 노래 사이에 지연이 없습니까? 그렇다면 그것은 완전히 다른 문제이며 타이머를 사용하여 해결되지 않은 문제입니다. – theMayer

답변

1

한 가지 방법 : 내가 지금까지있어

코드는 이것이다.

  • websites (이미없는 경우) websites을 클래스 필드로 지정하면 타이머 이벤트 핸들러에서이 컬렉션에 액세스 할 수 있습니다.
  • 현재 색인을 추적하는 필드를 추가하십시오.
  • 재진입 호출을 방지하기 위해 필드를 추가하여 PlaySounds.
  • 당신은 형태와 동일한 스레드에서 실행하는 윈폼 타이머를 사용하고, 그래서 InvokeRequired

일부 의사 코드가 필요 없다 (경고,이 안된) :

private bool isPlayingSounds; 
private int index; 
private List<String> websites; 
private Timer myTimer; 

private void Form1_Load() 
{ 
    myTimer = new System.Windows.Forms.Timer(); 
    myTimer.Interval = 7000; 
    myTimer.Tick += new EventHandler(myTimer_Tick); 
} 

public void PlaySounds(List<String> websites) 
{ 
    if (isPlayingSounds) 
    { 
     // Already playing. 
     // Throw exception here, or stop and play new website collection. 
    } 
    else 
    { 
     isPlayingSounds = true; 
     this.websites = websites; 
     PlayNextSound(); 
    } 
} 

private void PlayNextSound() 
{ 
    if (index < websites.Count) 
    { 
     webBrowser.Navigate(Uri.EscapeDataString(websites[index])); 
     myTimer.Start(); 

     // Prepare for next website, if any. 
     index++; 
    } 
    else 
    { 
     // Remove reference to object supplied by caller 
     websites = null; 

     /Reset index for next call to PlaySounds. 
     index = 0; 

     // Reset flag to indicate not playing. 
     isPlayingSounds = false; 
    } 
} 

private void myTimer_Tick(object sender, EventArgs e) 
{ 
    myTimer.Stop(); 
    PlayNextSound(); 
} 
관련 문제