使用threading.thread获得某些值后,执行停止 [英] execution stops after some value using threading.thread
本文介绍了使用threading.thread获得某些值后,执行停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Queue<opcdatastructure.opcservertags> OPCitemQueue = new Queue<opcdatastructure.opcservertags>();
private void DataChangeHandler(object sender, DataChangeEventArgs e)
{
try
{
var thstoredatatable = new System.Threading.Thread(producer);
thstoredatatable.Start(e);
}
catch { }
}
producer(object e)
{
lock (myLockHolder)
{
DataChangeEventArgs e113 = (DataChangeEventArgs)e;
int i ;
for (i=0; i < e113.sts.Length; i++)
{
opctag = new opcdatastructure.opcservertags();
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");
lock (OPCitemQueue)
{
OPCitemQueue.Enqueue(opctag);
Monitor.Pulse(OPCitemQueue);
}
consumer consumer = new consumer(OPCitemQueue);
while (threaddatatable == null)
{
threaddatatable = new System.Threading.Thread(ThreadRun);
threaddatatable.Start();
threaddatatable.Join();
}
}
public void ThreadRun()//excutes 8 times
{
try
{
lock (OPCitemQueue)
{
if (OPCitemQueue.Count == 0)
{
Monitor.Wait(OPCitemQueue);
}
opcdatastructure.opcservertags opctag = OPCitemQueue.Dequeue();
if (prameterupdate.X12 == 0)
{
prameterupdate.X12 = 1;
pu.update1(opctag.value, opctag.filetimestamp, opctag.tagtimestamp, opctag.quality, opctag.itemID);
}
}
}
catch { }
}</opcdatastructure.opcservertags></opcdatastructure.opcservertags>
但是在第9次循环之后,ThreadRun()停止执行,但是生产者将队列中的项目排队.为什么Threadrun()停止执行?
but after the 9th loop the ThreadRun() stop the execution but producer enqueue the items in the queue.why Threadrun()stop the execution?
推荐答案
AT乍一看:线程过程结束时,您有一个空的catch块.如果发生任何异常,则线程过程将终止,而没有任何进一步的问题迹象.即使在测试时,在地毯下扫除异常也不是一个好主意.如果抓住了它们,那么还必须处理它们.
AT first glance: You have an empty catch block at the end of the thread procedure. If any exception at all happens, the thread procedure will terminate without any further sign of a problem. Sweeping exceptions under the rug never is a good idea, even when testing. If you catch them, then you must also handle them.
这篇关于使用threading.thread获得某些值后,执行停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文