this is the updated version of the code does this add up with all the edits you made? I think it's mostly accurate but you can also try with visual studio, if you already did is it working on yours?
namespace Lexis
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
serialPort1.Open();
string lastLine = string.Empty;
Task.Run(() =>
{
while (true)
{
string tailValue = lastLine;
lastLine = serialPort1.ReadLine();
string line = lastLine;
label1.BeginInvoke(new Action
(() =>
{
label1.Text = string.IsNullOrEmpty(line) || string.Equals(tailValue, line)
? label1.Text
: $"{line}{Environment.NewLine}{label1.Text}";
}
));
Task.Delay(1000).Wait();
}
});
}
private void label1_Click(object sender, EventArgs e)
{
}
}
}
This may not be your golden solution but I hope this gives you a lead.
public Form1()
{
InitializeComponent();
serialPort1.Open();
string currentLine = string.Empty;
object mylock = new object();
Task.Run(() =>
{
while (true)
{
lock (mylock)
{
string previousLine = currentLine;
currentLine = serialPort1.ReadLine();
string line = currentLine;
label1.BeginInvoke(new Action
(() =>
{
label1.Text = string.IsNullOrEmpty(line) || string.Equals(previousLine, line)
? label1.Text
: $"{line}{Environment.NewLine}{label1.Text}";
}
));
Task.Delay(TimeSpan.FromSeconds(1)).Wait();
}
}
});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.