-4

i have some value in text box . i want to calculate the time of that value in text box .means for how long that value is there in the text box.

values are of Boolean type . it can be 1 or 0 .i want to calculate the time span of each value also their differences .

  • Good that you want to do that. What have you done so far? – Mong Zhu Aug 02 '16 at 13:29
  • nothing bro thats why i am here .thats the last thing remaning .task given to me – umar abbasi Aug 02 '16 at 13:37
  • people tend to non-reactions and downvotes when a question is posted without any visible own efforts to tackle the issue and when questions are phrased with "I want" or "I need". – Mong Zhu Aug 02 '16 at 13:41
  • i need to calculate the duration for how long value was 1 or 0 ..kindly help me – umar abbasi Aug 02 '16 at 13:41
  • how does this value get into your textbox? do you know the time of it's appearance? – Mong Zhu Aug 02 '16 at 13:43
  • i can post my code here but that is out of the context. i have one text box .. using onditions to check if condition is true value will be 1 if not 0 . i just want to know for how long it was 0 or 1 . duration – umar abbasi Aug 02 '16 at 13:43
  • try // { // var query = "select 1"; // var command = new SqlCommand(query, connection); // connection.Open(); // / // command.ExecuteScalar(); // ** sqlserver_status.Text = "Active";** // rectangle.Fill = new SolidColorBrush(Color.FromRgb(2, 245, 156)); // – umar abbasi Aug 02 '16 at 13:47
  • here i want to check the duration of sqlserver_status.Text="Active".. for how long it was active and for how long it values changes to something else – umar abbasi Aug 02 '16 at 13:48
  • thats my first time thats why i can not properly convey my message . – umar abbasi Aug 02 '16 at 13:50
  • no problem. I had also my first post here :) there is an [edit](http://stackoverflow.com/posts/38721535/edit) button below your post, you can use it and add the code to your existing post. – Mong Zhu Aug 02 '16 at 13:56

1 Answers1

0

It is not much code that you posted, but I would give it a try. I don't see in your code any bool variable. You probably should have one where you would save the current state.

since you write the value into the TextBox you could start the timer that MMbach suggested right after the line:

sqlserver_status.Text = "Active";
// start timer here

whenever further in you code this status changes, you would stop the timer and check the elapsed time.

You could also use the StopWatch class for that. It has a property called Elapsed which :

Gets the total elapsed time measured by the current instance.

If you need to run it in the background I would advise to go for the Timer.

Below is a small Demo realised with a System.Diagnostics.Stopwatch. There are many more realisation to this problem. It always depends on the structure of your program which realisation is better or worse.

This is a small Console application where you decide when to change the State Variable. It will monitor your decision process.

public class TimeDemo
{
    // Property to catch the timespan
    public TimeSpan TimeOfState { get; set; }

    // Full Property for the state
    private bool state;

    public bool State
    {
        get { return state; }
        set
        {
            // whenever a new state value is set start measuring
            state = value;
            this.TimeOfState = StopTime();
        }
    }
    // Use this to stop the time
    public System.Diagnostics.Stopwatch StopWatch { get; set; }

    public TimeDemo()
    {
        this.StopWatch = new System.Diagnostics.Stopwatch();
    }
    //Method to measure the elapsed time
    public TimeSpan StopTime()
    {
        TimeSpan t = new TimeSpan(0, 0, 0);

        if (this.StopWatch.IsRunning)
        {
            this.StopWatch.Stop();
            t = this.StopWatch.Elapsed;
            this.StopWatch.Restart();
            return t;
        }
        else
        {
            this.StopWatch.Start();
            return t;
        }
    }

    public void Demo()
    {
        Console.WriteLine("Please press Enter whenever you want..");
        Console.ReadKey();
        this.State = !this.State;

        Console.WriteLine("Elapsed Time: " + TimeOfState.ToString());


        Console.WriteLine("Please press Enter whenever you want..");
        Console.ReadKey();
        this.State = !this.State;

        Console.WriteLine("Elapsed Time: " + TimeOfState.ToString());



        Console.WriteLine("Please press Enter whenever you want..");
        Console.ReadKey();
        this.State = !this.State;

        Console.WriteLine("Elapsed Time: " + TimeOfState.ToString());


    }
}

May be you can adjust it according to your case.

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76