Click here to Skip to main content
15,907,120 members
Home / Discussions / C#
   

C#

 
QuestionWhere does Settings.Default.MySetting come from? Pin
buchstaben9-Oct-08 22:17
buchstaben9-Oct-08 22:17 
AnswerRe: Where does Settings.Default.MySetting come from? Pin
Simon P Stevens9-Oct-08 22:33
Simon P Stevens9-Oct-08 22:33 
GeneralRe: Where does Settings.Default.MySetting come from? Pin
buchstaben9-Oct-08 23:24
buchstaben9-Oct-08 23:24 
AnswerRe: Where does Settings.Default.MySetting come from? Pin
Mycroft Holmes9-Oct-08 23:22
professionalMycroft Holmes9-Oct-08 23:22 
QuestionRestore application from system tray when clicking the application's desktop shortcut icon in c# Pin
jairaj9-Oct-08 22:06
jairaj9-Oct-08 22:06 
AnswerRe: Restore application from system tray when clicking the application's desktop shortcut icon in c# Pin
DaveyM699-Oct-08 23:16
professionalDaveyM699-Oct-08 23:16 
AnswerRe: Restore application from system tray when clicking the application's desktop shortcut icon in c# Pin
Giorgi Dalakishvili9-Oct-08 23:38
mentorGiorgi Dalakishvili9-Oct-08 23:38 
QuestionPause and continue threads Pin
Mc_Topaz9-Oct-08 20:44
Mc_Topaz9-Oct-08 20:44 
Hello!

I use a program which runs several threads to simulate diffrent devices. Each thread simulate one device.

I have no problemmen staring the threads, but I don't know how to pause them. With pausing, I mean the thread should be stopped but not terminated.
Later in the program I wish to restart/continue the threads.

I have tried to use the suspend() and resume() methods, but I have read that those methods are not good to use for this. I also get irritating warnings when I compile the code, with susped() and resume() methods, which suggest I should use some other solution instead of suspend() and resume() methods.

This is my windows application code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            new Bar1("bar1");
            new Bar2("bar2");
        }

        private void btnExec_Click(object sender, EventArgs e)
        {
            if (!DeviceBase.Run)
            {
                // Starts or continue a thread.
                DeviceBase.execAll();
            }
            else
            {
                // Some code to pause the threads
            }

            DeviceBase.Run = !DeviceBase.Run;
        }
    }

    abstract class DeviceBase
    {
        public static Dictionary<string, DeviceBase> devices = new Dictionary<string, DeviceBase>();

        private string name;
        private Thread execThread;          // The thread to simulate a device.
        protected static bool run = false;

        public DeviceBase(string name)
        {
            this.name = name;
            devices[name] = this;
            devices[name].execThread = new Thread(exec);
        }

        public string Name
        {
            get
            {
                return name;
            }
        }

        public static bool Run
        {
            get
            {
                return run;
            }
            set
            {
                run = value;
            }
        }

        // Start or continue a thread
        public static void execAll()
        {
            foreach (string device in devices.Keys)
            {
                if (devices[device].execThread.ThreadState == ThreadState.Unstarted)
                {
                    // Start all threads.
                    devices[device].execThread.Start();
                }
                else
                {
                    //Continue all paused threads.
                }
            }
        }

        public static void pauseAll()
        {
            // Pause the threads so they are not running in the CPU, but can be restarted/continued
        }

        protected virtual void exec()
        {
        }
    }

    class Bar1 : DeviceBase
    {
        public Bar1(string name) : base(name)
        {
        }

        protected override void exec()
        {
            while (run)
                Console.WriteLine(Name + " is running");
        }
    }

    class Bar2 : DeviceBase
    {
        public Bar2(string name) : base(name)
        {
        }

        protected override void exec()
        {
            while (run)
                Console.WriteLine(Name + " is running");
        }
    }


I have created two diffrent devices (instances) with the classes 'Bar1' and 'Bar2' in the 'Form1' constructor. Each device class derive from the abstract base class 'DeviceBase'' and each 'Bar-instance' overrides the base class method exec(). The 'exec()'-method is the simullating-method which each thread is using/running.
The thread is connected to the derivied 'exec()'-method when the base instance is created.

I use a button: 'btnExec', in the Windows Application form: ´Form1' to start/continue and pause the threads. The static method in the base class: 'execAll()' starts all threads, if the threads are in 'unstated'-state.

To pause the threads, I would like to use the static method: 'pauseAll()' and to continue the threads again I would like to use the 'execAll()'-method again.


How do I solve this?
AnswerRe: Pause and continue threads Pin
Jimmanuel10-Oct-08 3:16
Jimmanuel10-Oct-08 3:16 
GeneralRe: Pause and continue threads Pin
Mc_Topaz10-Oct-08 3:50
Mc_Topaz10-Oct-08 3:50 
GeneralRe: Pause and continue threads Pin
S. Senthil Kumar10-Oct-08 5:22
S. Senthil Kumar10-Oct-08 5:22 
GeneralRe: Pause and continue threads Pin
Jimmanuel10-Oct-08 6:06
Jimmanuel10-Oct-08 6:06 
GeneralRe: Pause and continue threads Pin
Mark Salsbery10-Oct-08 6:13
Mark Salsbery10-Oct-08 6:13 
GeneralRe: Pause and continue threads Pin
Jimmanuel10-Oct-08 6:58
Jimmanuel10-Oct-08 6:58 
QuestionIs there a portable "Add Web Reference" auto-generating code? Pin
Rafferty Uy9-Oct-08 18:53
Rafferty Uy9-Oct-08 18:53 
AnswerRe: Is there a portable "Add Web Reference" auto-generating code? Pin
blackjack21509-Oct-08 20:26
blackjack21509-Oct-08 20:26 
QuestionCrystal Reports - Parameters Fields [modified] Pin
nelsonpaixao9-Oct-08 12:41
nelsonpaixao9-Oct-08 12:41 
QuestionObject containing structures or array Pin
davidbrammer9-Oct-08 11:04
davidbrammer9-Oct-08 11:04 
AnswerRe: Object containing structures or array Pin
Wes Aday9-Oct-08 12:51
professionalWes Aday9-Oct-08 12:51 
AnswerRe: Object containing structures or array Pin
DaveyM699-Oct-08 23:04
professionalDaveyM699-Oct-08 23:04 
AnswerRe: Object containing structures or array Pin
Guffa9-Oct-08 23:45
Guffa9-Oct-08 23:45 
GeneralRe: Object containing structures or array Pin
davidbrammer11-Oct-08 5:11
davidbrammer11-Oct-08 5:11 
QuestionHow to extend SharePoint Components/Features ? Pin
hdv2129-Oct-08 9:47
hdv2129-Oct-08 9:47 
Questionrun a method after getting a reponse it is done Pin
netJP12L9-Oct-08 9:25
netJP12L9-Oct-08 9:25 
AnswerRe: run a method after getting a reponse it is done Pin
Wendelius9-Oct-08 9:33
mentorWendelius9-Oct-08 9:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.