Measuring time between button 1 click and button 2 click












-1















I need to measure the time between button 1 click and button 2 click. I know that I can use Datetime.Now but any variable I assign, I can only use in one eventhandler. I searched on the internet but all i could find was using a stopwatch, but that doesn't seem to work anymore in Visual Studio 2017.










share|improve this question




















  • 1





    Is this on a webform? Windows Forms application? Other?

    – Jonathan
    Nov 23 '18 at 21:43











  • Assuming button1 and button2 are in the same class, you can set an instance variable when button1 is clicked and check it when button2 is clicked.

    – Jason Armstrong
    Nov 23 '18 at 21:43






  • 2





    "using a stopwatch but that doenst seem to work anymore in visual studio 2017" - works every day for me

    – T.S.
    Nov 23 '18 at 21:50
















-1















I need to measure the time between button 1 click and button 2 click. I know that I can use Datetime.Now but any variable I assign, I can only use in one eventhandler. I searched on the internet but all i could find was using a stopwatch, but that doesn't seem to work anymore in Visual Studio 2017.










share|improve this question




















  • 1





    Is this on a webform? Windows Forms application? Other?

    – Jonathan
    Nov 23 '18 at 21:43











  • Assuming button1 and button2 are in the same class, you can set an instance variable when button1 is clicked and check it when button2 is clicked.

    – Jason Armstrong
    Nov 23 '18 at 21:43






  • 2





    "using a stopwatch but that doenst seem to work anymore in visual studio 2017" - works every day for me

    – T.S.
    Nov 23 '18 at 21:50














-1












-1








-1








I need to measure the time between button 1 click and button 2 click. I know that I can use Datetime.Now but any variable I assign, I can only use in one eventhandler. I searched on the internet but all i could find was using a stopwatch, but that doesn't seem to work anymore in Visual Studio 2017.










share|improve this question
















I need to measure the time between button 1 click and button 2 click. I know that I can use Datetime.Now but any variable I assign, I can only use in one eventhandler. I searched on the internet but all i could find was using a stopwatch, but that doesn't seem to work anymore in Visual Studio 2017.







c# button






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 22:45









Berthur

709212




709212










asked Nov 23 '18 at 21:37









peerisfruitpeerisfruit

61




61








  • 1





    Is this on a webform? Windows Forms application? Other?

    – Jonathan
    Nov 23 '18 at 21:43











  • Assuming button1 and button2 are in the same class, you can set an instance variable when button1 is clicked and check it when button2 is clicked.

    – Jason Armstrong
    Nov 23 '18 at 21:43






  • 2





    "using a stopwatch but that doenst seem to work anymore in visual studio 2017" - works every day for me

    – T.S.
    Nov 23 '18 at 21:50














  • 1





    Is this on a webform? Windows Forms application? Other?

    – Jonathan
    Nov 23 '18 at 21:43











  • Assuming button1 and button2 are in the same class, you can set an instance variable when button1 is clicked and check it when button2 is clicked.

    – Jason Armstrong
    Nov 23 '18 at 21:43






  • 2





    "using a stopwatch but that doenst seem to work anymore in visual studio 2017" - works every day for me

    – T.S.
    Nov 23 '18 at 21:50








1




1





Is this on a webform? Windows Forms application? Other?

– Jonathan
Nov 23 '18 at 21:43





Is this on a webform? Windows Forms application? Other?

– Jonathan
Nov 23 '18 at 21:43













Assuming button1 and button2 are in the same class, you can set an instance variable when button1 is clicked and check it when button2 is clicked.

– Jason Armstrong
Nov 23 '18 at 21:43





Assuming button1 and button2 are in the same class, you can set an instance variable when button1 is clicked and check it when button2 is clicked.

– Jason Armstrong
Nov 23 '18 at 21:43




2




2





"using a stopwatch but that doenst seem to work anymore in visual studio 2017" - works every day for me

– T.S.
Nov 23 '18 at 21:50





"using a stopwatch but that doenst seem to work anymore in visual studio 2017" - works every day for me

– T.S.
Nov 23 '18 at 21:50












2 Answers
2






active

oldest

