[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2011-09-09
3
Medium Priority
?
264 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 45

Assisted Solution

by:AndyAinscow
AndyAinscow earned 375 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 375 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

656 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