[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 192
  • Last Modified:

Make button available when application is sleeping

I have a C# program that runs continuously.  Since it runs so fast I often want to make it go to sleep for a minute or two before querying my database again I simply use this line of code System.Threading.Thread.Sleep(60000); to make it go to sleep for a minute and will probably change it to sleep longer once it's in production.

My problem is I have another button I wish to have available to exit the program during these sleep cycles, but the button itself, the entire application is asleep and I can't click on the button until the application wakes up again.

Maybe I'm doing it all wrong.  Can someone help?

  Thanks
0
FPJCB
Asked:
FPJCB
2 Solutions
 
dsaboCommented:
Run everything in other thread, and the form with the button in the main thread....

it should work...
0
 
FPJCBAuthor Commented:
How do I start a thread?  Sleep is the only reason I ever use threads to begins with.  Can you give me a little coding example that creates and manages threads?  All I'm looking to do is manage this one button, making sure it is awake when the other thread is asleep.

  Thanks again
0
 
dsaboCommented:
OK, here it goes.  creating a new thread


Thread newthread = new Thread(new ThreadStart(ThreadMethod));
newthread .Start()  //starts the thread.
try
{
   newthread.Abort() //kill the thread
}
catch{}

public void ThreadMethod()
{
   //The code that you want to run in a new thread HERE....
}

hope this help's
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
AlexFMCommented:
Use timer.
0
 
skpatraCommented:
I am sending a sample code that does the followings:
1. On load of the form, it starts a thread in the background which starts printing time to a textbox in every 2 seconds. This keeps on running infinitely.
2. There is a button which when clicked, kills the background thread and the time is not calculated any more. Hope this helps...


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

namespace MyRunner
{
      /// <summary>
      /// Summary description for Form1.
      /// </summary>
      public class Form1 : System.Windows.Forms.Form
      {
            private System.Windows.Forms.Button button1;
            private System.Windows.Forms.TextBox textBox1;
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.Container components = null;

            public Form1()
            {
                  //
                  // Required for Windows Form Designer support
                  //
                  InitializeComponent();

                  //
                  // TODO: Add any constructor code after InitializeComponent call
                  //
            }

            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            protected override void Dispose( bool disposing )
            {
                  if( disposing )
                  {
                        if (components != null)
                        {
                              components.Dispose();
                        }
                  }
                  base.Dispose( disposing );
            }

            #region Windows Form Designer generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                  this.button1 = new System.Windows.Forms.Button();
                  this.textBox1 = new System.Windows.Forms.TextBox();
                  this.SuspendLayout();
                  //
                  // button1
                  //
                  this.button1.Location = new System.Drawing.Point(208, 168);
                  this.button1.Name = "button1";
                  this.button1.TabIndex = 0;
                  this.button1.Text = "button1";
                  this.button1.Click += new System.EventHandler(this.button1_Click);
                  //
                  // textBox1
                  //
                  this.textBox1.Location = new System.Drawing.Point(88, 32);
                  this.textBox1.Name = "textBox1";
                  this.textBox1.Size = new System.Drawing.Size(192, 20);
                  this.textBox1.TabIndex = 1;
                  this.textBox1.Text = "";
                  //
                  // Form1
                  //
                  this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                  this.ClientSize = new System.Drawing.Size(292, 273);
                  this.Controls.Add(this.textBox1);
                  this.Controls.Add(this.button1);
                  this.Name = "Form1";
                  this.Text = "Form1";
                  this.Load += new System.EventHandler(this.Form1_Load);
                  this.ResumeLayout(false);

            }
            #endregion

            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                  Application.Run(new Form1());
            }

            private Thread _oThread = null;

            private void Form1_Load(object sender, System.EventArgs e)
            {
                  //need to start a thread
            ThreadStart oStart = new ThreadStart(StartRunning);
                  _oThread = new Thread(oStart);
                  _oThread.Start();
            }

            private void StartRunning()
            {
                  while(true)
                  {
                        Thread.Sleep(2000);
                        textBox1.Text = DateTime.Now.ToLongTimeString();
                  }
            }

            private void button1_Click(object sender, System.EventArgs e)
            {
                  _oThread.Abort();
                  MessageBox.Show("Thread Closed");
            }
      }
}
0
 
dsaboCommented:
Hi...

In orde to avoid the exception that it's thorw by the method Abort, you need to wirte a try...catch

by default, Abort always throw an exception.
try
{
     _oThread.Abort();
}
catch{}

In order to know the status of the thread you could use the property ThreadState, more info about ThreadState http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemthreadingthreadstateclasstopic.asp 

Also, you should set the property isBackGroud = true, so in this mode, when the main thread is aborted or dead, the backgroud threads also is aborted. More info on isBackgroud, http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemthreadingthreadclassisbackgroundtopic.asp 

Hope this Helps
0
 
FPJCBAuthor Commented:
I'm not sure what happened or who got the points.  I increased the value of the question and tried to split it and received an errror that the points could not be split.

Thank you both for your answers.

  Jeff
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now