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;
using System.Threading;
namespace WindowsFormsApp1
{
delegate void ThreadCallBackDelegate(TestData testData);
public struct Item
{
public string name;
public int testCount;
public int currentCount;
public int status;
};
public struct TestData
{
public int id;
public Item[] item;
};
public partial class Form1 : Form
{
public TestData testData;
public Form1()
{
InitializeComponent();
testData.item = new Item[2];
}
private void ThreadCallBack(TestData testData)
{
if (testData.id == -1)
{
this.label1.Text = "all pass";
return;
}
if (testData.item[testData.id].status == 2)
{
this.label1.Text = testData.item[testData.id].name + " pass";
}
else
this.label1.Text = ("CallBack:" + testData.item[testData.id].name+ testData.item[testData.id].currentCount);
}
private void button1_Click(object sender, EventArgs e)
{
Test test = new Test();
int id = 0;
testData.item[id].testCount = 3;
testData.item[id].currentCount = 0;
testData.item[id].status = 1;
testData.item[id].name = "cs";
testData.item[++id].testCount = 5;
testData.item[id].currentCount = 0;
testData.item[id].status = 1;
testData.item[id].name = "reboot";
test.testData = this.testData;
test.callBack = ThreadCallBack;
Thread t = new Thread(new ThreadStart(test.TestAll));
t.Start();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace WindowsFormsApp1
{
class Test
{
public ThreadCallBackDelegate callBack;
public TestData testData;
public void TestAll()
{
TestCs();
TestReboot();
testData.id = -1;
callBack(testData);
}
public void TestCs()
{
int id = 0;
testData.id = id;
if (testData.item[id].status == 0) return;
for (; testData.item[id].currentCount < testData.item[id].testCount; testData.item[id].currentCount++)
{
Thread.Sleep(1000);
callBack(testData);
}
testData.item[id].status = 2;
Thread.Sleep(500);
callBack(testData);
}
public void TestReboot()
{
int id = 1;
testData.id = id;
if (testData.item[id].status == 0) return;
for (; testData.item[id].currentCount < testData.item[id].testCount; testData.item[id].currentCount++)
{
Thread.Sleep(500);
callBack(testData);
}
testData.item[id].status = 2;
Thread.Sleep(500);
callBack(testData);
}
}
}