• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1046
  • Last Modified:

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

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
OrenRozen
Asked:
OrenRozen
  • 2
  • 2
1 Solution
 
jfmadorCommented:
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
 
OrenRozenAuthor Commented:
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
 
OrenRozenAuthor Commented:
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
 
jfmadorCommented:
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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now