asyc task

This commit is contained in:
klavirshik 2024-07-18 15:09:47 +02:00
parent a643ede634
commit 2542586eb4
10 changed files with 103 additions and 24 deletions

View File

@ -114,7 +114,7 @@
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 1000;
this.timer1.Interval = 300;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// Form1

View File

@ -13,7 +13,7 @@ namespace Reseter2
{
resetertask = reseterTask;
}
public abstract void Tick();
public abstract Task<PingResult> Tick();
public abstract void Stop();
public abstract void Next();
}

20
Reseter2/PingResult.cs Normal file
View File

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Reseter2
{
internal class PingResult
{
public long Ping { get; set; }
public int TimeoutPing { get; set; }
public PingResult(long ping, int timeoutPing)
{
Ping = ping;
TimeoutPing = timeoutPing;
}
}
}

View File

@ -20,7 +20,7 @@ namespace Reseter2
public int Timeout() {
return TimeoutCount;
}
public long PingHost()
public PingResult PingHost()
{
bool pingable = false;
long ping = 0;
@ -44,7 +44,7 @@ namespace Reseter2
}
}
if (pingable) TimeoutCount++;
return ping;
return new PingResult(ping, TimeoutCount);
}
}

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
@ -8,9 +9,9 @@ namespace Reseter2
{
class ReseterTask
{
private Task task;
private Task<PingResult> task;
private IComp Comp;
private AStatusTask StatusTask;
public AStatusTask StatusTask { get; set; }
private TaskControl taskControl;
private Pinger Pingers;
public delegate void DataEvents(string ping, string timeout);
@ -35,8 +36,8 @@ namespace Reseter2
{
if (task.IsCompleted){
//this.DataContrl(Ping().ToString(), Timeout().ToString());
await task;
PingResult pingResult = await task;
taskControl.DataContrl(pingResult.Ping.ToString() + "ms", pingResult.TimeoutPing.ToString());
task = Task.Run(StatusTask.Tick);
}
}
@ -48,7 +49,11 @@ namespace Reseter2
}
public long Ping()
public void SetNameStage(string nameStage)
{
taskControl.SetNameStage(nameStage);
}
public PingResult Ping()
{
return Pingers.PingHost();
}
@ -60,7 +65,7 @@ namespace Reseter2
public void DataContrl(string ping, string timeout)
{
// taskControl.Invoke(DataChange);
DataChange.Invoke(ping, timeout);
// DataChange.Invoke(ping, timeout);
}
private void Clear()
{

View File

@ -56,10 +56,12 @@
<Compile Include="IComp.cs" />
<Compile Include="IStatusTask.cs" />
<Compile Include="Pinger.cs" />
<Compile Include="PingResult.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="reseter.cs" />
<Compile Include="ReseteTask.cs" />
<Compile Include="StatusReboot.cs" />
<Compile Include="StausPreReboot.cs" />
<Compile Include="TaskControl.cs">
<SubType>UserControl</SubType>

42
Reseter2/StatusReboot.cs Normal file
View File

@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Reseter2
{
internal class StatusReboot : AStatusTask
{
private int TimeCount;
public StatusReboot(ReseterTask reseterTask) : base(reseterTask)
{
// resetertask.SetNameStage("Перезагрузка");
}
public override Task<PingResult> Tick()
{
PingResult result = resetertask.Ping();
if (result.Ping == 0)
{
TimeCount++;
}
if (TimeCount > 2)
{
resetertask.StatusTask = new StatusPreReboot(resetertask);
}
return Task.FromResult(result);
// return resetertask.DataContrl(pingResult.Ping.ToString(), pingResult.Ping.ToString());
}
public override void Next()
{
}
public override void Stop()
{
}
}
}

View File

@ -11,12 +11,19 @@ namespace Reseter2
private int time;
public StatusPreReboot(ReseterTask reseterTask) : base(reseterTask)
{
resetertask.SetNameStage("Проверка связи");
}
public override void Tick()
public override Task<PingResult> Tick()
{
time++;
resetertask.DataContrl(resetertask.Ping().ToString(), resetertask.Timeout().ToString());
PingResult result = resetertask.Ping();
if (result.Ping > 0)
{
resetertask.StatusTask = new StatusReboot(resetertask);
}
return Task.FromResult(result);
// return resetertask.DataContrl(pingResult.Ping.ToString(), pingResult.Ping.ToString());
}
public override void Next()

View File

@ -32,7 +32,7 @@
this.label2 = new System.Windows.Forms.Label();
this.lb_name = new System.Windows.Forms.Label();
this.lb_ip = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.lb_stage = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.lb_ping = new System.Windows.Forms.Label();
this.Timeout = new System.Windows.Forms.Label();
@ -80,14 +80,14 @@
this.lb_ip.TabIndex = 3;
this.lb_ip.Text = "10.3.123.123";
//
// label3
// lb_stage
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(74, 10);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(104, 13);
this.label3.TabIndex = 4;
this.label3.Text = "Перезагружаеться";
this.lb_stage.AutoSize = true;
this.lb_stage.Location = new System.Drawing.Point(74, 10);
this.lb_stage.Name = "lb_stage";
this.lb_stage.Size = new System.Drawing.Size(104, 13);
this.lb_stage.TabIndex = 4;
this.lb_stage.Text = "Перезагружаеться";
//
// label4
//
@ -103,9 +103,8 @@
this.lb_ping.AutoSize = true;
this.lb_ping.Location = new System.Drawing.Point(108, 49);
this.lb_ping.Name = "lb_ping";
this.lb_ping.Size = new System.Drawing.Size(38, 13);
this.lb_ping.Size = new System.Drawing.Size(0, 13);
this.lb_ping.TabIndex = 6;
this.lb_ping.Text = "100ms";
//
// Timeout
//
@ -163,7 +162,7 @@
this.Controls.Add(this.Timeout);
this.Controls.Add(this.lb_ping);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.lb_stage);
this.Controls.Add(this.lb_ip);
this.Controls.Add(this.lb_name);
this.Controls.Add(this.label2);
@ -182,7 +181,7 @@
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label lb_name;
private System.Windows.Forms.Label lb_ip;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label lb_stage;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label lb_ping;
private System.Windows.Forms.Label Timeout;

View File

@ -32,6 +32,10 @@ namespace Reseter2
lb_timeout.Text = timeout;
}
public void SetNameStage(string nameStage)
{
lb_stage.Text = nameStage;
}
private void label1_Click(object sender, EventArgs e)
{