Solved

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

Posted on 2008-10-23
4
1,021 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

911 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

15 Experts available now in Live!

Get 1:1 Help Now