0

I need to use threading to pause/resume projectile motion. On the button3_Click event the projectile motion is drawn on-screen:

public void button3_Click(object sender, EventArgs e)
{
//... Lots of drawingcode...
}

I need to pause/resume the projectile motion using the same button3_Click but I am new to threading and cannot figure out how.

I have tried:

        public partial class Simulation : Form
{
            Thread parallel1;
            Thread parallel2;

            public Simulation()
            {
                InitializeComponent();
                parallel1 = new Thread(new ThreadStart(button3_Click));
            }
}

But I get an error...

No overload for 'button3_Click' matches delegate 'System.Threading.ThreadStart'

I think that I need two threads so that as one is paused, the other is running to take a button3_Click and resume the other thread. How can this be done?

slugster
  • 47,434
  • 13
  • 92
  • 138
  • 1
    if you have the drawingcode method named like `void Drawing()` and use that in the ThreadStart at least the compile error is gone...don't forget to call Start on parallel1...but I guess your next bump will be the fact that you can't invoke UI related stuff on a non UI thread. – rene Feb 24 '13 at 21:36
  • The answer seems to be that I can't do this because as you said there is only one UI thread. Shame really. – user1920206 Feb 24 '13 at 21:56
  • 1
    @user1920206 well, you can always use `Invoke` to post back to your UI thread. But if you are just trying to animate something, wouldn't you be better of with a `Timer` ? – bas Feb 24 '13 at 22:01
  • 2
    You have to revist your code and see if you can break it up in UI and non-UI stuff (like calculation, fetching data etc). The non-UI stuff can be done on a separate thread. Once it is done and ready once your buttuon is clicked only UI stuff needs to happen. You can also see if the UI stuff can be optimized( do you [double buffer](http://msdn.microsoft.com/en-us/library/b367a457.aspx)?). Then the idea of @bas is also a good suggestion. You break up the drawing in small tasks store those in a list and on every tick of the timer you execute a small task. – rene Feb 24 '13 at 22:29
  • Side note (mainly for future questions): It is not very clear from your question if "need to use threading to ..." is because you have some external requirement to do so OR you just come up with this idea? Saying something like "I want to use threading for..." or "I want to move projectile and I think threading is the answer..." could easily distinguish these cases. – Alexei Levenkov Feb 25 '13 at 00:27

1 Answers1

1

Thats about method signature. In c# methods are unique with their parameters and names. So this error tells you there is no method to call without any parameter. You can use

new Thread( new ParameterizedThreadStart(...

You can send your parameters. Sender is usually your button and you can send "new EventArgs()" for EventArgs parameter.

But the best way(i use it) don't write any complex code in button click. Write your complex code in a different method and call it under button click. Usually operations used more than once so you can call your method from everywhere wthout any change. Write a new method to make your operation. If you need any parameter in your method you can call it with parameterizedThreadStart. And note : if you get any error like this one or more thread access an object you can set this property before your thread start

CheckForIllegalThreadStart=false;

ersin ceylan
  • 107
  • 1
  • 6
  • Ah I see you mean checkforillegalcrossthreadcalls. Do you think that is safe? Why does the error exist if you can just turn it off? Aren't you suspicious of such a solution? (It is deeply unsafe: http://stackoverflow.com/questions/2587930/ramifications-of-checkforillegalcrossthreadcalls-false). Don't do that. – usr Feb 24 '13 at 22:00