0

Excuse my English; I have a problem with threads: I have a WinForm, in it I have two buttons (Play & Pause) I wish that when you press the PLAY button, if it is created the thread, thought and start, and the PAUSE button to pause the thread. Then if the thread is paused, pressing the PLAY button resumes the thread from where it left off. Like a video game .... In Java I succeed doing something,

Runnable h = new MiHilo();
Thread t = new Thread(h);
public void PLAYActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PLAYActionPerformed
    if (t.isAlive())
    {
        t.resume();
    }
    else
    {
        h = new MiHilo();
        t = new Thread(h);
        t.start();
    }
}
private void PAUSEActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_PAUSEActionPerformed
    if (!t.isAlive())
    {
        JOptionPane.showMessageDialog(null, "         ¿En qué está pensando?\nEs imposible pausar algo que no ha empezado.",
        "David", JOptionPane.ERROR_MESSAGE);
    }
    else
    {
        t.suspend();
    }
}



 class MiHilo implements Runnable
{
        public void run()
        {

                // Code here...

        }

}

How I can do this in C #? What if I would like, if not too much trouble to tell me an example of how to do something basic, Pausing and resuming, something simple, like a single WinForm with a label, and two buttons (Play & Pause) when starting the label is written 0, if hit Play, it starts a thread that calls a function that has a for which varies from 1-1000 with a Sleep of 1 second to display as text on the label, and pause, when pressed would pause this count progressive, then the play would give, but without using Timers ...

Hope it's not too much to ask, with that example I could adapt it to what I want.

Thank you for your time!

4
  • Same methods (Suspend, Resume) are also available in .NET, what did not work?
    – I4V
    Jun 16, 2013 at 21:59
  • These method are Deprecated and dangerous, do you can give me an example?, please
    – candlejack
    Jun 16, 2013 at 22:01
  • I would first post a .Net code showing "I did this" and then ask "how can I replace it with non-deprecated methods" ..
    – I4V
    Jun 16, 2013 at 22:09
  • Thanks, would appreciate postearas these codes, the need to advance
    – candlejack
    Jun 16, 2013 at 22:24

1 Answer 1

1

You should never use suspend/resume, be it in Java or C#, because only your threads know when it's safe to stop or suspend.

Instead use synchronization primitives like events to control the execution of a thread.

Here is a little sample using WinForms (so you just need to add a reference to System.Windows.Forms):

using System.Threading;
using System.Windows.Forms;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Label label = new Label { Dock = DockStyle.Top };
            Button button = new Button { Text = "||", Dock = DockStyle.Bottom };

            Form form = new Form();
            form.Controls.Add(label);
            form.Controls.Add(button);

            bool @continue = true;

            bool isRunning = true;
            ManualResetEvent run = new ManualResetEvent(true);

            int i = 0;
            form.Load += (s, a) =>
                {
                    ThreadPool.QueueUserWorkItem(o =>
                        {
                            while (@continue)
                            {
                                run.WaitOne();
                                label.Invoke((Action)(() => { label.Text = i++.ToString(); }));
                                Thread.Sleep(1000);
                            }
                        });
                };

            button.Click += (s, a) =>
                {
                    isRunning = !isRunning;
                    if (isRunning)
                        run.Set();
                    else
                        run.Reset();
                    button.Text = isRunning ? "||" : ">>";
                };

            form.Disposed += (s, a) => @continue = false;

            Application.Run(form);
        }
    }
}

So the thread that counts (note that this is not accurate at all if you want to count seconds) checks each time if it must continue or wait using "WaitOne".

The UI thread controls this by activating/deactivating the event: when you click on the button the event state is toggled.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.