?
Solved

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

Posted on 2008-10-23
4
Medium Priority
?
1,035 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
[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
  • 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 1000 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

752 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