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
FPJCBAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.