I have two voids that have to be called after one another every 5 seconds.I mean first the first void has to be called and after that the second void has to be called and the we have 5 seconds break...but my program only calls the first void(get pressure) and ignores the second one....could anyone show me the code?
private void getPressure()
{
if (serialPort1.IsOpen)
{
string PCR = "";
byte[] readCommand = { 0x50, 0x0D };
serialPort1.Write(readCommand, 0, 2);
int bytestoread = serialPort1.BytesToRead;
if (bytestoread > 0)
{
byte[] input = new byte[bytestoread];
serialPort1.Read(input, 0, bytestoread);
PCR = System.Text.Encoding.UTF8.GetString(input);
}
if (PCR.StartsWith("P"))
{
if (PCR.Length > 15)
{
PCR = PCR.Substring(3, PCR.IndexOf("T") - 3).Trim();
var decPCR = Decimal.Parse(PCR, NumberStyles.AllowExponent | NumberStyles.AllowDecimalPoint);
rTxtBoxPressure.Text = decPCR + " Torr";
double data;
bool result = Double.TryParse(decPCR.ToString(), out data);
if (result)
{
serialDataChart.TriggeredUpdate(data);
}
}
}
}
}
private void getVoltage()
{
if (serialPort1.IsOpen)
{
string UCR = "";
byte[] readCommand2 = { 0x55, 0x0D };
serialPort1.Write(readCommand2, 0, 2);
int bytestoread2 = serialPort1.BytesToRead;
if (bytestoread2 > 0)
{
byte[] input2 = new byte[bytestoread2];
serialPort1.Read(input2, 0, bytestoread2);
UCR = System.Text.Encoding.UTF8.GetString(input2);
}
if (UCR.StartsWith("V"))
{
if (UCR.Length > 15)
{
UCR = UCR.Substring(5, UCR.IndexOf("Volts") - 5).Trim();
var decUCR = Decimal.Parse(UCR, NumberStyles.AllowExponent | NumberStyles.AllowDecimalPoint);
rtxtBoxVoltage.Text = decUCR + " Volts";
double data;
bool result = Double.TryParse(decUCR.ToString(), out data);
if (result)
{
serialDataChart2.TriggeredUpdate(data);
}
}
}
}
}
private void InitTimer()
{
timer1 = new Timer();
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Interval =5000;
timer1.Start();
}
private void timer1_Tick(object sender , EventArgs e)
{
getPressure();
getVoltage();
}
First of all, you define your time with Interval = 5000
, which will trigger timer event every 5 seconds.
Secondly, in timer event handler you call desired methods one after another, without any interval, so once getPressure
finishes, getVoltage
starts immediately.
So, to sum it up - you don't need timer, as in your requirements I don't see that both functions should be called periodically. The only thing we need to call one, and then after 2 seconds call another, so I'd suggest something among the lines
getPressure();
// wait two seconds between calls, you could also use Thread.Sleep(2 * 1000)
await Task.Delay(2 * 1000);
getVoltage();
IF you want to wait 2 seconds before calling getVoltage
, but it should be 2 seconds from starting getPressure
, then the code should look like:
var pressureTask = Task.Run(() => getPressure());
await Task.Delay(2 * 1000);
getVoltage();
await pressureTask;
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.