Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hey guys I've a program like this (with a background worker) in backgroundWorker1_DoWork. For example I move a button to left and right:

C#
public partial class Form1 : Form
    {
        bool f = false;

        //--------------------------------------------------------------
        public Form1()
        {
            InitializeComponent();
        }

        //--------------------------------------------------------------
        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Y > 40)
            {
                this.BeginInvoke((ThreadStart)delegate()
                {
                    MenuShow();
                });
            }
            else if (e.Y < 100)
            {
                this.BeginInvoke((ThreadStart)delegate()
                {
                    MenuHide();
                });
            }
            //am I used this correctly?:~
            backgroundWorker1.RunWorkerAsync();
        }

        //--------------------------------------------------------------
        private void MenuHide()
        {
            while (menuStrip1.Top != 0)
            {
                menuStrip1.Top++;
                Thread.Sleep(10);
                menuStrip1.Update();
            }
        }

        //--------------------------------------------------------------
        private void MenuShow()
        {
            while (menuStrip1.Top != -25)
            {
                menuStrip1.Top--;
                Thread.Sleep(10);
            }
        }

        public delegate void InvokeDelegate();

        //--------------------------------------------------------------
        private void Invoke_Click(object sender, EventArgs e)
        {
            textBox1.BeginInvoke(new InvokeDelegate(InvokeMethod));
        }

        //--------------------------------------------------------------
        public void InvokeMethod()
        {
          textBox1.Text = "Executed the given delegate";
        }

        //--------------------------------------------------------------
        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {
            f = true;
        }

        //--------------------------------------------------------------
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
           while(!f)  //is this good way to stop the action?:~
           {
               {
                   this.BeginInvoke((ThreadStart)delegate()
                   {
                       //Invoke is a button name
                       while (InvokeButton.Left != 30)
                       {
                           InvokeButton.Left--;
                           Thread.Sleep(10);
                        }
                        while (InvokeButton.Left != 120)
                        {
                            InvokeButton.Left++;
                            Thread.Sleep(10);
                        }
                    });
                }
            }
        }
}


Only the button movement works. The show/hide menu doesn't work. How can I make these works do separately?
Posted
Updated 14-Mar-10 2:54am
v4

1 solution

First, you don't need the variable f - you'll see why in a moment.

Second, your BackgroundWorker object should support cancellation so that it cleans up correctly if the user does something to dismiss the form while the worker is busy.

The while statement should look something like this:

C#
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    BackgroundWorker worker = sender as BackgroundWorker;
    while(!worker.CancellationPending)  
    {
        // In here, invoke a MoveButton method that actually moves the 
        // button in the desired direction
    }
}



Third, ALL UI updates should be performed through delegates (using Invoke). You may need more than one depending on what you want to accomplish.

Lastly, the name "Invoke" is a poor choice for your button control, especially since there is an Invoke() method. Change it to buttonInvoke, or something like that.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900