votes


















2














You can use a Stopwatch instance or DateTime calculation. If you want to use Stopwatch you also have to import the containing namespace.



using System.Diagnostics;


Either way, you have to put your variable in a scope above the eventhandlers so that both eventhandlers have access to it. Here is a Winforms example that uses both methods, but it can easily be translated to other scenarios.



using System;
using System.Diagnostics;
using System.Windows.Forms;

namespace WindowsFormsApp1 {
public partial class Form1 : Form {
private DateTime from;
private Stopwatch watch = new Stopwatch();

public Form1() {
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e) {
from = DateTime.Now;
watch.Restart();
}

private void button2_Click(object sender, EventArgs e) {
watch.Stop();
MessageBox.Show(
"Date subtraction: " + DateTime.Now.Subtract(from).ToString() + Environment.NewLine +
"Stopwatch: " + watch.Elapsed.ToString());

}
}
}





share|improve this answer































    0














    Use Stopwatch



    Stopwatch _sw = new Stopwatch();

    void Button1_Click(sender, e)
    {
    if (_sw.IsRunning)
    _sw.Stop();
    _sw.Reset();
    _sw.Start();
    }


    void Button2_Click(sender, e)
    {
    if (_sw.IsRunning)
    _sw.Stop();
    MessageBox.Show(_sw.Elapsed);
    }


    If you in Web Forms, you can stuff your stop watch into Session. And in REST, well, you can use runtime caching.






    share|improve this answer

























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453188%2fmeasuring-time-between-button-1-click-and-button-2-click%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      You can use a Stopwatch instance or DateTime calculation. If you want to use Stopwatch you also have to import the containing namespace.



      using System.Diagnostics;


      Either way, you have to put your variable in a scope above the eventhandlers so that both eventhandlers have access to it. Here is a Winforms example that uses both methods, but it can easily be translated to other scenarios.



      using System;
      using System.Diagnostics;
      using System.Windows.Forms;

      namespace WindowsFormsApp1 {
      public partial class Form1 : Form {
      private DateTime from;
      private Stopwatch watch = new Stopwatch();

      public Form1() {
      InitializeComponent();
      }

      private void button1_Click(object sender, EventArgs e) {
      from = DateTime.Now;
      watch.Restart();
      }

      private void button2_Click(object sender, EventArgs e) {
      watch.Stop();
      MessageBox.Show(
      "Date subtraction: " + DateTime.Now.Subtract(from).ToString() + Environment.NewLine +
      "Stopwatch: " + watch.Elapsed.ToString());

      }
      }
      }





      share|improve this answer




























        2














        You can use a Stopwatch instance or DateTime calculation. If you want to use Stopwatch you also have to import the containing namespace.



        using System.Diagnostics;


        Either way, you have to put your variable in a scope above the eventhandlers so that both eventhandlers have access to it. Here is a Winforms example that uses both methods, but it can easily be translated to other scenarios.



        using System;
        using System.Diagnostics;
        using System.Windows.Forms;

        namespace WindowsFormsApp1 {
        public partial class Form1 : Form {
        private DateTime from;
        private Stopwatch watch = new Stopwatch();

        public Form1() {
        InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e) {
        from = DateTime.Now;
        watch.Restart();
        }

        private void button2_Click(object sender, EventArgs e) {
        watch.Stop();
        MessageBox.Show(
        "Date subtraction: " + DateTime.Now.Subtract(from).ToString() + Environment.NewLine +
        "Stopwatch: " + watch.Elapsed.ToString());

        }
        }
        }





        share|improve this answer


























          2












          2








          2







          You can use a Stopwatch instance or DateTime calculation. If you want to use Stopwatch you also have to import the containing namespace.



          using System.Diagnostics;


          Either way, you have to put your variable in a scope above the eventhandlers so that both eventhandlers have access to it. Here is a Winforms example that uses both methods, but it can easily be translated to other scenarios.



          using System;
          using System.Diagnostics;
          using System.Windows.Forms;

          namespace WindowsFormsApp1 {
          public partial class Form1 : Form {
          private DateTime from;
          private Stopwatch watch = new Stopwatch();

          public Form1() {
          InitializeComponent();
          }

          private void button1_Click(object sender, EventArgs e) {
          from = DateTime.Now;
          watch.Restart();
          }

          private void button2_Click(object sender, EventArgs e) {
          watch.Stop();
          MessageBox.Show(
          "Date subtraction: " + DateTime.Now.Subtract(from).ToString() + Environment.NewLine +
          "Stopwatch: " + watch.Elapsed.ToString());

          }
          }
          }





          share|improve this answer













          You can use a Stopwatch instance or DateTime calculation. If you want to use Stopwatch you also have to import the containing namespace.



          using System.Diagnostics;


          Either way, you have to put your variable in a scope above the eventhandlers so that both eventhandlers have access to it. Here is a Winforms example that uses both methods, but it can easily be translated to other scenarios.



          using System;
          using System.Diagnostics;
          using System.Windows.Forms;

          namespace WindowsFormsApp1 {
          public partial class Form1 : Form {
          private DateTime from;
          private Stopwatch watch = new Stopwatch();

          public Form1() {
          InitializeComponent();
          }

          private void button1_Click(object sender, EventArgs e) {
          from = DateTime.Now;
          watch.Restart();
          }

          private void button2_Click(object sender, EventArgs e) {
          watch.Stop();
          MessageBox.Show(
          "Date subtraction: " + DateTime.Now.Subtract(from).ToString() + Environment.NewLine +
          "Stopwatch: " + watch.Elapsed.ToString());

          }
          }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 21:57









          E. HoffmannE. Hoffmann

          314




          314

























              0














              Use Stopwatch



              Stopwatch _sw = new Stopwatch();

              void Button1_Click(sender, e)
              {
              if (_sw.IsRunning)
              _sw.Stop();
              _sw.Reset();
              _sw.Start();
              }


              void Button2_Click(sender, e)
              {
              if (_sw.IsRunning)
              _sw.Stop();
              MessageBox.Show(_sw.Elapsed);
              }


              If you in Web Forms, you can stuff your stop watch into Session. And in REST, well, you can use runtime caching.






              share|improve this answer






























                0














                Use Stopwatch



                Stopwatch _sw = new Stopwatch();

                void Button1_Click(sender, e)
                {
                if (_sw.IsRunning)
                _sw.Stop();
                _sw.Reset();
                _sw.Start();
                }


                void Button2_Click(sender, e)
                {
                if (_sw.IsRunning)
                _sw.Stop();
                MessageBox.Show(_sw.Elapsed);
                }


                If you in Web Forms, you can stuff your stop watch into Session. And in REST, well, you can use runtime caching.






                share|improve this answer




























                  0












                  0








                  0







                  Use Stopwatch



                  Stopwatch _sw = new Stopwatch();

                  void Button1_Click(sender, e)
                  {
                  if (_sw.IsRunning)
                  _sw.Stop();
                  _sw.Reset();
                  _sw.Start();
                  }


                  void Button2_Click(sender, e)
                  {
                  if (_sw.IsRunning)
                  _sw.Stop();
                  MessageBox.Show(_sw.Elapsed);
                  }


                  If you in Web Forms, you can stuff your stop watch into Session. And in REST, well, you can use runtime caching.






                  share|improve this answer















                  Use Stopwatch



                  Stopwatch _sw = new Stopwatch();

                  void Button1_Click(sender, e)
                  {
                  if (_sw.IsRunning)
                  _sw.Stop();
                  _sw.Reset();
                  _sw.Start();
                  }


                  void Button2_Click(sender, e)
                  {
                  if (_sw.IsRunning)
                  _sw.Stop();
                  MessageBox.Show(_sw.Elapsed);
                  }


                  If you in Web Forms, you can stuff your stop watch into Session. And in REST, well, you can use runtime caching.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 23 '18 at 21:52

























                  answered Nov 23 '18 at 21:46









                  T.S.T.S.

                  9,933103253




                  9,933103253






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453188%2fmeasuring-time-between-button-1-click-and-button-2-click%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Costa Masnaga

                      Fotorealismo

                      Sidney Franklin