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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ewokspy
    New Member
    • Sep 2007
    • 30

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

    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:

    Code:
    		public void SnoozeForm(int minutes)
    		{
    			//VAR
    			TimeSpan waitLength = DateTime.Now.AddMinutes(minutes) - DateTime.Now;
    			
    			//BEGIN
    			
    			//Hide the form
    			this.Hide();
    
    			//Tell threader this bubble is hidden again
    			this.myThreader.BubblesVisible--;
    
    			//Turn on Context Menu if no more visible bubbles
    			if(this.myThreader.BubblesVisible == 0)
    			{
    				this.myThreader.frmEOD.FlipContextMenu(true);
    			}//end if
    
    			//Suspend Thread for endTime
    			this.ThreadController.WaitOne(waitLength, false);
    			
    			//Check to see if we were awoken because of end of day
    			if(!this.myThreader.EndOfDay)
    			{
    				//Turn off context menu
    				this.myThreader.frmEOD.FlipContextMenu(false);
    
    				//Tell threader we are visible
    				this.myThreader.BubblesVisible++;
    
    				//Once time has expired unhide the form
    				if(!this.Visible)
    				{
    					this.Show();
    				}//end if
    			}
    			else if(this.myThreader.AboutOn)
    			{
    				SnoozeForm(5);
    			}
    			else
    			{
    				//Resynch Thread
    				ResynchThread(true);
    			}
    
    		}//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 (ThreadControll er) 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.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    You are using a regular thread? (System.Threadi ng.Thread?)
    Call myThread.Abort( ); to tell it to end.
    (It actually raises the ThreadAbortingE xception on the thread, so if you have try/catch blocks you will need to adjust them accordingly)

    Comment

    • ewokspy
      New Member
      • Sep 2007
      • 30

      #3
      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?

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        The form_closing even is called when system shutsdown.
        You can check for it too in the event args they give you.

        Comment

        • ewokspy
          New Member
          • Sep 2007
          • 30

          #5
          Originally posted by Plater
          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.

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            I would guess that the bubble blocks other code from happening?

            Comment

            • ewokspy
              New Member
              • Sep 2007
              • 30

              #7
              Originally posted by Plater
              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.

              Comment

              Working...