mardi 6 mars 2018

Thread Dublicate Run

i ran this code using thread, which add custom-made movies controls to my flow layout panel. but the problem is that the code adds every movie twice so what i did wrong here? (Note that when i ran the code without using thread the movies were loaded correctly which means that the problem is only causes by the thread code) Here is my code, any help is appreciated in advance.

private void AddBox()
{
    Thread thread= new Thread(() => LoadControls());
    thread.Start();
}

private void LoadControls()
{
    int Nu = int.Parse(PageNumber.Text);
    for (int i = (Nu - 1) * 100; i < Nu * 200 && i < MoviesList.Count; i++)
    {
        MovieControl tmp = new MovieControl(MoviesList[i]);
        if (tmp.InvokeRequired || MoviesFlowPanel.InvokeRequired)
        {
            this.Invoke((MethodInvoker)delegate { MoviesFlowPanel.Controls.Add(tmp); }); //the problem seems to be here
        }
        else 
        MoviesFlowPanel.Controls.Add(tmp);
        tmp = null;
    }
}

Aucun commentaire:

Enregistrer un commentaire