halcon图片上传到mysql_C# 10个线程并发执行Halcon图像算法 报“尝试读取或写入受保护的内存。这通常指示其他内存已损坏。”...
如題,這個問題本人已經糾結了快三個工作日了。本人不同WinFrom程序一起動就會開啟10個線程,并發對10張圖片進行算法處理,問題是只要程序一起動就會報“嘗試讀取或寫入受保護的內存。這通常指示其他內存已損壞。”異常。
本人試過將8個線程停掉,只開兩個,發現沒有問題,開三個四個偶爾會出問題….反正10個一個開是一定會報異常的。開起來好像是線程開太多CPU反應不過來導致的問題,可是CPU反應不過來就會報“嘗試讀取或寫入受保護的內存。這通常指示其他內存已損壞。”異常嗎?本人在網上查了查這種多線程并發報異常的問題很可能是線程與線程間有共享資源導致的問題,本人倒覺得沒有共享什么資源啊,各位高手幫本人看看本人的線程處理函數里面存在共享資源嗎?
線程1的線程處理函數:
private?void?AlgorithmHandlerThreadMethod1()
{
Bitmap?test1?=?new?Bitmap("E:\和路雪\和路雪?2014-7-09\TestControl\蛋筒\1.jpg");
ProjectCalculation?testHalcon?=?new?ProjectCalculation();
ConesInspectionHalcon01?ParameterIn?=?new?ConesInspectionHalcon01();
ConesInspectionHalcon01?ParameterOut?=?new?ConesInspectionHalcon01();
ParameterIn.AreaAddSet?=?50000;
ParameterIn.MaxAreaSet?=?350;
ParameterIn.MaxEdgeSet?=?150;
ParameterIn.AreaCutSet?=?40000;
while?(true)
{
if?(threadFlag)
{
long?start?=?DateTime.Now.Ticks;
ProjectCalculation.ConesInspectionHalcon01(test1,?ParameterIn,?out?ParameterOut);
Thread.Sleep(1000);
}
}
}
線程2的線程處理函數:
private?void?AlgorithmHandlerThreadMethod2()
{
Bitmap?test2?=?new?Bitmap("E:\和路雪\和路雪?2014-7-09\TestControl\蛋筒\2.jpg");
ProjectCalculation?testHalcon?=?new?ProjectCalculation();
ConesInspectionHalcon01?ParameterIn?=?new?ConesInspectionHalcon01();
ConesInspectionHalcon01?ParameterOut?=?new?ConesInspectionHalcon01();
ParameterIn.AreaAddSet?=?50000;
ParameterIn.MaxAreaSet?=?350;
ParameterIn.MaxEdgeSet?=?150;
ParameterIn.AreaCutSet?=?40000;
while?(true)
{
if?(threadFlag)
{
long?start?=?DateTime.Now.Ticks;
ProjectCalculation.ConesInspectionHalcon02(test2,?ParameterIn,?out?ParameterOut);
Thread.Sleep(1000);
}
}
}
線程3、4….10的線程處理函數一次類推。
ConesInspectionHalcon01函數:
public?static?void?ConesInspectionHalcon01(Bitmap?bitmap,?ConesInspectionHalcon01?ParameterIn,?out?ConesInspectionHalcon01?ParameterOut)
{
Bitmap?bm?=?(Bitmap)bitmap.Clone();
Stopwatch?sw?=?new?Stopwatch();
sw.Start();
HDevelopExport?he?=?new?HDevelopExport();
BitmapData?bd?=?bm.LockBits(new?Rectangle(0,?0,?bm.Width,?bm.Height),
ImageLockMode.ReadWrite,?PixelFormat.Format24bppRgb);
IntPtr?intp?=?bd.Scan0;
Bitmap?b1?=?new?Bitmap(bm.Width,?bm.Height,?PixelFormat.Format8bppIndexed);
Bitmap?b2?=?new?Bitmap(bm.Width,?bm.Height,?PixelFormat.Format8bppIndexed);
Bitmap?b3?=?new?Bitmap(bm.Width,?bm.Height,?PixelFormat.Format8bppIndexed);
BitmapData?bd1?=?b1.LockBits(new?Rectangle(0,?0,?bm.Width,?bm.Height),
ImageLockMode.ReadWrite,?PixelFormat.Format8bppIndexed);
BitmapData?bd2?=?b2.LockBits(new?Rectangle(0,?0,?bm.Width,?bm.Height),
ImageLockMode.ReadWrite,?PixelFormat.Format8bppIndexed);
BitmapData?bd3?=?b3.LockBits(new?Rectangle(0,?0,?bm.Width,?bm.Height),
ImageLockMode.ReadWrite,?PixelFormat.Format8bppIndexed);
IntPtr?intp1?=?bd1.Scan0;
IntPtr?intp2?=?bd2.Scan0;
IntPtr?intp3?=?bd3.Scan0;
unsafe
{
Work.Correction.RGBToChannel(intp,?intp1,?intp2,?intp3,?bm.Width,?bm.Height);
}
sw.Stop();
string?tmp?=?sw.ElapsedMilliseconds.ToString();
Console.WriteLine(tmp);
sw.Reset();
//?label2.Text?=?tmp;
bm.UnlockBits(bd);
HImage?h1?=?new?HImage();
HImage?h2?=?new?HImage();
HImage?h3?=?new?HImage();
HTuple?AreaCut,?MaxArea,?MaxEdge,?AreaAdd;
HTuple?hv_Gray,?hv_Deviation;
sw.Start();
h1.GenImage1("byte",?bm.Width,?bm.Height,?intp1);
h2.GenImage1("byte",?bm.Width,?bm.Height,?intp2);
h3.GenImage1("byte",?bm.Width,?bm.Height,?intp3);
sw.Stop();
tmp?=?sw.ElapsedMilliseconds.ToString();
Console.WriteLine(tmp);
sw.Reset();
try
{
sw.Start();
he.ScaleCircle(h1,?h2,?h3,?out?AreaAdd,?out?MaxArea,?out?MaxEdge,?out?AreaCut,?out?hv_Gray,?out?hv_Deviation);
sw.Stop();
tmp?=?sw.ElapsedMilliseconds.ToString();
Console.WriteLine(tmp);
sw.Reset();
}
catch?(Exception?ex)
{
AreaAdd?=?1000000;
MaxArea?=?1000000;
MaxEdge?=?1000000;
AreaCut?=?1000000;
hv_Gray?=?0;
hv_Deviation?=?0;
}
if?(hv_Gray.D?<=?30)
{
AreaAdd?=?1000000;
MaxArea?=?1000000;
MaxEdge?=?1000000;
AreaCut?=?1000000;
}
ParameterOut?=?new?ConesInspectionHalcon01();
//?ParameterOut=ParameterIn;
ParameterOut.CurrentAreaAdd?=?AreaAdd;
ParameterOut.CurrentAreaCut?=?AreaCut;
ParameterOut.CurrentMaxEdge?=?MaxEdge;
ParameterOut.CurrentMaxArea?=?MaxArea;
ParameterOut.hv_Gray?=?hv_Gray;
//????ParameterOut.hv_Deviation?=?hv_Deviation;
h1.Dispose();
h2.Dispose();
h3.Dispose();
b1.UnlockBits(bd1);
b2.UnlockBits(bd2);
b3.UnlockBits(bd3);
b1.Dispose();
b2.Dispose();
b3.Dispose();
if?(ParameterOut.CurrentAreaAdd?>?ParameterIn.AreaAddSet
||?ParameterOut.CurrentMaxArea?>?ParameterIn.MaxAreaSet
||?ParameterOut.CurrentMaxEdge?>?ParameterIn.MaxEdgeSet
||?ParameterOut.CurrentAreaCut?>?ParameterIn.AreaCutSet)
{
ParameterOut.Result?=?false;
ParameterOut.Index?=?10;
}
else
{
ParameterOut.Result?=?true;
ParameterOut.Index?=?11;
}
//ParameterOut.Index?=?ParameterIn.Index;
if?(bm?!=?null)?{?bm.Dispose();?}
}
ConesInspectionHalcon02
ConesInspectionHalcon03?…ConesInspectionHalcon10和ConesInspectionHalcon01內容是一樣的。
總結
以上是生活随笔為你收集整理的halcon图片上传到mysql_C# 10个线程并发执行Halcon图像算法 报“尝试读取或写入受保护的内存。这通常指示其他内存已损坏。”...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 合肥比亚迪第一辆整车下线:DM-i交车要
- 下一篇: mysql 磁盘组_有效管理 ASM 磁