Reputation: 107
I have written a snippet here for the serial data transfer to be done in a asynchronously. VS2015 threw a message that the method will be run synchronously and lacks the await. Please let me know where am I going wrong.
private bool SendRecieveSerialData(string port, byte Cmd, string fileName)
{
// bool write_status = ReadWriteSerialData(port, Cmd, fileName);
bool write_status = SendandReceiveAsync(port,Cmd,fileName).Result;
if (write_status)
{
return true;
}
else
{
return false;
}
}
//private bool ReadWriteSerialData(string port,byte Command, string fileName)
public bool ReadWriteSerialData(string port,byte Command, string fileName)
{
bool comFlag = false;
if (String.IsNullOrEmpty(port))
{
log.WriteErrorLog("Null Port");
comFlag = false;
return comFlag;
}
else
{
// SerialPortFixer.Execute(port);
serialPort = new SerialPort(port);
for (int count = 0; count < BluetoothGlobals.retry_count; count++)
{
byte[] text = File.ReadAllBytes(fileName);
try
{
if (!serialPort.IsOpen)
{
ConnectedFlag = true;
if (fileName != null)
{
for (int counter = 0; counter < text.Length; counter += 256)
{
int tempsize = text.Length - counter;
if(tempsize > 256)
{
tempsize = 256;
}
Array.Clear(DataFrame, 0, DataFrame.Length);
DataFrame[0] = (byte)(MessageFormat.START_FRAME); //Start frame
DataFrame[1] = (byte)(tempsize); // Frame Size to be dynamic
DataFrame[2] = (byte)(Command & 0xFFu); // LSB
DataFrame[3] = (byte)((Command >> 8) & 0xFFu); // MSB
Array.Copy(text, counter, DataFrame, 4, tempsize);
DataFrame[tempsize + 4] = (byte)(ComputeCheckSum(DataFrame) & 0xFFu);
DataFrame[tempsize + 5] = (byte)(((ComputeCheckSum(DataFrame) >> 8) & 0xFFu)); // MSB
DataFrame[tempsize + 6] = (byte)(MessageFormat.END_FRAME);
}
}
else
{
DataFrame[0] = (byte)(MessageFormat.START_FRAME); //Start frame
DataFrame[1] = (byte)(MessageFormat.PING_SIZE); // Frame Size to be dynamic
DataFrame[2] = (byte)(Command & 0xFFu); // LSB
DataFrame[3] = (byte)((Command >> 8) & 0xFFu); // MSB
DataFrame[4] = 0;
DataFrame[5] = (byte)(ComputeCheckSum(DataFrame) & 0xFFu);
DataFrame[6] = (byte)(((ComputeCheckSum(DataFrame) >> 8) & 0xFFu)); // MSB
DataFrame[7] = (byte)(MessageFormat.END_FRAME);
}
serialPort.Open();
serialPort.BaudRate = 115200;
serialPort.Parity = Parity.None;
serialPort.StopBits = StopBits.One;
serialPort.Handshake = Handshake.None;
serialPort.DataBits = 8;
serialPort.Write(DataFrame, 0, DataFrame.Length);
serialPort.DataReceived += new SerialDataReceivedEventHandler(Current_port_DataReceived);
comFlag = true;
if (comFlag)
{
break;
}
}
}
catch (Exception e)
{
log.WriteErrorLog(e.Message);
continue;
}
}
}
return comFlag;
}
public async Task<bool> SendandReceiveAsync(string portnum,byte cmd, string file)
{
bool task_state = await Task.Factory.StartNew(() => ReadWriteSerialData(portnum, cmd, file));
if (task_state)
return true;
else
return false;
}
Upvotes: 1
Views: 2999
Reputation: 1358
Do it like this: http://blog.stephencleary.com/2012/02/async-and-await.html
public async Task NewStuffAsync()
{
// Use await and have fun with the new stuff.
await ...
}
public Task MyOldTaskParallelLibraryCode()
{
// Note that this is not an async method, so we can't use await in here.
...
}
public async Task ComposeAsync()
{
// We can await Tasks, regardless of where they come from.
await NewStuffAsync();
await MyOldTaskParallelLibraryCode();
}
if you're calling ComposeAsync from an event
public async void ButtonClick(object sender, System.EventArgs e) {
await ComposeAsync; //or
await Task.Run(() => ComposeNonAsync());
}
Upvotes: 0
Reputation: 1741
The keyword async
in the methoddeclaration does NOT make a method be run asynchronously.
Only the method calls that called with await
inside a method declared with the async
keyword will be executed asynchronously.
For example:
public async Task RunPartsAsync()
{
using(var someStream = new SomeStream(someSource))
{
await someStream.ReadAsync(); //this will be executed asynchronously
someStream.ReadAsync(); //this will be executed asynchronously but not awaited
// => Console.WriteLine might be called/finish before ReadAsync finished
Console.WriteLine("asdf"); //this will NOT be executed asyncronously
}
}
Upvotes: 3
Reputation: 4881
You can try following:
Make you method sync
public bool ReadWriteSerialData(string port, byte Command, string fileName)
{
var comFlag = false;
// ... your cdode here.....
return comFlag;
}
Invoke it as separate task
public async Task<bool> SendandReceiveAsync(string portnum, byte cmd, string file)
{
bool task_state = await Task.Factory.StartNew(()=>ReadWriteSerialData(portnum, cmd, file));
if (task_state)
return true;
else
return false;
}
Upvotes: 0