473,387 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

C# Form Timer and Form Visibility vs. Threading and .Set

30
I have a C# program in 2003 that creates a reminder bubble (windows form) to remind a user to pick up their printout. The bubbles show up fine, and they have 2 buttons, a yes and a snooze. The yes resynchs the thread. The snooze puts the bubble to sleep for 5 minutes.

Originally my event handler code for the snooze button pointed to the following method:

Expand|Select|Wrap|Line Numbers
  1.         public void SnoozeForm(int minutes)
  2.         {
  3.             //VAR
  4.             TimeSpan waitLength = DateTime.Now.AddMinutes(minutes) - DateTime.Now;
  5.  
  6.             //BEGIN
  7.  
  8.             //Hide the form
  9.             this.Hide();
  10.  
  11.             //Tell threader this bubble is hidden again
  12.             this.myThreader.BubblesVisible--;
  13.  
  14.             //Turn on Context Menu if no more visible bubbles
  15.             if(this.myThreader.BubblesVisible == 0)
  16.             {
  17.                 this.myThreader.frmEOD.FlipContextMenu(true);
  18.             }//end if
  19.  
  20.             //Suspend Thread for endTime
  21.             this.ThreadController.WaitOne(waitLength, false);
  22.  
  23.             //Check to see if we were awoken because of end of day
  24.             if(!this.myThreader.EndOfDay)
  25.             {
  26.                 //Turn off context menu
  27.                 this.myThreader.frmEOD.FlipContextMenu(false);
  28.  
  29.                 //Tell threader we are visible
  30.                 this.myThreader.BubblesVisible++;
  31.  
  32.                 //Once time has expired unhide the form
  33.                 if(!this.Visible)
  34.                 {
  35.                     this.Show();
  36.                 }//end if
  37.             }
  38.             else if(this.myThreader.AboutOn)
  39.             {
  40.                 SnoozeForm(5);
  41.             }
  42.             else
  43.             {
  44.                 //Resynch Thread
  45.                 ResynchThread(true);
  46.             }
  47.  
  48.         }//SnoozeForm
This worked fine, except that if a bubble was snoozed and the user tried to shut down the computer, the shutdown would abort trying to close my program, and wouldn't allow a logoff/out until the bubble was resynched at which time the shutdown happened as expected. I assume the reason is the AutoEventReset object (ThreadController) and the wait command not releasing. the closing event doesn't fire during the windows shutdown process so I could not release the thread wait that way.

So I decided I would try a timer on the form, which would make sure the bubble had no issues being closed during shutdown, except that it seems the form must be visible when using the timer, as when I modified the code to use the timer, the timer would be enabled and the form would be invisible, but once the end of the method hit, the form would pop up visible again.

So any idea which method is better for this situation, and if so what am I doing wrong to get these bubbles to snooze and hide properly, and allow windows to shut them down? Thanks a bunch.
Nov 20 '07 #1
6 4293
Plater
7,872 Expert 4TB
You are using a regular thread? (System.Threading.Thread?)
Call myThread.Abort(); to tell it to end.
(It actually raises the ThreadAbortingException on the thread, so if you have try/catch blocks you will need to adjust them accordingly)
Nov 20 '07 #2
ewokspy
30
The question is where do I call that? I have tried logging what events were called in my program during shutdown but I cannot find a place to put that code. The close methods and dispose methods don't get called when system is shutting down it seems. The for is hidden so I can't active montior the system messsages. Is there a specific place in the code I can put this where it will run as the first thing the program does when trying to shut down?
Nov 20 '07 #3
Plater
7,872 Expert 4TB
The form_closing even is called when system shutsdown.
You can check for it too in the event args they give you.
Nov 20 '07 #4
ewokspy
30
The form_closing even is called when system shutsdown.
You can check for it too in the event args they give you.
I was sure that was only for .NET 2.0, I'm using studio 2003, so it's only 1.1 for me.

Regardless I will rephrase and say I don't see the code in my closing events being executed upon shutdown. I put a method to create a txt file and log the event but it never executed until after the bubble was no longer waiting.
Nov 20 '07 #5
Plater
7,872 Expert 4TB
I would guess that the bubble blocks other code from happening?
Nov 20 '07 #6
ewokspy
30
I would guess that the bubble blocks other code from happening?
It doesn't seem to stop the code for the rest of the program. Just the thread that it's working with. I use the Main thread to force the bubble thread to become active when they use a specific function, and I would love to resynch the thread upon windows shutdown, but I have no forms that are active to sniff the Wndproc for shutdown messages, since I noticed that winproc won't return anything if the form isn't active.
Nov 20 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: andrewcw | last post by:
I am trying to do a windows service with C#. I am using as a base the VB.NET article in VS, but I thing the WITHEVENTS timer notation is a delegate. Can anyone provide sample code & anh hints. ...
3
by: mjheitland | last post by:
Hi, I like to know how many threads are used by a Threading.Timer object. When I create a Threading.Timer object calling a short running method every 5 seconds I expected to have one additional...
4
by: Martin Hill | last post by:
Hi there, How can I find the components added to web form from my custom web control? Suppose I added a Timer component (with name "timer1") onto the web form, from my user web control, how...
8
by: Stephen Rice | last post by:
Hi, I have a periodic problem which I am having a real time trying to sort. Background: An MDI VB app with a DB on SQL 2000. I have wrapped all the DB access into an object which spawns a...
2
by: Sid Price | last post by:
Hello, I am having a problem with a windows form when controlling its visibility. My application has a form that displays a timing function. Normally the timing form is hidden. An object has a...
8
by: =?Utf-8?B?RGF2ZSBCb29rZXI=?= | last post by:
I have a Timer that I set to go off once a day, but it frequently fails! In order to debug I would like to be able to check, at any moment, whether the Timer is enabled and when it will next...
1
by: Michael Howes | last post by:
I and trying to detect idle time or sorts in the applications I'm building. To do this the main Form also inherits from IMessageFilter and I listen for WM_MOUSEMOVE. When that happens I reset a...
2
by: Just_a_fan | last post by:
Situation: Timer1 on main form needs to be stopped during a thread action. Programming in second thread: timer1.enabled = false Action:
3
by: Sourcerer | last post by:
Hello, I'm trying to make a simple scheduler application for Windows in MS VS 2005, in C#. I'm using System.Threading.Timer class which is supposed to pop-up a window when it's time for one...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.