如何更有效地在 C# 中创建 300 个秒表? [英] How to create 300 stopwatches in C# more efficiently?

查看:47
本文介绍了如何更有效地在 C# 中创建 300 个秒表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个 UWP 应用程序,该应用程序需要测量用户查看对象的时间.这个应用程序有大约 300 个对象来测量时间.为此,我们将使用大约 300 个计时器.为此,我们必须单独创建 300 个秒表,这是非常低效的.

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.

计时器在用户注视相应对象时启动,并在用户不再注视相应对象时停止.如果用户的目光再次注视相应的对象,则计时器当然会再次启动.最后,秒表的所有时间都会保存到一个文件中.创建 300 个秒表需要为每个秒表添加一行新代码,这似乎不是很有效.我曾尝试使用 Enumerable.range 使秒表创建过程自动化,但到目前为止我还没有找到解决方案.

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();

推荐答案

因此,首先您需要获取可用对象的列表.您可以使用以下代码创建一个通用字典,为您拥有的每个对象保存一个秒表.还有一个生成凝视调查的方法的示例实现.

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.

您仍然需要添加调用 start 和 stop 方法的代码.

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());
    }
}

这篇关于如何更有效地在 C# 中创建 300 个秒表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