Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
465 views
in Technique[技术] by (71.8m points)

uwp - How to create 300 stopwatches in C# more efficiently?

I'm creating an UWP application that needs to measure the time the user is looking at an object. This application has around 300 objects to measure the time of. To do this we will be using around 300 timers. To achieve this we will have to create 300 stopwatches individually which is highly inefficient.

The timer starts when the user is looking at the corresponding object and stops when the user is no longer looking at the corresponding object. If the user's gaze is fixated on the corresponding object again the timer of course starts again. At the end all of the times of the stopwatches will be saved into a file. Creating 300 stopwatches requires a new line of code for every stopwatch which does not seem very efficient. I have tried to automate the stopwatch creation process by using Enumerable.range but so far i have not been able to find a solution.

    /// <summary>
    /// All stopwatches for every word. In our real code we will have around 300 stopwatches.
    /// </summary>
    Stopwatch Stopwatch1 = new Stopwatch();
    Stopwatch Stopwatch2 = new Stopwatch();
    Stopwatch Stopwatch3 = new Stopwatch();
    Stopwatch Stopwatch4 = new Stopwatch();
    Stopwatch Stopwatch5 = new Stopwatch();
    Stopwatch Stopwatch6 = new Stopwatch();
    Stopwatch Stopwatch7 = new Stopwatch();
    Stopwatch Stopwatch8 = new Stopwatch();
    Stopwatch Stopwatch9 = new Stopwatch();
    Stopwatch Stopwatch10 = new Stopwatch();
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

So, at first you need to get a list of the available objects. You can use the following code to create a generic dictionary that keeps a stopwatch for every object you have. There is also a sample implementation of a method that generates a gazing survey.

You still have to add the code that calls the start and stop methods.

class Program
{
    static Dictionary<object, Stopwatch> stopwatchesByObject;

    static void Main(string[] args)
    {
        List<object> objects = new List<object>();

        // now you have to fill the objects list...

        stopwatchesByObject = new Dictionary<object, Stopwatch>();
        foreach (var o in objects)
        {
            stopwatchesByObject.Add(o, new Stopwatch());
        }
    }

    // Call this method when the user starts gazing at object o
    static void StartGazingAt(object o)
    {
        stopwatchesByObject[o].Start();
    }

    // Call this method when the user stops gazing at object o
    static void StopGazingAt(object o)
    {
        stopwatchesByObject[o].Stop();
    }

    static void CreateStatistics()
    {
        StringBuilder sb = new StringBuilder();
        foreach (var entry in stopwatchesByObject)
        {
            sb.AppendLine($"Gazed at {entry.Key.ToString()} for {entry.Value.Elapsed.TotalSeconds} seconds.");
        }
        File.WriteAllText("c:\temp\statictics.txt", sb.ToString());
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...