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
Solved

C# : pass stopwatch counter data running on form1 to form2

Posted on 2008-10-23
4
1,024 Views
Last Modified: 2013-12-17
Hi,

In form1 I'm using stopwatch method to calculate and display results in textboxs.

is it possible to use the stopwatch in form1 to make calculations in form2?

Thanks,

0
Comment
Question by:OrenRozen
  • 2
  • 2
4 Comments
 
LVL 5

Expert Comment

by:jfmador
ID: 22792621
Is your stepwatch method use properties from your form 1?

if not, it is a good practice to separate UI (User interface) from your calculation method, this way you can use it from any form, any class, web services, ...

Create a new class and add your method in this class, if this method is simple and doesn't have relation with multiple properties that you can add to this class you can declare it as static method. or else I will suggest you to create a complete class with properties and method and add calculation method in it

Static method
public class Calculate
{
    public static int add(int x, int y)
    {
        return x * y;
    }
}

How to call it from your form simply
int total = Calculate.Add(2,3);

if it is a more complex example

public class Rectangle
{
    private double m_Length;
    private double m_Width:
    public double length
    {
        get
        {
            return m_Length;
        }
        set
        {
            m_Length = value;
        }
    }

    public double width
    {
        get
        {
            return m_Width;
        }
        set
        {
            m_Width = value;
        }
    }

    public double area()
    {
        return m_Length * m_Width;
    }
    public double perimeter()
    {
       return m_Length * 2 + m_Width * 2;
    }
}


To call it from your form simply
Rectange r = new Rectangle();
r.width = 20;
r.length = 30;
double area = r.Area();
double perimeter  = r.Perimeter()

// also note this way you don't have to provide your properties multiple times only once for the object
0
 
LVL 3

Author Comment

by:OrenRozen
ID: 22794220
appreciate your answer,

I'd like to explain what I'm trying to do in more details:

I need to perform a specific task on several running processes at different time intervals. for example:
prc1 every 15 seconds
prc2 every 25 minutes
prc3 every 2 hours
and so on.

some times i need to change the interval on some or all processes.
also, i need to display all processes on screen and show the countdown for every process.

every thing is working great as long i'm running the timer, the stopwatch, calculating the countdown,
and display everything on the same form.

my problem is that this form is getting bigger and loaded with data and i want to split the information to display in two forms.
in form1 I'd like to run the countdown and perform tasks on the processes.
in form2 I'd like to display the processes and the countdown for every process as performed in form1.

Thanks,
0
 
LVL 3

Author Comment

by:OrenRozen
ID: 22794833
I've found a solution. don't know if it's the best solution but this is what i did:

I've added a timer and stopwatch to form2.
then I passed the current stopwatch time in form1 to a string in form2
in form2 I've started the stopwatch and calculated the time by adding the data from passed string.

If there is a better solution or more elegant one, please inform.

Thanks
//FORM1
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Timer MyTimer = new Timer();
        Stopwatch MySw = new Stopwatch();
 
        public int TimeNow = 0;
 
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            //start timer
            MyTimer.Interval = 100;
            MyTimer.Enabled = true;
            MyTimer.Start();
            MyTimer.Tick += new EventHandler(timer1_Tick);
 
            MySw.Start();
        }
 
        private void timer1_Tick(object sender, EventArgs e)
        {
            TimeNow = int.Parse(MySw.Elapsed.TotalSeconds.ToString("0"));
 
            textBox1.Text = TimeNow.ToString();
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 StartForm2 = new Form2(TimeNow.ToString());
            StartForm2.Show();
            StartForm2 = null;
        }
 
    }
}
 
//FORM2
namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        Timer MyTimer2 = new Timer();
        Stopwatch MySw = new Stopwatch();
 
        int Form1CountSeconds = 0;
        int CountSeconds = 0;
 
        public Form2()
        {
            InitializeComponent();
        }
 
        private void Form2_Load(object sender, EventArgs e)
        {
            //StartPosition timer
            MyTimer2.Interval = 100;
            MyTimer2.Enabled = true;
            MyTimer2.Start();
            MyTimer2.Tick += new EventHandler(timer1_Tick);
            MySw.Start();
 
        }
 
        public Form2(string time)
        {
            InitializeComponent();
            Form1CountSeconds = int.Parse(time);
        }
 
        private void timer1_Tick(object sender, EventArgs e)
        {
            CountSeconds = Form1CountSeconds + int.Parse(MySw.Elapsed.TotalSeconds.ToString("0"));
            textBox1.Text = CountSeconds.ToString();
        
        }
    }
}

Open in new window

0
 
LVL 5

Accepted Solution

by:
jfmador earned 250 total points
ID: 22795321
A more elegant way to do what you explain is definitively using threading,

Here is a first exemple about threading that you can read. Using threading you can separate your treatement in a different class and use delegate, that will separate your process from your UI. And your application will run without having the form hanging during the calculation.

http://www.codeproject.com/KB/cs/workerthread.aspx
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

839 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