Solved

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

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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

832 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