Sarah
Sarah

Reputation: 3

Moving pictureBox with thread programming

I want to move picturebox with thread programming. Also, I want to get Picturebox's count with a Textbox in my solution, but it's my first try so I have problems with it. Could you please help me?

Thread th;
        public void F_Thread()
        {
            for (int i = 0; i < Convert.ToInt16(textBox1.Text); i++)
            {
                this.pictureBox1.Left = this.pictureBox1.Left - 10;
                Thread.Sleep(100);
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            th = new Thread(F_Thread);
            th.Start();
        }

Upvotes: 0

Views: 228

Answers (1)

TaW
TaW

Reputation: 54433

Here is a cheap, minimal example of using Invoke to change a property of a control from a different thread:

public void F_Thread()
{
    for (int i = 0; i < Convert.ToInt16(textBox1.Text); i++)
    {
        if (pictureBox1.InvokeRequired ) 
            this.Invoke(new UpdatePBInvoker(UpdatePB), -10);

        Thread.Sleep(100);
    }
}

delegate void UpdatePBInvoker(int moveX);

private void UpdatePB(int moveX)
{
    pictureBox1.Left = pictureBox1.Left + moveX;
}

Feel free to add more parameters; just make sure to keep the signatures of the function and the delegate the same:

    delegate void UpdatePBInvoker(Control ctl, int moveX);

    private void UpdatePB(Control ctl, int moveX)
    {
        ctl.Left = ctl.Left + moveX;
    }

Call the 2nd version like this:

.. this.Invoke(new UpdatePBInvoker(UpdatePB), pictureBox1, -10);

Note the the check if ( someControl.InvokeRequired ) is optional and often added to allow for the option of not calling the function from a different thread; for theses cases one usually adds an else branch with the direct call: UpdatePB(...)

Also note that the thread may still run when you close the Form. To prevent errors make sure to abort it, maybe like this:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if ((th!= null && th.IsAlive) th.Abort();
}

If your thread would do more complicated things, especially aquiring locks, it should not be aborted but given a chance to finish and close itself, usually by setting a global flag. But in this case it should work ok. Also see MSDN on this topic..

Upvotes: 2

Related Questions