Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 生成随机数并使用InotifyProperty更改为更新UI_C#_Winforms_Random_Inotifypropertychanged - Fatal编程技术网

C# 生成随机数并使用InotifyProperty更改为更新UI

C# 生成随机数并使用InotifyProperty更改为更新UI,c#,winforms,random,inotifypropertychanged,C#,Winforms,Random,Inotifypropertychanged,我是c#的新手,目前正在做一个项目。它有一个简单的用户界面,有两个按钮(一个叫开,另一个叫关),还有一个显示结果的文本框。基本上,我想做的是,如果用户单击“on”按钮,在不同于windows窗体的类上,将使用一种方法每隔一秒钟生成一个随机数。通过实现INotifyPropertyChanged,我想让Textbox知道这个值已经更新了,这样Textbox就会用我们新的随机数不断更新。一旦用户点击“关闭”按钮,我想停止生成随机数 我的windows窗体 using System; using Sy

我是c#的新手,目前正在做一个项目。它有一个简单的用户界面,有两个按钮(一个叫,另一个叫),还有一个显示结果的文本框。基本上,我想做的是,如果用户单击“on”按钮,在不同于windows窗体的类上,将使用一种方法每隔一秒钟生成一个随机数。通过实现INotifyPropertyChanged,我想让Textbox知道这个值已经更新了,这样Textbox就会用我们新的随机数不断更新。一旦用户点击“关闭”按钮,我想停止生成随机数

我的windows窗体

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SWE_Assignment
{
    public partial class Monitor : Form
    {
        Patient newPatient = Patient.Instance;
        public static bool pulseRateOn = false;
        public Monitor()
        {
            InitializeComponent();

            newPatient.PropertyChanged += _PulseRate_PropertyChanged;
        }

        private void Save_Click(object sender, EventArgs e)
        {

           // newPatient.PL(newPatient.startRnd = true;);
        }

        void _PulseRate_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "PulseRate")
            {
                HeartBeat.Text = newPatient.PulseRate.ToString();
            }
        }



        private void Stop_Click(object sender, EventArgs e)
        {
            newPatient.startRnd = false;
        }
    }
}

我的患者类别

namespace SWE_Assignment
{
    class Patient : INotifyPropertyChanged
    {

        private int _pulseRate;
        public bool startRnd = false;
        Random rnd = new Random();
        public int PulseRate
        {
            get { return _pulseRate; }
            set
            {
                _pulseRate = value;
                OnPropertyChanged("PL");
            }
        }


        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string properyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(properyName));
        }

        private static Patient _instance = null;
        private static readonly object _padlock = new object();
        public static Patient Instance
        {
            get
            {
                lock (_padlock)
                {
                    if (_instance == null)
                    {
                        _instance = new Patient();
                    }
                    return _instance;
                }
            }
        }




        public void PL(bool srt)
        {
            Timer timer = new Timer();
            timer.AutoReset = true;
            timer.Interval = 1000;


            if (startRnd == true)
            {
                timer.Elapsed += PLS;
                timer.Enabled = true;
                timer.Start();
            } else
            {
                timer.Enabled = false;
                timer.Stop();
            }
        }

        private void PLS(object sender, ElapsedEventArgs e)
        {

            PulseRate = rnd.Next(100, 150);
            Console.WriteLine(PulseRate);

        }
    }
}

另外,我对我的患者使用单例模式,因为我只希望有一个患者实例,这样我就可以访问另一个类(称为Alarm)上的同一个随机数,以检查它是否大于或小于某个数字。我确实意识到我的“停止”按钮是错误的,因为它只会再次调用该方法,而不会停止该方法的运行。如果有人能帮忙,我将不胜感激。

有几件事需要改变

OnPropertyChanged(“PL”)-属性名称为
PulseRate
。“PulseRate”是您在事件处理程序中签入的内容。使其
OnPropertyChanged(“PulseRate”)

Timer=new Timer()-每个患者只应创建一个计时器实例。最好在构造函数中完成。否则,您将有多个正在运行的计时器副本

可以通过启用的属性管理计时器: 调用Start()方法与将
Enabled
设置为
true
相同。同样,调用
Stop()
方法与将
Enabled
设置为
false
相同

private void Stop_Click(object sender, EventArgs e)
{
    // newPatient.startRnd = false;
    newPatient.PL(false);
}

class Patient : INotifyPropertyChanged
{
    Timer timer;
    private Patient()
    {
        timer = new Timer();
        timer.AutoReset = true;
        timer.Interval = 1000;
        timer.Elapsed += PLS;
    }

    Random rnd = new Random();

    private int _pulseRate;
    public int PulseRate
    {
        get { return _pulseRate; }
        set
        {
            _pulseRate = value;
            OnPropertyChanged("PulseRate");
        }
    }

    public void PL(bool srt)
    {
        timer.Enabled = srt;
    }

    private void PLS(object sender, ElapsedEventArgs e)
    {
        PulseRate = rnd.Next(100, 150);
        Console.WriteLine(PulseRate);
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string properyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(properyName));
    }

    private static Patient _instance = null;
    private static readonly object _padlock = new object();
    public static Patient Instance
    {
        get
        {
            lock (_padlock)
            {
                if (_instance == null)
                {
                    _instance = new Patient();
                }
                return _instance;
            }
        }
    }
}

OnPropertyChanged(“PL”)属性的名称为PulseRate。PulseRate是您在event hadler中检查的内容。使其
OnPropertyChanged(“PulseRate”)
定时器=新定时器()-每个患者只应创建一个计时器实例。最好在构造函数中完成。否则,您将有我创建的TimerTank的多个运行副本,一旦我运行它并单击“开始”,我会得到以下错误系统。invalidoperationexception跨线程操作无效,从创建它的线程以外的线程访问控件。当我在类构造函数内创建timer时,在我的PLS方法中,我得到一个错误,即当前上下文中不存在名称“timer”。您是否使用WinForms?请参阅编辑-我添加了字段
timer
谢谢,这次它给了我错误代码,对象引用未设置为对象的实例。请注意,我需要更改类构造函数,并且不再得到关于timer的错误,但是我得到system.invalidoperationexception跨线程操作无效控件,该控件是从创建它的线程以外的线程访问的