Witam.
Napisałem pomniejszy programik do niszowego oprogramowania (KCPos), który wyświetla dane z pliku tekstowego. Oprogramowanie oryginalne, zapisuje dane do pliku ekran.txt
, a ja je mam czytać i wyświetlić na drugim, małym monitorze. Zasada "działania" jest jak w sklepach Biedronka - kasjerka kasuje, a klientowi pokazuje się na małym wyświetlaczu co jest już skasowane i jaka jest suma.
Kasjerka kasuje na KCPos -> to zostaje wpisane w odpowiednim formacie do pliku ekran.txt
-> ja odczytuje i wyświetlam w swoim programie.
PROBLEM
Aby to działało w miarę poprawnie, muszę w funkcji odczytującej linijka po linijce zrobić await Task.Delay(500)
, choć w niektórych przypadkach to i tak jest za mało. Wygląda to tak jakby kasjerka za szybko kasowała
public async Task<Document> ReadEkranFile()
{
await Task.Delay(500); // tymczasowe rozwiązanie
Document document = new Document();
var lines = File.OpenRead(_file.FullName);
using (StreamReader sr = new StreamReader(lines, Encoding.GetEncoding("Windows-1250")))
{
List<Product> products = new List<Product>();
sr.ReadLine();
sr.ReadLine();
while (sr.Peek() != -1)
{
var line = sr.ReadLine();
if (line != "-------------------------------------------------------------------------")
{
if (line.Length >= lineLength)
{
if (line.StartsWith("STORNO"))
{
int newLpSize = lpSize + 2;
int newNameSize = nameSize - 2;
var lp = line.Substring(0, newLpSize).Trim();
var name = line.Substring(newLpSize, newNameSize).Trim();
var unit = line.Substring(newLpSize + newNameSize, unitSize).Trim();
var quantity = line.Substring(newLpSize + newNameSize + unitSize, quantitySize).Trim();
var price = "-" + line.Substring(newLpSize + newNameSize + unitSize + quantitySize, priceSize).Trim();
var value = "-" + line.Substring(newLpSize + newNameSize + unitSize + quantitySize + priceSize, valueSize).Trim();
var product = new Product(lp, name, unit, quantity, price, value);
products.Add(product);
}
else
{
var lp = line.Substring(0, lpSize).Trim();
if (!string.IsNullOrEmpty(lp))
{
var name = line.Substring(lpSize, nameSize).Trim();
var unit = line.Substring(lpSize + nameSize, unitSize).Trim();
var quantity = line.Substring(lpSize + nameSize + unitSize, quantitySize).Trim();
var price = line.Substring(lpSize + nameSize + unitSize + quantitySize, priceSize).Trim();
var value = line.Substring(lpSize + nameSize + unitSize + quantitySize + priceSize, valueSize).Trim();
var product = new Product(lp, name, unit, quantity, price, value);
products.Add(product);
}
}
}
else if (line.Contains("DO ZAPŁATY"))
{
int index = line.IndexOf(":") + 1;
var toPay = line.Substring(index, 13).Trim();
document.ToPay = toPay;
}
else if (line.Contains("WPŁACONO"))
{
int index = line.IndexOf(":") + 1;
var payed = line.Substring(index, 13).Trim();
document.Payed = payed;
}
else if (line.Contains("RESZTA"))
{
int index = line.IndexOf(":") + 1;
var change = line.Substring(index, 13).Trim();
document.Change = change;
}
}
}
document.Products = products;
}
counter++;
return document;
}
FILE WATCHER
FileSystemWatcher watcher = new FileSystemWatcher(path);
watcher.Filter = "ekran.txt";
watcher.Changed += Watcher_Changed;
watcher.EnableRaisingEvents = true;
WATCHER CHANGED
private void Watcher_Changed(object sender, FileSystemEventArgs e)
{
try
{
if (e.Name == "ekran.txt")
{
if (InvokeRequired)
{
Invoke(new Action(() => LoadDocument(e)));
return;
}
}
else
{
Logger.Info(string.Format("Zmiana w pliku {0}", e.Name));
}
}
catch(Exception ex)
{
Logger.Error(ex);
}
}
Ja niczego do pliku nie dopisuje, ja go tylko odczytuje, aby wyświetlić co jest nabite na kasę. Nie chce co chwilę dokładać więcej milisekund, bo to nie jest rozwiązanie.
PS.
Z File.ReadLines
jest to samo.