private static int _interval = 60;
private static DatabaseCtx _ctx = DatabaseCtx.Instance;
public static BindingList<Data> Items { get; set; } = new BindingList<Data>();
public ExportGridForm()
{
InitializeComponent();
ReloadData();
bindingSource1.DataSource = Items;
}
private void ReloadData()
{
Items.Clear();
foreach (var data in _ctx.GetDatas()) Items.Add(data);
}
public partial class Form1 : Form
{
private LongTimeJob _ltj;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
_ltj = new LongTimeJob();
button1.Enabled = false;
_ltj.Complete += _ltj_Complete;
_ltj.Tick += _ltj_Tick;
_ltj.Run();
}
private void _ltj_Tick(object sender, TickArgs args) =>
progressBar1.Invoke((MethodInvoker)delegate { progressBar1.Value = args.Percent; });
private void _ltj_Complete(object sender, EventArgs args)
{
button1.Invoke((MethodInvoker)delegate { button1.Enabled = true; });
textBox1.Invoke((MethodInvoker)delegate { textBox1.Text = _ltj.Text; });
}
}
public class LongTimeJob
{
public int Percent { get; private set; }
public string Text { get; private set; } = "";
public void Run()
{
Percent = 0;
Task.Run(async () => await Download());
}
private async Task Download()
{
for (var i = 0; i < 9; i++)
{
Percent = i * 10;
Text += $"Download: {i}\r\n";
OnTick(Percent);
await Task.Delay(400);
}
await Parse();
}
private async Task Parse()
{
Percent = 100;
Text += "Parsing\r\n";
OnTick(Percent);
await Task.Delay(500);
OnComplete();
}
public event TickDelegate Tick;
protected virtual void OnTick(int percent)
{
Tick?.Invoke(this, new TickArgs(percent));
}
public event CompleteDelegate Complete;
protected virtual void OnComplete()
{
Complete?.Invoke(this, new EventArgs());
}
}
public delegate void TickDelegate(object sender, TickArgs args);
public delegate void CompleteDelegate(object sender, EventArgs args);
public class TickArgs : EventArgs
{
public TickArgs(int percent)
{
Percent = percent;
}
public int Percent { get; private set; }
}
public void Truncate()
{
var truncate = "TRUNCATE TABLE HOUSE_HOUSENUM";
var reset = "DBCC CHECKIDENT ('HOUSE_HOUSENUM', RESEED, 0)";
_ctx.Database.ExecuteSqlRaw(truncate);
_ctx.Database.ExecuteSqlRaw(reset);
}
void Test()
{
var peoples = new People[]
{
new People{LastName = "Ivanov", Name = "Ivan", School = 17, One = 5, Two = 4},
new People{LastName = "Petrov", Name = "Petr", School = 17, One = 5, Two = 3},
new People{LastName = "Dariyana", Name = "Daria", School = 17, One = 5, Two = 4},
new People{LastName = "Ivanov", Name = "Ivan", School = 16, One = 5, Two = 4},
new People{LastName = "Petrov", Name = "Petr", School = 16, One = 5, Two = 3},
new People{LastName = "Dariyana", Name = "Daria", School = 16, One = 5, Two = 4},
new People{LastName = "Dariyana", Name = "Daria", School = 19, One = 2, Two = 2},
};
var path = "file.xml";
Save(path,peoples);
peoples = Load(path);
var schoolList = peoples.Select(s => s.School).Distinct();
var best =
(from sn in schoolList let max = peoples.Where(s => s.School == sn)
.Max(x => x.One + x.Two)
select
peoples.First(x => x.School == sn && (x.One + x.Two) == max))
.ToList();
;
}
private static People[] Load(string path)
{
var formatter = new XmlSerializer(typeof(People[]));
using var fs = new FileStream(path, FileMode.OpenOrCreate);
var peoples = (People[])formatter.Deserialize(fs);
return peoples;
}
private static void Save(string path, People[] peoples)
{
var formatter = new XmlSerializer(typeof(People[]));
using var fs = new FileStream(path, FileMode.OpenOrCreate);
formatter.Serialize(fs, peoples);
}
[Serializable]
public class People
{
public string LastName { get; set; }
public string Name { get; set; }
public int School { get; set; }
public int One { get; set; }
public int Two { get; set; }
}