Solved

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

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 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

9 Experts available now in Live!

Get 1:1 Help Now