You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
2.7 KiB

4 years ago
using System;
using System.Windows.Forms;
namespace CK.SCP.UniApi
{
public partial class CktUniApiButton : UserControl
{
public string Title
{
get{return groupPanel.Text;}
set{groupPanel.Text = value;}
}
public bool SwitchEnabled
{
get { return Switch.Value; }
set { Switch.Value = value; }
}
public int InitCount { get; set; }
public int Count
{
get { return IntCounter.Value; }
set { IntCounter.Value = value; }
}
public bool EnableManual
{
get { return ButtonManual.Enabled; }
set { ButtonManual.Enabled = value; }
}
public CktUniApiButton()
{
InitializeComponent();
}
public delegate void ButtonClickHandler(object sender, EventArgs e);
public event ButtonClickHandler ButtonClick;
private void ButtonManual_Click(object sender, EventArgs e)
{
try
{
Console.WriteLine($@"手动执行:{Title}。");
RunOnceExecuted();
ButtonClick?.Invoke(sender, e);
}
catch
{
// ignored
}
}
public delegate void SwitchValueChangedHandler(object sender, EventArgs e);
public event SwitchValueChangedHandler SwitchValueChanged;
private void Switch_ValueChanged(object sender, EventArgs e)
{
try
{
SwitchValueChanged?.Invoke(sender, e);
}
catch
{
// ignored
}
}
public delegate void RunOnceHandler();
public event RunOnceHandler RunOnceExecute;
private void RunOnceExecuted()
{
try
{
RunOnceExecute?.Invoke();
}
catch
{
// ignored
}
}
public void Start()
{
if (!SwitchEnabled) return;
Console.WriteLine($@"启动:{Title},执行周期:{Count}秒。");
RunOnceExecuted();
Count = InitCount;
}
public void Stop()
{
if (!SwitchEnabled) return;
Console.WriteLine($@"停止:{Title}。");
}
public void RunOnce()
{
if (!SwitchEnabled) return;
if (Count == 0)
{
RunOnceExecuted();
Count = InitCount;
}
else
{
Count--;
}
}
}
}