在队列中的每个出队元素处启动一个线程 [英] starts one thread at every dequeue element from the queue
本文介绍了在队列中的每个出队元素处启动一个线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
在1毫秒及以上启动计时器.我想在队列中的每个出队元素处启动一个线程,以创建函数的新实例:
Hi All,
Starts the timer at 1ms & i want to start one thread at every dequeue element from the queue create the new instance of the function:
System.Threading.Timer timer1 ;
public void enqueue(object e)
{
lock (SyncObject)
{
DataChangeEventArgs e113 = (DataChangeEventArgs)e;
for (; i < e113.sts.Length; i++)
{
opctag.value = Convert.ToString(e113.sts[i].DataValue);
opctag.tagtimestamp = DateTime.FromFileTime(e113.sts[i].TimeStamp);
opctag.itemID = e113.sts[i].HandleClient;
opctag.quality = e113.sts[i].Quality;
int hour113 = e113.sts[i].TimeStampNet.Hour;
int minute113 = e113.sts[i].TimeStampNet.Minute;
int second113 = e113.sts[i].TimeStampNet.Second;
int millisecond113 = e113.sts[i].TimeStampNet.Millisecond;
int year113 = e113.sts[i].TimeStampNet.Year;
int month113 = e113.sts[i].TimeStampNet.Month;
int day113 = e113.sts[i].TimeStampNet.Day;
DateTime sdate113 = new DateTime(year113, month113, day113, hour113,minute113, second113, millisecond113);
opctag.filetimestamp = sdate113.ToString("dd-MM-yyyy HH:mm:ss.fff");
OPCitemQueue.Enqueue(opctag);
if(timer1==null)
{
TimerCallback call = new TimerCallback(dequeue);
TimeSpan dueTime = new TimeSpan(0, 0, 0, 0, 1);
TimeSpan interval = new TimeSpan(0, 0, 0, 0, 1);
timer1 = new System.Threading.Timer(call, null, dueTime, interval);
}
}
i = 0;
datachangehandler.X = 0;
}
}
public void dequeue(object sender)
{
try
{
lock (SyncObject1)
{
if (OPCitemQueue.Count != 0)
{
opctag = new opcservertags();
opctag = (opcservertags)OPCitemQueue.Dequeue();
th = new Thread(unused => update(opctag.value, opctag.filetimestamp, opctag.tagtimestamp, opctag.quality, opctag.itemID));
th.Start();
}
}
}
catch { }
}
public void update(string value,string filetimestamp,DateTime tagtimestamp,int quality,int itemID)
{
try
{
lock (myLockHolder)
{
X1 = 1;
OdbcConnection con = new OdbcConnection(LocalConnection.GetLocalConnetionString());
OdbcCommand cmd;
string query = "";
query = "update parameter" + Environment.NewLine;
query += "set paramvalue=''" + value + "'',date_logged1=''" + filetimestamp + "'',Quality=''" + quality + "'',date_logged=''" + tagtimestamp + "''" + Environment.NewLine;
query += " where itemID=''" + itemID + "''";
if (con.State == ConnectionState.Closed)
con.Open();
cmd = new OdbcCommand(query, con);
cmd.ExecuteNonQuery();
if (con.State == ConnectionState.Open)
con.Close(); X1 = 0;
}
}
catch { }
finally
{
X1 = 0;
}
}
我会这样写,以使元素出队的过程花费很多时间,但是,我想要快速出队元素&存储到MYSQL数据库中.
请帮我解决这个问题.
提前致谢.
I will be write like this process take to dequeue the element takes lots of time but, I want the fast dequeue element & stored into the MYSQL database.
Please help me in this reagrd.
Thanks in Advance.
推荐答案
昨天您提出此问题时, ^ ]一个完全明智的答案:为什么又要问同样的事情?
When you asked this question yesterday, dequeue the element one by one then stored this value to the MYSQL database[^] you got a perfectly sensible answer: why are you asking the same thing again?
您正在使用线程,这很好.我看不到您需要计时器.计时器是邪恶的.整个方法看起来是错误的.就是说,从以下内容开始:向您解释目标.不要将其与您的技术决策混为一谈.在没有太多如何"的情况下解释最终目标.
非常基本:仅使用一个线程,并使用Thread.Sleep
计时轮询.
-SA
You''re using threads, that''s good. I cannot see that you need a timer. Timers are evil. The whole approach looks wrong. That said, start with the following: explain you goal. Don''t mix it with your technical decisions. Explain the ultimate goal, without much of "how".
Very basically: use just a thread and time your polling usingThread.Sleep
.
—SA
使用 MySql Connector/Net [ ^ ]本机.NET驱动程序而不是MySql ODBC驱动程序.性能更高.
正如OriginalGriff所写,为什么要重新发布.您可以始终使用改善问题".
Use MySql Connector/Net[^] native .NET driver instead of MySql ODBC driver. There is more performance.
And as OriginalGriff write, why repost. You can alway use "Improve Question".
这篇关于在队列中的每个出队元素处启动一个线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文