-1

I get List of websites I need to loop through and to spend on each certain amount of time. Looping needs to be asynchronous, because on each website music will be played, and that's the main point - to hear the music in that amount of time, and then to load another page and to listen to its music and so on. Also, form need to be available for user actions.

Code I've got so far is this:

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


            }
        }
    }
}
Tommz
  • 3,393
  • 7
  • 32
  • 44
  • 1
    I Just need to understand what you would like to do :) You want to Play the music in the same thread but the loading in another thread? According to your TickQuestion: http://stackoverflow.com/questions/13256164/send-a-extra-argument-in-dispatchertimer-tick-event ... – Softwarehuset Jan 12 '14 at 03:08
  • @Softwarehuset Let's say that time is always 5 seconds. So I would like to play music 5 seconds on each website. Music will automatically start when browser navigates to the website, so no worries about that. That means that I would need to somehow loop through websites, navigate and be there for 5 seconds, and then to go to loop to next website, but I can't stop in the loop with Thread.Sleep() since then music wouldn't be heard. – Tommz Jan 12 '14 at 03:16
  • This question is unclear. How are you going to listen to music in parallel? Do you mean that you want to load the web page/music in the background, so there is no delay between songs? If so, that is another problem entirely and one that is not solved using a timer. – theMayer Jan 12 '14 at 23:42

1 Answers1

1

One way to do this is as below.

  • Make websites a class field (if it isn't already), so the timer event handler can access this collection.
  • Add a field to keep track of the current index.
  • Add a field to prevent re-entrant calls to PlaySounds.
  • You're using a WinForms timer, which executes on the same thread as the form, so there's no need for InvokeRequired etc.

Some pseudo-code (warning, this is untested):

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();
}
groverboy
  • 1,133
  • 8
  • 20