C# 最小化Windows中的任何窗口后,程序停止工作
该程序使用两个线程:一个用于从USB收集数据,另一个用于写入/显示数据。它似乎按预期工作,但是,只要我最小化/恢复电脑上的任何窗口,或按下窗体上的任何按钮,它就会停止正常工作(停止接收数据)。我使用链接列表来保持数据流的有序性。我怀疑可能是由于变量声明不当,链接丢失了。我的代码中是否有一个明显的错误可能导致这种情况C# 最小化Windows中的任何窗口后,程序停止工作,c#,C#,该程序使用两个线程:一个用于从USB收集数据,另一个用于写入/显示数据。它似乎按预期工作,但是,只要我最小化/恢复电脑上的任何窗口,或按下窗体上的任何按钮,它就会停止正常工作(停止接收数据)。我使用链接列表来保持数据流的有序性。我怀疑可能是由于变量声明不当,链接丢失了。我的代码中是否有一个明显的错误可能导致这种情况 namespace Streamer { public class Form1 : System.Windows.Forms.Form { Thre
namespace Streamer
{
public class Form1 : System.Windows.Forms.Form
{
Thread tListen;
Thread tDisplay;
public class MY_DATA_BUFFER
{
public byte[] buffer;
public int length;
public MY_DATA_BUFFER nextBuff;
}
private Object thisLock = new Object();
MY_DATA_BUFFER pHead = null;
private static AutoResetEvent DataQueueEvent = new AutoResetEvent(false);
public Form1()
{
// some settings;
}
public unsafe void dataDisplayThread()
{
MY_DATA_BUFFER pWorkingSet = pHead;
if (pWorkingSet == null)
{
// Make sure link list head is initialized...........
do {DataQueueEvent.WaitOne();} while (pHead == null);
// Wait till the two datas are available to write.
if (pHead.nextBuff == null){ DataQueueEvent.WaitOne(); }
if (pWorkingSet == null) { pWorkingSet = pHead; }
}
// Let's start the data write loop
while (bRunning || (pWorkingSet != null))
{
// copy small array to a bigger array
for (int i = 0; i < pWorkingSet.length; i++)
{
pixelValues[pixptr] = pWorkingSet.buffer[i];
pixptr++;
}
if (pixptr >= imWidth * imHeight)
{
pixptr = 0;
// show data in pixelValues
}
// Traverse through the link list data structure.
if (pWorkingSet.nextBuff == null)
{
do
{
if (pWorkingSet.nextBuff == null DataQueueEvent.WaitOne();
lock (thisLock)
{
if (pWorkingSet.nextBuff == null && !bRunning)
break;
}
} while (pWorkingSet.nextBuff == null);
}
// We are good to loop for the next operation
lock (thisLock)
{
pHead = pHead.nextBuff;
pWorkingSet = pHead;
}
}
// All write operation is at completion
pHead = null;
}
public unsafe void XferData(...)
{
MY_DATA_BUFFER tempBuff = null;
for (; bRunning; )
{
// .... do something
// push the data to a link list for
lock (thisLock)
{
MY_DATA_BUFFER newBuff = new MY_DATA_BUFFER();
newBuff.nextBuff = null;
newBuff.buffer = xBufs[k];
newBuff.length = len;
if (tempBuff == null) tempBuff = newBuff;
else
{
tempBuff.nextBuff = newBuff;
tempBuff = newBuff;
}
if (pHead == null) pHead = newBuff;
else
{
DataQueueEvent.Set();
}
}
///////////////////Link List Population completes///////////
Thread.Sleep(0);
} // End infinite loop
}
private void StartBtn_Click(object sender, System.EventArgs e)
{
if (StartBtn.Text.Equals("Start"))
{
// ...
pHead = null;
bRunning = true;
tListen = new Thread(new ThreadStart(XferThread));
tListen.IsBackground = true;
tListen.Priority = ThreadPriority.Highest;
tListen.Start();
tDisplay = new Thread(new ThreadStart(dataDisplayThread));
tDisplay.IsBackground = true;
tDisplay.Priority = ThreadPriority.Highest;
tDisplay.Start();
}
else
{
if (tListen.IsAlive)
{
// ...
bRunning = false;
if (tListen.Join(5000) == false )
tListen.Abort();
tListen = null;
}
if (tDisplay.IsAlive)
{
if (tDisplay.Join(5000) == false )
tDisplay.Abort();
Display = null;
}
}
}
}
}
名称空间拖缆
{
公共类Form1:System.Windows.Forms.Form
{
线程列表;
螺纹拉伸;
公共类我的\u数据\u缓冲区
{
公共字节[]缓冲区;
公共整数长度;
公共我的数据缓冲区下一个缓冲区;
}
私有对象thisLock=新对象();
MY_DATA_BUFFER pHead=null;
私有静态自动恢复事件DataQueueEvent=新自动恢复事件(false);
公共表格1()
{
//一些设置;
}
公共不安全的void dataDisplayThread()
{
MY_DATA_BUFFER pWorkingSet=pHead;
如果(pWorkingSet==null)
{
//确保链接列表头已初始化。。。。。。。。。。。
do{DataQueueEvent.WaitOne();}while(pHead==null);
//等待这两个数据可以写入。
如果(pHead.nextBuff==null){DataQueueEvent.WaitOne();}
如果(pWorkingSet==null){pWorkingSet=pHead;}
}
//让我们开始数据写入循环
while(bRunning | |(pWorkingSet!=null))
{
//将小数组复制到大数组
对于(int i=0;i=imWidth*imHeight)
{
pixptr=0;
//以像素值显示数据
}
//遍历链接列表数据结构。
if(pWorkingSet.nextBuff==null)
{
做
{
if(pWorkingSet.nextBuff==null DataQueueEvent.WaitOne();
锁(这个锁)
{
if(pWorkingSet.nextBuff==null&&!bRunning)
打破
}
}while(pWorkingSet.nextBuff==null);
}
//我们可以为下一个操作循环
锁(这个锁)
{
pHead=pHead.nextBuff;
pWorkingSet=pHead;
}
}
//所有写入操作都已完成
pHead=null;
}
公共不安全的void XferData(…)
{
MY_DATA_BUFFER tempBuff=null;
为了(;布鲁宁;)
{
//……做点什么
//将数据推送到一个链接列表,以便
锁(这个锁)
{
MY_DATA_BUFFER newBuff=新建MY_DATA_BUFFER();
newBuff.nextBuff=null;
newBuff.buffer=xBufs[k];
newBuff.length=len;
如果(tempBuff==null)tempBuff=newBuff;
其他的
{
tempBuff.nextBuff=newBuff;
tempBuff=newBuff;
}
如果(pHead==null)pHead=newBuff;
其他的
{
DataQueueEvent.Set();
}
}
///////////////////链接列表填充完成///////////
睡眠(0);
}//结束无限循环
}
私有无效开始单击(对象发送方,System.EventArgs e)
{
if(StartBtn.Text.Equals(“开始”))
{
// ...
pHead=null;
布伦宁=真;
tListen=新线程(新线程开始(XferThread));
tListen.IsBackground=true;
tListen.Priority=ThreadPriority.Highest;
tListen.Start();
tDisplay=新线程(新线程开始(dataDisplayThread));
tDisplay.IsBackground=true;
tDisplay.Priority=ThreadPriority.Highest;
tDisplay.Start();
}
其他的
{
如果(tListen.IsAlive)
{
// ...
布鲁宁=假;
if(tListen.Join(5000)=false)
tListen.Abort();
tListen=null;
}
如果(tDisplay.IsAlive)
{
if(tDisplay.Join(5000)=false)
tDisplay.Abort();
显示=空;
}
}
}
}
}
这个线程从何处开始?这些函数是如何调用的?您是否使用系统.窗口.窗体.计时器
?@RonBeyer刚刚在点击按钮时更新。我使用日期时间来计算数据速率。看起来您可能有资源争用,您的tListen
线程正在尽可能快地循环,即使“收益”在最后,它可能仍然占据着锁。lock
s不能解决资源争用,它只是让它变得更加明显。我会考虑使用某种并发集合,而不是滚动自己的集合。@RonBeyer你有一个例子说明在我的情况下可以做些什么吗?比如,h