0

I have Visual c# 2010 express installed, I Wanted to call a method that takes parameters using a thread, but during the execution of the method, the thread should stop, and I am supposed to see the impact on the form, and then the method should continue. The operation is simply moving a Label -that is generated from the code on the form_load event- on the form.

I have used three different ways of calling Thread.sleep() method, but each of them is either not suggested by the intellisense or causing an exception -for the last case, which I want to understand. Why am I having this exception and what does it mean?. Is there a better approach to follow?.

  public Thread thr;
    private void Form1_Load(object sender, EventArgs e)
    {
        Label os = (Label)this.Controls[0];
        os.Text = "codeLB";
        thr = new Thread(() => Beta(os,thr));
        thr.Start();
    }
    public void Beta(Label os,Thread tr)
    {

1.    os.Location = new Point(os.Location.X + 10, os.Location.Y + 10);


        while(os.Location.Y<=400)
        {
            this.Refresh();
            //here I want to sleep the thread.


            //tr.sleep(1000);   doesn't work (not suggested)
            //Thread.CurrentThread.sleep(1000);  doesn't work (not suggested)
            Thread.Sleep(1000);  // it works but it causes an unhandled InvalidOperationException in line 1 
"Cross-thread operation not valid: Control '' accessed from a thread other than the thread it was created on."

            os.Location = new Point(os.Location.X + 10, os.Location.Y + 10);
        }
        os.Location = new Point(0, 0);
    }

Many thanks.

TiyebM
  • 2,684
  • 3
  • 40
  • 66
  • 3
    I think you need to do some invoking. Try checking this out http://msdn.microsoft.com/en-us/library/ms171728(v=vs.85).aspx – Pavenhimself Sep 18 '14 at 11:50
  • 1
    Your question is about how to access UI in worker thread, which you can find answer [here](http://stackoverflow.com/questions/661561/how-to-update-the-gui-from-another-thread-in-c). That said, What you need is `System.Windows.Forms.Timer` not thread. – Sriram Sakthivel Sep 18 '14 at 11:58
  • @Pavenhimself , Sorry but Thread.Sleep(1000) caused the exception I described!!, obviously I am running "tr" thread not another thread in the system, but I couldn't access that.. – TiyebM Sep 18 '14 at 12:05
  • @SriramSakthivel the solution there is a bit complicated and specified. – TiyebM Sep 18 '14 at 12:18
  • Which .NET version are you using? – Patrice Gahide Sep 18 '14 at 12:34
  • 1
    @Tiyeb3W IMO, you don't need a Thread. You just need a timer(System.Windows.Forms.Timer). If you want to simply move a control there will be so many posts describing that. For instance refer [this](http://stackoverflow.com/questions/15548615/moving-picture-box-with-timer-faster) or my article have an [example](http://www.codeproject.com/Articles/737164/Enumerate-Collection-with-Delay) at bottom of the page. Whichever is useful make use of it. But remember don't mix `Thread` and UI. You don't need a thread here. thanks – Sriram Sakthivel Sep 18 '14 at 12:35
  • Also here is another [example](http://stackoverflow.com/questions/4833864/move-images-in-c-sharp) – Sriram Sakthivel Sep 18 '14 at 12:39
  • To quote Stephen Cleary in his concurrency cookbook: "If you type `new Thread()` .. you already have legacy code". You should really look up the newer asynchronous functionality in .NET. They make it much easier to do what you're doing. – Simon Whitehead Sep 18 '14 at 12:53

0 Answers0