private void DataChangeHandler(object sender, DataChangeEventArgs e)
{
try
{ update3(e);
}
catch { }
}
public void update3(object h)
{
try
{
if (h != null)
{
DataChangeEventArgs e = (DataChangeEventArgs)h;
OdbcConnection con128 = new OdbcConnection(LocalConnection.GetLocalConnetionString());
int counter = 0;
OdbcCommand cmd;
string UpdateQuery = "";
string query1 = "";
while (counter < e.sts.Length)
{
object val = e.sts[counter].DataValue;
int hour = e.sts[counter].TimeStampNet.Hour;
int minute = e.sts[counter].TimeStampNet.Minute;
int second = e.sts[counter].TimeStampNet.Second;
int millisecond = e.sts[counter].TimeStampNet.Millisecond;
int year = e.sts[counter].TimeStampNet.Year;
int month = e.sts[counter].TimeStampNet.Month;
int day = e.sts[counter].TimeStampNet.Day;
DateTime sdate = new DateTime(year, month, day, hour, minute, second, millisecond);
string date = sdate.ToString("dd-MM-yyyy HH:mm:ss.fff");
DateTime dt = DateTime.FromFileTime(e.sts[counter].TimeStamp);
query1 += "select '" + val + "' as DTvalue ,'" + date + "' as DTdatelogged1,'" + OpcGroup.QualityToString(e.sts[counter].Quality) + "' as DTquality ,'" + dt + "' as DTtimestamp ,'" + e.sts[counter].HandleClient + "' as DTparamID Union " + Environment.NewLine;
counter++;
}
query1 = query1.Remove(query1.LastIndexOf("Union"));
UpdateQuery = "Update parameter t Left join " + Environment.NewLine;
UpdateQuery += " ( " + query1 + " ) Temp on" + Environment.NewLine;
UpdateQuery += "t.itemID=Temp.DTparamID" + Environment.NewLine;
UpdateQuery += "set paramvalue=DTvalue,date_logged1=DTdatelogged1,Quality=DTquality,date_logged=DTtimestamp " + Environment.NewLine;
UpdateQuery += "where t.itemID=Temp.DTparamID ";
if (con128.State == ConnectionState.Closed)
con128.Open();
cmd = new OdbcCommand(UpdateQuery, con128);
cmd.ExecuteNonQuery();
if ((con128.State == ConnectionState.Connecting) || (con128.State == ConnectionState.Open))
{
con128.Close();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
数据更改处理程序每隔一秒调用一次由OPC服务器完成,如计时器
答案 0 :(得分:4)
您应该保持对所创建内容的控制和跟踪,而不是创建大量线程。例如,使用一个线程池,甚至更好地使用任务并行库。
第二个想法是你不应该尝试从OOM异常中恢复,你应该事先做好一切,以便首先避免它。
答案 1 :(得分:1)
我非常怀疑任何人都能够在没有源代码的情况下为您提供“正确”的答案。我能做的最好的就是提供一些通用指针:
答案 2 :(得分:0)
现在您已经发布了一些代码:
代码不会显示您提到的许多线程的创建,但有几点需要注意:
OdbcCommand
和OdbcConnection
都是IDisposable
,因此应将其包含在using
中,以确保正确释放所有(未管理的)资源。string.Format()
而不是连接来避免创建许多临时字符串。我怀疑这确实是一个问题,但这种方式更清洁。除此之外,您的代码看起来不包含任何内存泄漏。