I have a demo application from the supplier. This demo application allows me to connect to a RFID reader via a network connection. Once the connection is established I can then scan for RFID tags and the tag data is then displayed on the application.
The eventhandler is raised whenever the CAlienServer receives a message from the RFID reader.
private void addServerEvents(int idx)
{
mServers[idx].ServerMessageReceived += new CAlienServer.ServerMessageReceivedEventHandler(mServer_ServerMessageReceived);
mServers[idx].ServerConnectionEstablished += new CAlienServer.ServerConnectionEstablishedEventHandler(mServer_ServerConnectionEstablished);
mServers[idx].ServerConnectionEnded += new CAlienServer.ServerConnectionEndedEventHandler(mServer_ServerConnectionEnded);
mServers[idx].ServerSocketError += new CAlienServer.ServerSocketErrorEventHandler(mServer_ServerSocketError);
}
private void mServer_ServerMessageReceived(string msg)
{
if (msg != null)
{
displayText(msg.Insert(msg.IndexOf(" ") + 1, "\r\n"));
}
}
private void displayText(String data)
{
try
{
if (this.InvokeRequired)
{
object[] temp = {data};
this.Invoke(new displayMessageDlgt(displayText),temp);
return;
}
else
{
txtNotifications.Text += data.Replace("\0","") + "\r\n";
Task task = insert(data);
NotifyInfo ni = null;
AlienUtils.ParseNotification(data, out ni);
if (ni != null)
{
lblReaderName.Text = ni.ReaderName;
}
}
}
catch(Exception ex)
{
Debug.WriteLine("Exception in the DiscplayText(): " + ex.Message);
}
}
I want to know run another function that stores the data in the variable 'data' or 'msg' into a database. I know calling a synchronous function will not work because what if the programming is running the insertion of the data into the database function and the reader picks up new tags. Hence a synchronous function will not work. I am thinking of using an asynchronous function instead. So my database insertion function is. Is this the correct way to go about it?
private async Task insert(string rfid)
{
await Task.Factory.StartNew(()=>
{
using (var connection = new MySqlConnection("server=localhost;user id='" + username_mysql + "';database=new;password='" + password_mysql + "'"))
{
connection.Open();
try
{
string sql = "INSERT INTO `data` (`RFID TAG`) VALUE (@rfid)";
MySqlCommand cmd = new MySqlCommand(sql, connection);
cmd.Parameters.Add("@rfid", MySqlDbType.VarChar).Value = rfid;
if (cmd.ExecuteNonQuery() == 1)
{
}
}
catch (Exception ex)
{
}
}
});
}
Update:
Code:
private void insert(string rfid)
{
using (var connection = new MySqlConnection("server=localhost;user id='" + username_mysql + "';database=alien;password='" + password_mysql + "'"))
{
connection.Open();
try
{
string sql = "INSERT INTO `data` (`DATA`) VALUE (@data)";
MySqlCommand cmd = new MySqlCommand(sql, connection);
cmd.Parameters.Add("@data", MySqlDbType.VarChar).Value = rfid;
if (cmd.ExecuteNonQuery() == 1)
{
Debug.WriteLine("Successfull: " + rfid);
textBox2.BackColor = Color.Green;
}
}
catch (Exception ex)
{
Debug.WriteLine("Exception in the insert(): " + ex.Message);
}
}
}
private void displayText(String data)
{
try
{
if (this.InvokeRequired)
{
object[] temp = {data};
this.Invoke(new displayMessageDlgt(displayText), temp);
return;
}
else
{
txtNotifications.Text += data.Replace("\0","") + "\r\n";
//await Task.Run(() => insert(data));
insert(data);
textBox2.BackColor = Color.Red;
NotifyInfo ni = null;
AlienUtils.ParseNotification(data, out ni);
if (ni != null)
{
lblReaderName.Text = ni.ReaderName;
}
}
}
catch(Exception ex)
{
Debug.WriteLine("Exception in the DisplayText(): " + ex.Message);
}
}
SERVER MESSAGE is from the mServer_ServerMessageReceived(string msg) method Succesfull is from the insert() method.
This clearly shows that all of that tag data is been inserted in the database.
Debug output:
SERVER MESSAGE: 00:1B:5F:00:F9:B4 0004,3,1
SERVER MESSAGE: 00:1B:5F:00:F9:B4 A123 0001,3,1
Successfull: 00:1B:5F:00:F9:B4 0004,3,1
SERVER MESSAGE: 00:1B:5F:00:F9:B4 A641 2541,3,1
Successfull: 00:1B:5F:00:F9:B4 A123 0001,3,1
SERVER MESSAGE: 00:1B:5F:00:F9:B4 0014,3,1
Successfull: 00:1B:5F:00:F9:B4 A641 2541,3,1
SERVER MESSAGE: 00:1B:5F:00:F9:B4 5253 7AB2 0200 0000 0000 1002,3,1
Successfull: 00:1B:5F:00:F9:B4 0014,3,1
SERVER MESSAGE: 00:1B:5F:00:F9:B4 A064 0001 0601,3,1
Successfull: 00:1B:5F:00:F9:B4 5253 7AB2 0200 0000 0000 1002,3,1
SERVER MESSAGE: 00:1B:5F:00:F9:B4 0000 0000,3,1
SERVER MESSAGE: 00:1B:5F:00:F9:B4 0001,3,1
SERVER MESSAGE: 00:1B:5F:00:F9:B4 2123 6521,3,1
SERVER MESSAGE: 00:1B:5F:00:F9:B4 0016,3,1
Successfull: 00:1B:5F:00:F9:B4 A064 0001 0601,3,1
Successfull: 00:1B:5F:00:F9:B4 0000 0000,3,1
Successfull: 00:1B:5F:00:F9:B4 0001,3,1
Successfull: 00:1B:5F:00:F9:B4 2123 6521,3,1
Successfull: 00:1B:5F:00:F9:B4 0016,3,1