0

Hello,

I do not know anything about Thread. And I know this question is answered in many forums, but nothing worked for me.
My calculatins are very complex and long and I need at the start of calculations, a progress gif in a picturebox set to visible, and at the end, set to false. My .NET framework is 4.0. I saw a simple examle in the forums but it dose not work.
Any seggestions?

Thank you

namespace Thread_Example
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int count = 0;

            pictureBoxProgress.Invoke(new Action(() => pictureBoxProgress.Visible = true)); //does not work

            // This loop is an example of a very complex calculations.
            // Do not consider its 'count' or 'i'.
            for(int i =0; i < 10000000; i++)
            {
                count += 1;
            }

            pictureBoxProgress.Invoke(new Action(() => pictureBoxProgress.Visible = false));//does not work
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            pictureBox1.Visible = false;
        }
    }
}

Edited by Mohsen_3

3
Contributors
4
Replies
22
Views
2 Weeks
Discussion Span
Last Post by Felix Green
0

It worked, in the simple example above, with adding:

for (int i = 0; i < 10000000; i++)
{
    Application.DoEvents();
    count += 1;
}

But in the main app, no.
In the main app I am using MVP pattern.

private void buttonCalculation_Click(object sender, EventArgs e)
    {
        ...
        pictureBoxProgress.Invoke(new Action(() => pictureBoxProgress.Visible = true));

         Application.DoEvents();//does not work

        this.Cursor = Cursors.WaitCursor;
        presenter.OnCalculationClicked();
        this.Cursor = Cursors.Default;

        pictureBoxProgress.Invoke(new Action(() => pictureBoxProgress.Visible = false));
        ...
    }

I also added it in the main loop (in the presenter.OnCalculationClicked()) of the app. Does not Work.

Edited by Mohsen_3

0

First a comment about line 3 in the first code snippet. I would state it's being called too many times. Also I hope the loop is frivolous and not what you actually intend as you could add 10000000 and be done.

As to the second issue. You inadvertantly called DoEvents thousands of times in code snippet one but in example 2, I found that Windows messages (again, something you need to study when you have time and even then, madness) needed more than one DoEvents with some delay to let the messages process and well, it's a Windows thing. As the coder you work around as best you can. Good hunting.

1

you can as well use the back ground worker control .

 //call the runworker function in the button
 backgroundWorker1.RunWorkerAsycn();

 private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            foreach (DataGridViewRow row in data.Rows)
            {
                this.Invoke((Action)delegate
                {
                  });
            }
        }
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.