EDN Admin
Well-known member
I am currently working on a tool which takes txt log file(15-25 MB and 40000 to 70000 records) and looks for database and writes to xml file. But it takes nearly 60 to 90 minutes with sequential programming. Please do help me make this code thread one so that i can increase the performance and reducing the conversion time.
here is the code to using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using System.Data.OleDb;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
label1.Text = openFileDialog1.FileName;
//textBox1.Text = File.ReadAllText(label1.Text);
}
}
private void button2_Click(object sender, EventArgs e)
{
}
private static string sample(string Message, string MessageId, string Payload)
{
if (Message == null)
{
Message = "";
return Message;
}
else
{
if (Message == "MumdAudioControlStatus" || Message == "MumdAudioControlRequest"
|| Message == "MumdAudioControlStatusMsg" || Message == "MumdAudioControlRequestMsg")
{
string payload = "http://ccpcweb.comm.mot.com/users/Tools/MsgMonitorInterpreter_Test/DecodeAmPackets.asp?SysType=";
return payload;
}
else
{
if (Message == "MumdTrunkingCallControlRequest" ||
Message == "MumdConventionalCallControlRequest" ||
Message == "MumdTrunkingCallControlStatus" ||
Message == "MumdConventionalCallControlStatus")
{
if (Message == "MumdTrunkingCallControlRequest" ||
Message == "MumdConventionalCallControlRequest")
{
String[] NewPayload = Payload.Split);
string sOpCode = NewPayload[1].Substring(60, 4);
string sPayload = NewPayload[1].Substring(65, NewPayload[1].Length - 65);
//string newPayload =
string newPayload = Decoder.Decoders(sOpCode, sPayload, true);
return newPayload;
}
else
{
String[] NewPayload = Payload.Split);
string sOpCode = NewPayload[0].Substring(65, 4);
int temp = NewPayload[0].Length - 70;
string sPayload = NewPayload[0].Substring(70, temp);
string newPayload = Decoder.Decoders(sOpCode, sPayload, true);
return newPayload;
}
}
else
{
if (Message != "MlogWrite")
{
String[] sOpCode = MessageId.Split);
String[] sPayload = Payload.Split);
string newPayload = Decoder.Decoders(sOpCode[1], sPayload[1], false);
return newPayload;
}
}
}
return Message;
}
}
private void button3_Click(object sender, EventArgs e)
{
if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
label2.Text = saveFileDialog1.FileName;
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
XmlWriter writer = XmlWriter.Create(label2.Text,settings);
writer.WriteStartDocument();
writer.WriteStartElement("Log_details");
string file = openFileDialog1.FileName;
string[] readText = File.ReadAllLines(file);
XElement root = new XElement("root");//create a root node
for(int i=0;i< readText.Length;i++)
{
if (!readText.Contains("#"))
{
string[] fields = readText.Split(t); //<-- change field separator here & split fields
for (int j=0;j<fields.Length;j++)
{
string[] value;
if (fields[j].Contains(":"))
{
value = fields[j].Split(new char[] { : }, 2);
string temp = value[0].ToString();
if (temp.Contains("""))
{
XElement field = new XElement(temp.Trim(new Char[] { " }), value[1].ToString().Trim());
field.WriteTo(writer);
}
else
{
if (temp == "Message")
{
XElement field = new XElement(value[0].ToString().Trim(), value[1].ToString().Trim());
field.WriteTo(writer);
if (value[1].ToString().Trim() != "MlogWrite")
{
writer.WriteStartElement("DecodedPayload");
string Decoded=sample(fields[7], fields[8], fields[9]);
string[] decodedFields = Decoded.Split(t);
for(int k=0;k<decodedFields.Length;k++)
{
string[] xmlNode;
if (decodedFields[k].Contains(":"))
{
xmlNode = decodedFields[k].Split(new char[] { : }, 2);
XElement childElement = new XElement(xmlNode[0].ToString().Trim(), xmlNode[1].ToString().Trim());
childElement.WriteTo(writer);
}
else
{
XElement childElement = new XElement("ApiName", decodedFields[k]);
childElement.WriteTo(writer);
}
}
writer.WriteEndElement();
}
}
else
{
XElement field = new XElement(value[0].ToString().Trim(), value[1].ToString().Trim());
field.WriteTo(writer);
}
}
}
else if (fields[j].Contains("-"))
{
value = fields[j].Split(-);
XElement field = new XElement(value[0].ToString().Trim(), value[1].ToString().Trim());
field.WriteTo(writer);
}
else
{
writer.WriteStartElement("Logs");
XElement field = new XElement("LogNo", fields[j].ToString().Trim());
field.WriteTo(writer);
}
}
writer.WriteEndElement();
}
}
writer.WriteEndElement(); //SAVED
writer.Close();
}
}
}
}
and decoder.cs contains calls to database.using System;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Collections.Generic;
namespace WindowsFormsApplication1
{
class Decoder
{
private static OleDbConnection GetConnection()
{
//connection
}
private static void CloseConnection(OleDbConnection conn)
{
//close
}
private static int ProcessHex(string str)
{
var strOut = "";
for (var i = 0; i < str.Length; i += 5)
{
strOut += str.Substring(i + 2, 2);
strOut += str.Substring(i, 2);
}
return Int32.Parse(strOut, System.Globalization.NumberStyles.HexNumber);
}
private static string Process(string str)
{
string strOut = "";
for (var i = 0; i < str.Length; i += 5)
{
strOut += str.Substring(i + 2, 2);
strOut += str.Substring(i, 2);
}
return strOut;
}
public static string Decoders(string opCode, string payLoad, bool isZCMsg)
{
String szSQL;
int szMsgId, wApiId, wGroupId;
bool bIsHidden;
dynamic wParamId, bDisplayHex, wNoOfBytes, szParamName, szTypeName, wTypeId;
int bTryToDecode = 1, bDecodeFailed = 1;
string szParamValueDec, szParamValueHex, szAlias = "", szTempMsg = "", szApiName;
ushort wParamTypeId = 0, wAliasLength = 0, wStationControlCmdCount = 0;
ushort byValue;
ushort wValue;
uint dwValue;
String[] field = payLoad.Split(.);
string fields = string.Join(String.Empty, field); // Null arguments are bad
try
{
if (true == isZCMsg)
{
szMsgId = Int16.Parse(opCode, System.Globalization.NumberStyles.HexNumber);
szSQL = "SELECT * FROM Api WHERE ApiDefineValue = " + szMsgId.ToString() + " AND ApiGroupId = 10";
}
else
{
szMsgId = ProcessHex(opCode);
szSQL = "SELECT * FROM Api WHERE ApiDefineValue = " + szMsgId.ToString() + " AND ( (ApiGroupId <> 10) and (ApiGroupId <> 22) )";
}
OleDbConnection conn = GetConnection();
DataSet dataset = new DataSet();
OleDbDataAdapter adapter = new OleDbDataAdapter();
OleDbCommand command = new OleDbCommand(szSQL, conn);
adapter.SelectCommand = command;
adapter.Fill(dataset);
//conn.Close();
DataTable datatable = dataset.Tables[0];
foreach (DataRow rows in datatable.Rows)
{
//Console.Write(rows["ApiId"].ToString() + "t" + rows["ApiName"] + "t" + rows["ApiGroupId"] + rows["ApiDefineValue"].ToString());
wApiId = Convert.ToInt32(rows["ApiId"]);
}
if (dataset.Tables[0].Rows.Count != 0)
{
wApiId = Convert.ToInt32(datatable.Rows[0]["ApiId"]);
wGroupId = Convert.ToInt32(datatable.Rows[0]["ApiGroupId"]);
szApiName = datatable.Rows[0]["ApiName"].ToString();
szTempMsg = szTempMsg + szApiName;
szSQL = String.Format("SELECT Parameter.ParameterId, NoOfBytes, ParameterName, TypeName, Parameter.TypeId, DisplayHex, IsHidden " +
"FROM ApiParameter INNER JOIN (Parameter LEFT JOIN Type ON Parameter.TypeId = Type.TypeId) " +
"ON ApiParameter.ParameterId = Parameter.ParameterId " + "WHERE ApiParameter.ApiId = " + wApiId + " ORDER BY ApiParameter.Ordinal");
DataSet dataset1 = new DataSet();
OleDbCommand command1 = new OleDbCommand(szSQL, conn);
adapter.SelectCommand = command1;
adapter.Fill(dataset1);
//conn.Close();
int i = 0;
DataTable datatable1 = dataset1.Tables[0];
foreach (DataRow rows in datatable1.Rows)
{
//Console.Write(rows["ParameterId"] + "t" + rows["NoOfBytes"] +"t" + rows["ParameterName"] + "t" + rows["TypeName"] + "t" +rows["TypeId"] + "t" + rows["DisplayHex"]);
//Console.WriteLine();
bIsHidden = (bool)rows["IsHidden"];
wParamId = rows["ParameterId"];
szParamName = rows["ParameterName"];
szTypeName = rows["TypeName"];
wNoOfBytes = rows["NoOfBytes"];
wTypeId = rows["TypeId"];
bDisplayHex = rows["DisplayHex"];
bTryToDecode = 1;
szParamValueDec = "";
szParamValueHex = "";
if ((object)wTypeId == System.DBNull.Value)
{
wParamTypeId = 0;
}
else
{
wParamTypeId = (ushort)wTypeId;
}
if (true == isZCMsg)
{
string szByteValue;
if (wParamTypeId == 1)
{
byte[] pbyAliasLength = BitConverter.GetBytes(wAliasLength);
pbyAliasLength[1] = Convert.ToByte(fields);
i = i + 2;
pbyAliasLength[0] = Convert.ToByte(fields);
i = i + 2;
szParamValueDec = wAliasLength.ToString("D2");
szParamValueHex = wAliasLength.ToString("X2");
}
// alias
else if (wParamTypeId == 2)
{
szAlias = "";
for (int j = 1; j <= wAliasLength; j++)
{
szAlias = szAlias + fields;
i++;
}
bTryToDecode = 0;
}
else if (wParamTypeId == 6) // station control command variable length field
{
// this section will start with a 2 byte section that has the count of station control
// commands and then be followed by that number of pairs. the decoding will always
// hide the number of pairs and just decode the actual values
byte[] pbyStationControlCmdCount = BitConverter.GetBytes(wStationControlCmdCount);
pbyStationControlCmdCount[0] = Convert.ToByte(fields);
i++;
szParamValueHex = wStationControlCmdCount.ToString("X2");
szTempMsg = szTempMsg + (string)("tNumberOfStationCntrlCmds:0x") + szParamValueHex;
if (wStationControlCmdCount > 0)
{
// now read in the right number of pairs and add them to the message
for (ushort k = 1; k <= wStationControlCmdCount; k++)
{
// read in the station control command
szByteValue = string.Format("StationCntrlCmd%d:0x%02X", k, Convert.ToByte(fields));
szParamValueHex = szByteValue;
i = i + 2;
szByteValue = Convert.ToByte(fields).ToString("X2");
szParamValueHex = szParamValueHex + szByteValue;
i = i + 2;
szTempMsg = szTempMsg + (string)("t") + szParamValueHex;
// read in the station control arguement
szByteValue = string.Format("StationCntrlArg%d:0x%02X", k, Convert.ToByte(fields));
szParamValueHex = szByteValue;
i = i + 2;
szByteValue = Convert.ToByte(fields).ToString("X2");
szParamValueHex = szParamValueHex + szByteValue;
i = i + 2;
szTempMsg = szTempMsg + (string)("t") + szParamValueHex;
}
}
bIsHidden = true;
}
else
{
for (int j = 1; j <= (int)wNoOfBytes; j++)
{
szByteValue = (uint.Parse(fields.Substring(i, 2), System.Globalization.NumberStyles.HexNumber)).ToString("D2");
szParamValueDec = szParamValueDec + szByteValue;
szByteValue = uint.Parse(fields.Substring(i, 2), System.Globalization.NumberStyles.HexNumber).ToString("X2");
szParamValueHex = szParamValueHex + szByteValue;
i = i + 2;
}
}
}
else
{
switch ((int)wNoOfBytes)
{
case 1:
//byValue = Convert.ToByte(fields.Substring(i, 2));
byValue = UInt16.Parse(fields.Substring(i, 2), System.Globalization.NumberStyles.HexNumber);
i = i + 2;
szParamValueDec = byValue.ToString("D2");
szParamValueHex = byValue.ToString("X2");
break;
case 2:
string s1 = fields.Substring(i + 2, 2) + fields.Substring(i, 2);
wValue = UInt16.Parse(s1, System.Globalization.NumberStyles.HexNumber);
i = i + 4;
szParamValueDec = wValue.ToString("D4");
szParamValueHex = wValue.ToString("X4");
break;
case 4:
string dWord = fields.Substring(i + 6, 2) + fields.Substring(i + 4, 2) + fields.Substring(i + 2, 2) + fields.Substring(i, 2);
dwValue = uint.Parse(dWord, System.Globalization.NumberStyles.HexNumber);
i = i + 8;
szParamValueDec = dwValue.ToString("D8");
szParamValueHex = dwValue.ToString("X8");
break;
}
}
if (false == bIsHidden)
{
if (System.DBNull.Value != (object)szTypeName)
{
szTempMsg = szTempMsg + (string)("t") + (string)((string)szTypeName) + (string)(":");
}
else
{
szTempMsg = szTempMsg + (string)("t") + (string)((string)szParamName) + (string)(":");
}
if (bTryToDecode != 0)
{
szSQL = "";
if (szParamValueDec != "")
{
szSQL = "SELECT ValueName FROM [Value], ParameterValue "
+ "WHERE ParameterValue.ParameterId = " + wParamId + " AND ParameterValue.ValueId = [Value].ValueId "
+ "AND [Value].[Value] = " + szParamValueDec;
OleDbCommand command2 = new OleDbCommand(szSQL, conn);
DataSet dataset2 = new DataSet();
adapter.SelectCommand = command2;
adapter.Fill(dataset2);
conn.Close();
DataTable datatable2 = dataset2.Tables[0];
//foreach (DataRow rows1 in datatable2.Rows)
if (datatable2 == null)
{
bDecodeFailed = 0;
}
else
{
foreach (DataRow rows1 in datatable2.Rows)
{
szTempMsg = szTempMsg + rows1["ValueName"];
}
bDecodeFailed = 1;
}
}
}
else
{
bDecodeFailed = 1;
}
if (bDecodeFailed != 0)
{
// alias type
if (wParamTypeId == 2)
{
szTempMsg = szTempMsg + szAlias + "n";
}
else if (true == (bool)bDisplayHex)
{
szTempMsg = szTempMsg + " (0X" + szParamValueHex.ToString() + ") n";
}
else
{
szTempMsg = szTempMsg + "(0X" + szParamValueHex.ToString() + ")n";
}
}
}
/*lse
{
for (int k = 0; k < wNoOfBytes; k++)
{
i=i+2;
}
}*/
}
}
//Console.WriteLine(szTempMsg);
//Console.ReadLine();
conn.Close();
return szTempMsg;
}
catch(SyntaxErrorException)
{
}
return szTempMsg;
}
}
}
it will be very great full if some one suggest me how to multithread this code.
Girish
View the full article
here is the code to using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using System.Data.OleDb;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
label1.Text = openFileDialog1.FileName;
//textBox1.Text = File.ReadAllText(label1.Text);
}
}
private void button2_Click(object sender, EventArgs e)
{
}
private static string sample(string Message, string MessageId, string Payload)
{
if (Message == null)
{
Message = "";
return Message;
}
else
{
if (Message == "MumdAudioControlStatus" || Message == "MumdAudioControlRequest"
|| Message == "MumdAudioControlStatusMsg" || Message == "MumdAudioControlRequestMsg")
{
string payload = "http://ccpcweb.comm.mot.com/users/Tools/MsgMonitorInterpreter_Test/DecodeAmPackets.asp?SysType=";
return payload;
}
else
{
if (Message == "MumdTrunkingCallControlRequest" ||
Message == "MumdConventionalCallControlRequest" ||
Message == "MumdTrunkingCallControlStatus" ||
Message == "MumdConventionalCallControlStatus")
{
if (Message == "MumdTrunkingCallControlRequest" ||
Message == "MumdConventionalCallControlRequest")
{
String[] NewPayload = Payload.Split);
string sOpCode = NewPayload[1].Substring(60, 4);
string sPayload = NewPayload[1].Substring(65, NewPayload[1].Length - 65);
//string newPayload =
string newPayload = Decoder.Decoders(sOpCode, sPayload, true);
return newPayload;
}
else
{
String[] NewPayload = Payload.Split);
string sOpCode = NewPayload[0].Substring(65, 4);
int temp = NewPayload[0].Length - 70;
string sPayload = NewPayload[0].Substring(70, temp);
string newPayload = Decoder.Decoders(sOpCode, sPayload, true);
return newPayload;
}
}
else
{
if (Message != "MlogWrite")
{
String[] sOpCode = MessageId.Split);
String[] sPayload = Payload.Split);
string newPayload = Decoder.Decoders(sOpCode[1], sPayload[1], false);
return newPayload;
}
}
}
return Message;
}
}
private void button3_Click(object sender, EventArgs e)
{
if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
label2.Text = saveFileDialog1.FileName;
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
XmlWriter writer = XmlWriter.Create(label2.Text,settings);
writer.WriteStartDocument();
writer.WriteStartElement("Log_details");
string file = openFileDialog1.FileName;
string[] readText = File.ReadAllLines(file);
XElement root = new XElement("root");//create a root node
for(int i=0;i< readText.Length;i++)
{
if (!readText.Contains("#"))
{
string[] fields = readText.Split(t); //<-- change field separator here & split fields
for (int j=0;j<fields.Length;j++)
{
string[] value;
if (fields[j].Contains(":"))
{
value = fields[j].Split(new char[] { : }, 2);
string temp = value[0].ToString();
if (temp.Contains("""))
{
XElement field = new XElement(temp.Trim(new Char[] { " }), value[1].ToString().Trim());
field.WriteTo(writer);
}
else
{
if (temp == "Message")
{
XElement field = new XElement(value[0].ToString().Trim(), value[1].ToString().Trim());
field.WriteTo(writer);
if (value[1].ToString().Trim() != "MlogWrite")
{
writer.WriteStartElement("DecodedPayload");
string Decoded=sample(fields[7], fields[8], fields[9]);
string[] decodedFields = Decoded.Split(t);
for(int k=0;k<decodedFields.Length;k++)
{
string[] xmlNode;
if (decodedFields[k].Contains(":"))
{
xmlNode = decodedFields[k].Split(new char[] { : }, 2);
XElement childElement = new XElement(xmlNode[0].ToString().Trim(), xmlNode[1].ToString().Trim());
childElement.WriteTo(writer);
}
else
{
XElement childElement = new XElement("ApiName", decodedFields[k]);
childElement.WriteTo(writer);
}
}
writer.WriteEndElement();
}
}
else
{
XElement field = new XElement(value[0].ToString().Trim(), value[1].ToString().Trim());
field.WriteTo(writer);
}
}
}
else if (fields[j].Contains("-"))
{
value = fields[j].Split(-);
XElement field = new XElement(value[0].ToString().Trim(), value[1].ToString().Trim());
field.WriteTo(writer);
}
else
{
writer.WriteStartElement("Logs");
XElement field = new XElement("LogNo", fields[j].ToString().Trim());
field.WriteTo(writer);
}
}
writer.WriteEndElement();
}
}
writer.WriteEndElement(); //SAVED
writer.Close();
}
}
}
}
and decoder.cs contains calls to database.using System;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Collections.Generic;
namespace WindowsFormsApplication1
{
class Decoder
{
private static OleDbConnection GetConnection()
{
//connection
}
private static void CloseConnection(OleDbConnection conn)
{
//close
}
private static int ProcessHex(string str)
{
var strOut = "";
for (var i = 0; i < str.Length; i += 5)
{
strOut += str.Substring(i + 2, 2);
strOut += str.Substring(i, 2);
}
return Int32.Parse(strOut, System.Globalization.NumberStyles.HexNumber);
}
private static string Process(string str)
{
string strOut = "";
for (var i = 0; i < str.Length; i += 5)
{
strOut += str.Substring(i + 2, 2);
strOut += str.Substring(i, 2);
}
return strOut;
}
public static string Decoders(string opCode, string payLoad, bool isZCMsg)
{
String szSQL;
int szMsgId, wApiId, wGroupId;
bool bIsHidden;
dynamic wParamId, bDisplayHex, wNoOfBytes, szParamName, szTypeName, wTypeId;
int bTryToDecode = 1, bDecodeFailed = 1;
string szParamValueDec, szParamValueHex, szAlias = "", szTempMsg = "", szApiName;
ushort wParamTypeId = 0, wAliasLength = 0, wStationControlCmdCount = 0;
ushort byValue;
ushort wValue;
uint dwValue;
String[] field = payLoad.Split(.);
string fields = string.Join(String.Empty, field); // Null arguments are bad
try
{
if (true == isZCMsg)
{
szMsgId = Int16.Parse(opCode, System.Globalization.NumberStyles.HexNumber);
szSQL = "SELECT * FROM Api WHERE ApiDefineValue = " + szMsgId.ToString() + " AND ApiGroupId = 10";
}
else
{
szMsgId = ProcessHex(opCode);
szSQL = "SELECT * FROM Api WHERE ApiDefineValue = " + szMsgId.ToString() + " AND ( (ApiGroupId <> 10) and (ApiGroupId <> 22) )";
}
OleDbConnection conn = GetConnection();
DataSet dataset = new DataSet();
OleDbDataAdapter adapter = new OleDbDataAdapter();
OleDbCommand command = new OleDbCommand(szSQL, conn);
adapter.SelectCommand = command;
adapter.Fill(dataset);
//conn.Close();
DataTable datatable = dataset.Tables[0];
foreach (DataRow rows in datatable.Rows)
{
//Console.Write(rows["ApiId"].ToString() + "t" + rows["ApiName"] + "t" + rows["ApiGroupId"] + rows["ApiDefineValue"].ToString());
wApiId = Convert.ToInt32(rows["ApiId"]);
}
if (dataset.Tables[0].Rows.Count != 0)
{
wApiId = Convert.ToInt32(datatable.Rows[0]["ApiId"]);
wGroupId = Convert.ToInt32(datatable.Rows[0]["ApiGroupId"]);
szApiName = datatable.Rows[0]["ApiName"].ToString();
szTempMsg = szTempMsg + szApiName;
szSQL = String.Format("SELECT Parameter.ParameterId, NoOfBytes, ParameterName, TypeName, Parameter.TypeId, DisplayHex, IsHidden " +
"FROM ApiParameter INNER JOIN (Parameter LEFT JOIN Type ON Parameter.TypeId = Type.TypeId) " +
"ON ApiParameter.ParameterId = Parameter.ParameterId " + "WHERE ApiParameter.ApiId = " + wApiId + " ORDER BY ApiParameter.Ordinal");
DataSet dataset1 = new DataSet();
OleDbCommand command1 = new OleDbCommand(szSQL, conn);
adapter.SelectCommand = command1;
adapter.Fill(dataset1);
//conn.Close();
int i = 0;
DataTable datatable1 = dataset1.Tables[0];
foreach (DataRow rows in datatable1.Rows)
{
//Console.Write(rows["ParameterId"] + "t" + rows["NoOfBytes"] +"t" + rows["ParameterName"] + "t" + rows["TypeName"] + "t" +rows["TypeId"] + "t" + rows["DisplayHex"]);
//Console.WriteLine();
bIsHidden = (bool)rows["IsHidden"];
wParamId = rows["ParameterId"];
szParamName = rows["ParameterName"];
szTypeName = rows["TypeName"];
wNoOfBytes = rows["NoOfBytes"];
wTypeId = rows["TypeId"];
bDisplayHex = rows["DisplayHex"];
bTryToDecode = 1;
szParamValueDec = "";
szParamValueHex = "";
if ((object)wTypeId == System.DBNull.Value)
{
wParamTypeId = 0;
}
else
{
wParamTypeId = (ushort)wTypeId;
}
if (true == isZCMsg)
{
string szByteValue;
if (wParamTypeId == 1)
{
byte[] pbyAliasLength = BitConverter.GetBytes(wAliasLength);
pbyAliasLength[1] = Convert.ToByte(fields);
i = i + 2;
pbyAliasLength[0] = Convert.ToByte(fields);
i = i + 2;
szParamValueDec = wAliasLength.ToString("D2");
szParamValueHex = wAliasLength.ToString("X2");
}
// alias
else if (wParamTypeId == 2)
{
szAlias = "";
for (int j = 1; j <= wAliasLength; j++)
{
szAlias = szAlias + fields;
i++;
}
bTryToDecode = 0;
}
else if (wParamTypeId == 6) // station control command variable length field
{
// this section will start with a 2 byte section that has the count of station control
// commands and then be followed by that number of pairs. the decoding will always
// hide the number of pairs and just decode the actual values
byte[] pbyStationControlCmdCount = BitConverter.GetBytes(wStationControlCmdCount);
pbyStationControlCmdCount[0] = Convert.ToByte(fields);
i++;
szParamValueHex = wStationControlCmdCount.ToString("X2");
szTempMsg = szTempMsg + (string)("tNumberOfStationCntrlCmds:0x") + szParamValueHex;
if (wStationControlCmdCount > 0)
{
// now read in the right number of pairs and add them to the message
for (ushort k = 1; k <= wStationControlCmdCount; k++)
{
// read in the station control command
szByteValue = string.Format("StationCntrlCmd%d:0x%02X", k, Convert.ToByte(fields));
szParamValueHex = szByteValue;
i = i + 2;
szByteValue = Convert.ToByte(fields).ToString("X2");
szParamValueHex = szParamValueHex + szByteValue;
i = i + 2;
szTempMsg = szTempMsg + (string)("t") + szParamValueHex;
// read in the station control arguement
szByteValue = string.Format("StationCntrlArg%d:0x%02X", k, Convert.ToByte(fields));
szParamValueHex = szByteValue;
i = i + 2;
szByteValue = Convert.ToByte(fields).ToString("X2");
szParamValueHex = szParamValueHex + szByteValue;
i = i + 2;
szTempMsg = szTempMsg + (string)("t") + szParamValueHex;
}
}
bIsHidden = true;
}
else
{
for (int j = 1; j <= (int)wNoOfBytes; j++)
{
szByteValue = (uint.Parse(fields.Substring(i, 2), System.Globalization.NumberStyles.HexNumber)).ToString("D2");
szParamValueDec = szParamValueDec + szByteValue;
szByteValue = uint.Parse(fields.Substring(i, 2), System.Globalization.NumberStyles.HexNumber).ToString("X2");
szParamValueHex = szParamValueHex + szByteValue;
i = i + 2;
}
}
}
else
{
switch ((int)wNoOfBytes)
{
case 1:
//byValue = Convert.ToByte(fields.Substring(i, 2));
byValue = UInt16.Parse(fields.Substring(i, 2), System.Globalization.NumberStyles.HexNumber);
i = i + 2;
szParamValueDec = byValue.ToString("D2");
szParamValueHex = byValue.ToString("X2");
break;
case 2:
string s1 = fields.Substring(i + 2, 2) + fields.Substring(i, 2);
wValue = UInt16.Parse(s1, System.Globalization.NumberStyles.HexNumber);
i = i + 4;
szParamValueDec = wValue.ToString("D4");
szParamValueHex = wValue.ToString("X4");
break;
case 4:
string dWord = fields.Substring(i + 6, 2) + fields.Substring(i + 4, 2) + fields.Substring(i + 2, 2) + fields.Substring(i, 2);
dwValue = uint.Parse(dWord, System.Globalization.NumberStyles.HexNumber);
i = i + 8;
szParamValueDec = dwValue.ToString("D8");
szParamValueHex = dwValue.ToString("X8");
break;
}
}
if (false == bIsHidden)
{
if (System.DBNull.Value != (object)szTypeName)
{
szTempMsg = szTempMsg + (string)("t") + (string)((string)szTypeName) + (string)(":");
}
else
{
szTempMsg = szTempMsg + (string)("t") + (string)((string)szParamName) + (string)(":");
}
if (bTryToDecode != 0)
{
szSQL = "";
if (szParamValueDec != "")
{
szSQL = "SELECT ValueName FROM [Value], ParameterValue "
+ "WHERE ParameterValue.ParameterId = " + wParamId + " AND ParameterValue.ValueId = [Value].ValueId "
+ "AND [Value].[Value] = " + szParamValueDec;
OleDbCommand command2 = new OleDbCommand(szSQL, conn);
DataSet dataset2 = new DataSet();
adapter.SelectCommand = command2;
adapter.Fill(dataset2);
conn.Close();
DataTable datatable2 = dataset2.Tables[0];
//foreach (DataRow rows1 in datatable2.Rows)
if (datatable2 == null)
{
bDecodeFailed = 0;
}
else
{
foreach (DataRow rows1 in datatable2.Rows)
{
szTempMsg = szTempMsg + rows1["ValueName"];
}
bDecodeFailed = 1;
}
}
}
else
{
bDecodeFailed = 1;
}
if (bDecodeFailed != 0)
{
// alias type
if (wParamTypeId == 2)
{
szTempMsg = szTempMsg + szAlias + "n";
}
else if (true == (bool)bDisplayHex)
{
szTempMsg = szTempMsg + " (0X" + szParamValueHex.ToString() + ") n";
}
else
{
szTempMsg = szTempMsg + "(0X" + szParamValueHex.ToString() + ")n";
}
}
}
/*lse
{
for (int k = 0; k < wNoOfBytes; k++)
{
i=i+2;
}
}*/
}
}
//Console.WriteLine(szTempMsg);
//Console.ReadLine();
conn.Close();
return szTempMsg;
}
catch(SyntaxErrorException)
{
}
return szTempMsg;
}
}
}
it will be very great full if some one suggest me how to multithread this code.
Girish
View the full article