程序中的负毫秒

时间:2018-06-02 18:51:19

标签: c# xamarin

      //Global variables
bool pause;

        TextView ClickToStart;
        TextView ClickAsSoonAsPossible;
        TextView Tries;
        TextView TriesCount;
        TextView AverageMs;
        TextView AverageMsCount;
        Button ScreenClickButton;
        System.Timers.Timer timer1;
        Color currentColor = Color.White;
        Random rnd = new Random();
        DateTime startTime1;
        DateTime endTime1;
        int secondsToFormat;

        int ClicksCount = 0;



  private void Timer1_Elapsed(object sender, ElapsedEventArgs e)
                {
                    RunOnUiThread(() =>
                    {
                        Toast.MakeText(this, "Timer", ToastLength.Short).Show();
                        currentColor = Color.Green;
                        ScreenClickButton.SetBackgroundColor(currentColor);
                    });
                    timer1.Stop();
                    startTime1 = DateTime.Now;
                }


            private void ClickCount(object sender, EventArgs e)
            {
                if (currentColor == Color.White)
                {

                    currentColor = Color.Red;
                    ScreenClickButton.SetBackgroundColor(currentColor);
                    int seconds = rnd.Next(1, 5);
                    Toast.MakeText(this, "Seconds:" + seconds, ToastLength.Short).Show();
                    timer1.Interval = seconds * 1000;
                    timer1.Start();

                }
                else if (currentColor == Color.Green)
                {
                    endTime1 = DateTime.Now;
                    ClicksCount++;

                    secondsToFormat =endTime1.Millisecond - startTime1.Millisecond;

     AverageMsCount.Text = secondsToFormat.ToString();

                   // if(secondsToFormat>=1000)
                 //   {
                 //       AverageMs.Text = ((endTime1 - startTime1).Milliseconds).ToString();
                 //   }

                    currentColor = Color.White;
                    ScreenClickButton.SetBackgroundColor(currentColor);
                }
                else if (currentColor == Color.Red)
                {

                    currentColor = Color.White;
                    ScreenClickButton.SetBackgroundColor(currentColor);
                }


                //Toast.MakeText(this, "You clicked me " + ClicksCount + "times", ToastLength.Short).Show();
                if (ClicksCount == 1)

                    AverageMsCount.Text = secondsToFormat.ToString();
                TriesCount.Text = "1";

                if (ClicksCount == 2)
                {
                    secondsToFormat = secondsToFormat / 2;
                    AverageMsCount.Text = secondsToFormat.ToString();

                    TriesCount.Text = "2";

                }
                if (ClicksCount == 3)
                {
                    secondsToFormat = secondsToFormat / 3;
                    AverageMsCount.Text = secondsToFormat.ToString();
                    TriesCount.Text = "3";
                }
                if (ClicksCount == 4)
                {
                    secondsToFormat = secondsToFormat / 4;
                    AverageMsCount.Text = secondsToFormat.ToString();
                    TriesCount.Text = "4";

                }

                if (ClicksCount == 5)
                {
                    secondsToFormat = secondsToFormat / 5;
                    AverageMsCount.Text = secondsToFormat.ToString();
                    TriesCount.Text = "5";
                }


                if (Click`enter code here`sCount == 6)
                {
                    ClicksCount = 1;

                }



            }

大家好,我试图在Android Xamarin上做类似的事情:https://www.humanbenchmark.com/tests/reactiontime/。所以在第一次点击返回毫秒之后是负数,如-525。经过一番点击后,它变得更高,直到最终为正。有人可以解释为什么它的消极?经过的计时器基本上是随机时间弹出的绿屏。加速后我的毫秒计数开始了。当点击绿色按钮时,它就完成了。我真的很感激任何帮助。

1 个答案:

答案 0 :(得分:3)

这是问题所在:

from imblearn import FunctionSampler
from imblearn.pipeline import make_pipeline
from sklearn.ensemble import IsolationForest
from sklearn.model_selection import GridSearchCV
from sklearn.linear_model import LogisticRegression


class FunctionSamplerWrapper(object):    
    def unwrap(self, key, values):
        return FunctionSampler(func=outlier_rejection, kw_args={key[0]: values})

pipe = make_pipeline(FunctionSamplerWrapper().unwrap(key = ['max_samples'], 
                                                     values = [1]), 
                     LogisticRegression(random_state=rng))

cv = GridSearchCV(pipe, 
                  [{'logisticregression__C': [1., 10.], 
                   'functionsamplerwrapper__key': ['max_samples'],
                   'functionsamplerwrapper__values': [1, 10, 100]}])

secondsToFormat = endTime1.Millisecond - startTime1.Millisecond; 属性是“秒内的毫秒”。所以我们假设:

Millisecond

...这会给你-650毫秒的结果,因为startTime1 = 2018-06-02T18:52:30.750Z endTime1 = 2018-06-02T18:52:31.100Z 是100,而startTime1.Millisecond是750。

理想情况下:

  • 根本不要使用endTime1.Millisecond:使用您重置的DateTime并在您想要开始计时时启动,然后使用Stopwatch属性获取{{1} 1}}每当你想知道已经过了多少时间。使用Elapsed的{​​{1}}属性来查找已经过了多少毫秒。 (或者,使用TimeSpan。)
  • 如果 继续使用TotalMilliseconds
    • 使用TimeSpan代替Stopwatch.ElapsedMilliseconds,这样如果您的用户在时区偏移更改边界(例如夏令时)上运行此操作,您就不会得到奇怪的结果
    • 将这两个值之差取为DateTime,例如DateTime.UtcNow,然后再次使用DateTime.Now属性