Solved

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

Posted on 2011-09-09
3
243 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
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 85

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ConsoleSql 1 34
Need to pass stored proc parameters in list 13 32
free scanner TWAIN can be operated with a Web application 9 39
Code works but it's slow 24 40
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 This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

910 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now