Solved

How to start timers in the main form from a popup window?

Posted on 2011-09-09
3
254 Views
Last Modified: 2016-02-19
I have a program that runs an automat, and this automat is connected to various devices that customers might rent for some time. The automat is a Linux machine, so I'm using Mono to make my Visual Studio C# project work on it.

I have created a popup panel that maintenance personell etc. can access to turn devices on and off without having to pay for it. So for this problem I think it should suffice to look at two of the windows forms in the project, frmMain.cs and frmPopupPanel.cs.

frmPopupPanel.cs is started from frmMain.cs by some obscure combination of screen touches, so that regular users won't stumble upon it (I have a PIN code there as well). When I start a device from frmPopupPanel.cs, I need to start a timer that should run for as long as the maintenance person has set it to run.

In frmMain.cs I have a method UpdateDeviceStatus() that is executed every second by another timer to see if the various devices should stay turned on or be turned off. The timers I need to start when I turn on a device should thus be something like this:

int TimeToLive = //this will get the value that the maintenance person specifies
Boolean keedDeviceOn = false;

Timer deviceTimer = new Timer();
deviceTimer.interval = 1000;
deviceTimer.Tick += new Eventhandler(deviceTimer_Tick);
deviceTimer.Enabled = True;

void deviceTimer_Tick(Sender object, Eventargs e)
{
      if (TimeToLive == 0)
           keepDeviceOn = false;
      else 
      {
            keepDeviceOn = true;
            TimeToLive--;
      }
}

Open in new window


Now to the problem. As far as I can see, these timers (one for every device) must be created in frmMain.cs, so that UpdateDeviceStatus can check the boolean value keepDeviceOn for every device when it runs. On the other hand, I need to start the timer from frmPopupPanel.cs when the maintenance person clicks a button there.

I am at loss as to how I should do this. What I've read about timers says that they are started when they are created, so how can I make this work in my program?

I tried setting the parent of frmPopupPanel.cs to frmMain.cs, but it crashed when I tried to run that on the Linux machine, so it appears I cannot use the parent property.
0
Comment
Question by:webuser9632
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 125 total points
ID: 36508712
In the popup form have a reference to the main form.  This has a public function that can be called to create the timers.

0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 125 total points
ID: 36510449
"I tried setting the parent of frmPopupPanel.cs to frmMain.cs, but it crashed"

How did you do that?

Pass in "this" to your instance of frmPopupPanel when you Show() it:

    frmPopupPanel popup = new frmPopupPanel();
    popup.Show(this); // <-- set the owner

Now, in "popup", you can cast that back to frmMain via the Owner() property:

    // ... in frmPopupPanel ...
    frmMain main = (frmMain)this.Owner;
    main.Text = "Hello from Popup!";

If that doesn't work then you'll need to manually pass in a reference as Andy proposes.  Let us know if that is the case and you need help setting that up.
0
 

Author Closing Comment

by:webuser9632
ID: 36585306
Sorry for being late guys. I thought I closed this question last week, but I must have forgotten to press some button.

You both more or less gave me the solution. However, the syntax that Idle_Mind suggested caused an error when I ran it in Linux. I had to do it this way:
I let the constructor in frmPopupPanel take a frmMain as parameter, then I wrote this line in frmMain:
frmPopupPanel _Popup = new frmPopupPanel(this);
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question