user1920206
user1920206

Reputation: 69

Starting and pausing threads?

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?

Upvotes: 0

Views: 112

Answers (1)

ersin ceylan
ersin ceylan

Reputation: 107

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;

Upvotes: 1

Related Questions