- Aug 16 Sun 2015 12:22
-
C# Dictionary<TKey, TValue>
- Aug 13 Thu 2015 12:00
-
C#多執行緒測試

Worker類別public class Worker
{
/* Initializes a new intance of the ManualRestEvent class
* with a Boolean value indicating whether to set the
* initial state signaled
*/
private ManualResetEvent m_pauseEvent = new ManualResetEvent(true);
private ManualResetEvent m_shutdownEvent = new ManualResetEvent(false);
private Thread m_thread;
private int m_cnt = 0;
private string m_status;
private Label m_lbl;
private TextBox m_tBox;
private ProgressBar m_pBar;
public int UserID { get; set; }
public string Status
{
get { return m_status; }
}
public int Count
{
get { return m_cnt; }
set { m_cnt = value; }
}
public Worker() { }
public Worker(Label lbl, TextBox tBox, ProgressBar pBar)
{
m_lbl = lbl;
m_tBox = tBox;
m_pBar = pBar;
}
#region Job
public void Job()
{
const int UPDATE_PERIOD = 10000;
while (true)
{
/* 若收到訊號, 則m_pauseEvent程式繼續往下執行
* 反之, 沒有收到訊號, 則持續等待(infinite),
* 程式不會往下執行(凍結)
*/
m_pauseEvent.WaitOne(Timeout.Infinite);
/* 若沒有收到訊號, 則一直等待訊號,
* 但因為timeout設定等待 0 ms, 自動跳開回傳false
* 反之, 收到訊號, 回傳true
*/
if(m_shutdownEvent.WaitOne(0))
break;
m_cnt++;
if (m_cnt % UPDATE_PERIOD == 0)
{
updateTextBox();
updateProgressBar();
}
}
}
#endregion
#region Start
public void Start()
{
m_thread = new Thread(Job);
m_thread.Start();
m_status = "執行緒開始執行...";
updateLabel();
}
#endregion
#region Pause
public void Pause()
{
// 將事件設定為未收到訊號, 會造成執行緒封鎖
m_pauseEvent.Reset();
m_status = "執行緒暫停";
updateLabel();
}
#endregion
#region Resume
public void Resume()
{
// 將事件設定為收到訊號, 會封鎖的執行緒繼續
m_pauseEvent.Set();
m_status = "執行緒繼續";
updateLabel();
}
#endregion
#region Stop
public void Stop()
{
/* m_shutdownEven事件收到訊號,
* 則(m_shutdownEvent.WaitOne(0))
* 回傳true
*/
m_shutdownEvent.Set();
/* 記得通知_pauseEvent, 即_pauseEvent.Set();
* 否則通知_shutdownEvent也枉然
*/
m_pauseEvent.Set();
/* 優雅地等執行緒結束,
* 即 呼叫_thread.Join();
* 而不是Thread.Terminate
*/
m_thread.Join();
m_status = "執行緒停止";
updateLabel();
}
#endregion
#region Reset Cnt
public void ResetCnt()
{
m_cnt = 0;
}
#endregion
#region Update UI
public void updateLabel()
{
if (m_lbl.InvokeRequired)
{
m_lbl.BeginInvoke((Action)(() => { updateLabel(); }));
}
else
{
m_lbl.Text = m_status;
}
}
public void updateTextBox()
{
if (m_tBox.InvokeRequired)
{
m_tBox.BeginInvoke((Action)(() => { updateTextBox(); }));
}
else
{
m_tBox.Text = m_cnt.ToString();
}
}
public void updateProgressBar()
{
if (m_pBar.InvokeRequired)
{
m_pBar.BeginInvoke((Action) (()=>{ updateProgressBar();}) );
}
else
{
m_pBar.Value = m_cnt;
}
}
#endregion
}
- Aug 13 Thu 2015 02:28
-
C# 如何創建, 暫停, 繼續, 終止一個執行緒(Thread)

今天來複習一下執行緒, 先前有一些觀念錯誤: 關於暫停/關閉執行緒Suspend, Terminate不該踩到地雷, 我一個都沒錯過@@ 1. 本範例示範如何開一個worker執行緒, 呼叫Start開始不斷執行Job內容, 且不影響原本的主執行緒2. 要暫停一個執行緒不建議使用Thread.Suspend, 這會讓你不曉得在你呼叫Suspend 當下該執行緒在幹甚麼;更準確地說, 你會不曉得worker在Job中已經 完成多少(停在Job中的哪個階段)3. 不要使用Terminate, 建議使用Join3. 透過ManualResetEvent搭配WaitOne 方法, 可以讓你更精準控制: 暫停, 繼續, 以及停止執行緒
- Aug 12 Wed 2015 10:17
-
安裝ActiViz.NET-5.8.0.607-win64-OpenSource
- Aug 11 Tue 2015 11:20
-
VTK/Examples/Cxx/Plotting/LinePlot

include目錄設定C:\VTK 5.6\VTK\Views;C:\VTK 5.6\VTK\Charts;C:\VTK 5.6\VTK\Widgets;C:\VTK 5.6\VTK\Rendering;C:\VTK 5.6\VTK\IO;C:\VTK 5.6\VTK\Imaging;C:\VTK 5.6\VTK\Graphics;C:\VTK 5.6\VTK\Geovis;C:\VTK 5.6\VTK\GenericFiltering;C:\VTK 5.6\VTK\Filtering;C:\VTK 5.6\VTK\Common;C:\VTK 5.6\release加入下列函式庫vtkCharts.lib
vtkViews.lib
vtkWidgets.lib
vtkHybrid.lib
vtkRendering.lib
vtkGraphics.lib
vtkImaging.lib
vtkIO.lib
vtkFiltering.lib
vtkCommon.lib
vtksys.lib
- Aug 11 Tue 2015 09:44
-
VTK/Examples/Cxx/IO/ReadSTL

將C:\VTK 5.6\vtkdata\Data\42400-IDGH.stl加入專案
1: // ReadSTLEx.cpp : 定義主控台應用程式的進入點。
2: //
3: 4: #include "stdafx.h"
5: #include <vtkPolyData.h>
6: #include <vtkSTLReader.h>
7: #include <vtkSmartPointer.h>
8: #include <vtkPolyDataMapper.h>
9: #include <vtkActor.h>
10: #include <vtkRenderWindow.h>
11: #include <vtkRenderer.h>
12: #include <vtkRenderWindowInteractor.h>
13: int _tmain(int argc, _TCHAR* argv[])
14: {15: std::string inputFilename = "42400-IDGH.stl";
16: 17: vtkSmartPointer<vtkSTLReader> reader = vtkSmartPointer<vtkSTLReader>::New(); 18: reader->SetFileName(inputFilename.c_str()); 19: reader->Update(); 20: 21: // Visualize
22: vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New(); 23: mapper->SetInputConnection(reader->GetOutputPort()); 24: 25: vtkSmartPointer<vtkActor> actor = 26: vtkSmartPointer<vtkActor>::New(); 27: actor->SetMapper(mapper); 28: 29: vtkSmartPointer<vtkRenderer> renderer = 30: vtkSmartPointer<vtkRenderer>::New(); 31: vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New(); 32: renderWindow->AddRenderer(renderer); 33: vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New(); 34: renderWindowInteractor->SetRenderWindow(renderWindow); 35: 36: renderer->AddActor(actor);37: renderer->SetBackground(.0, .0, .6); // Background color green
38: 39: renderWindow->Render(); 40: renderWindowInteractor->Start();41: return 0;
42: } 43:
- Aug 11 Tue 2015 04:08
-
VTK/Examples/Cxx/Rendering/Cylinder

1: renWin->SetSize(300, 200);C:\VTK 5.6\VTK\Widgets;C:\VTK 5.6\VTK\Rendering;C:\VTK 5.6\VTK\IO;C:\VTK 5.6\VTK\Imaging;C:\VTK 5.6\VTK\Graphics;C:\VTK 5.6\VTK\Geovis;C:\VTK 5.6\VTK\GenericFiltering;C:\VTK 5.6\VTK\Filtering;C:\VTK 5.6\VTK\Common;C:\VTK 5.6\release;C:\VTK 5.6\libvtkWidgets.libvtkHybrid.lib
vtkRendering.lib
vtkGraphics.lib
vtkImaging.lib
vtkIO.lib
vtkFiltering.lib
vtkCommon.lib
vtksys.lib
- Aug 08 Sat 2015 15:26
-
Emgu CV: 搜尋資料夾特定副檔名進行影像處理

在進行影像處理工作時, 經常需要批次處理, 處理的對象可能是某個目錄夾下的一序列影像 {0.bmp, 1.bmp, 2.bmp,….},可以存放在一個List<string>的容器內, 紀錄該目錄夾特定附檔名的檔案名稱
1: public static List<string> searchFileNames(string sDirPath, string fileExtension)
2: {3: List<string> list = new List<string>();
4: if (!Directory.Exists(sDirPath)) return null;
5: foreach (string filePath in System.IO.Directory.GetFiles(sDirPath, fileExtension))
6: { 7: list.Add(System.IO.Path.GetFileName(filePath)); 8: }9: return list;
10: }
- Aug 07 Fri 2015 12:46
-
執行緒如何更新windows form control?

之前有寫過一篇關於如何將執行緒運算結果顯示於人機介面上?, 今天剛好找到一個不錯的範例How to: Make Thread-Safe Calls to Windows Forms Controls, Let’s cut to the chase and get started!======================================================================Access to Windows Forms controls is not inherently thread safe. If you have two or more threads manipulating the state of a control, it is possible to force the control into an inconsistent state. Other thread-related bugs are possible as well, including race conditions and deadlocks. It is important to ensure that access to your controls is done in a thread-safe way.更新控制元件與threrad safe觀念: 如果有兩個執行緒, 一個UI(主執行緒), 另一個或更多個使用者開的執行緒要更新控制元件上(windows Form, TextBox, Label,…), 有可能造成不相容狀況
- Aug 06 Thu 2015 03:04
-
Emgu CV: BGR上下限篩選感興趣區域及BLOB運算

載入一張照片, 利用 InRange參數lower和higher顏色BGR上下限篩選感興趣區域, 得到一張遮罩m_img_inRange,再利用CvBlobDetector 進行Blob運算
1: using Emgu.CV;
2: using Emgu.CV.Cvb;
3: using Emgu.CV.CvEnum;
4: using Emgu.CV.Structure;
5: using Emgu.CV.VideoSurveillance;
6: using System.Diagnostics;
7: using System.Threading;
8: 9: private void CalInRange()
10: {11: if (m_img_color == null) return;
12: m_img_inRange = m_img_color.InRange(lower, higher); 13: m_img_inRange_not = m_img_inRange.Not();14: using (CvBlobs blobs = new CvBlobs())
15: { 16: m_blobDetector.Detect(m_img_inRange, blobs); 17: m_img_color_copy = m_img_color.Copy();18: foreach (var pair in blobs)
19: { 20: CvBlob b = pair.Value;21: CvInvoke.Rectangle(m_img_color_copy, b.BoundingBox, new MCvScalar(255.255, 255, 0), 5);
22: //CvInvoke.PutText(frame, blob.ID.ToString(), Point.Round(blob.Center), FontFace.HersheyPlain, 1.0, new MCvScalar(255.0, 255.0, 255.0));
23: } 24: 25: imageBox1.Image = m_img_color_copy; 26: imageBox2.Image = m_img_inRange; 27: imageBox3.Image = m_img_inRange_not; 28: } 29: }
- Aug 05 Wed 2015 18:56
-
Emgu CV: 計算線段內角(Interior Angle)與外角(Exterior Angle)

1. 利用Canny Method 直線偵測2. 利用Hough Transform(霍夫轉換)來找線段候選人3. 利用 GetExteriorAngleDegree 計算目前線段(line)與自訂義水平線(otherline)的夾角
1: private void CalExteriorAngles()
2: { 3: LineSegment2D[] lines = CvInvoke.HoughLinesP( 4: m_img_thresh_gauss.Canny(100, 64),5: 1, //Distance resolution in pixel-related units
6: Math.PI / 360.0, //Angle resolution measured in radians.
7: 90, //threshold
8: 140, //min Line width
9: 120); //gap between lines
10: 11: 12: Color[] colorArray = new Color[13] { Color.Red, Color.Green, Color.Blue, Color.Honeydew, Color.AliceBlue,
13: Color.AliceBlue, Color.Pink, Color.Indigo, Color.IndianRed, Color.GreenYellow, 14: Color.Lavender, Color.LemonChiffon, Color.LightBlue};15: Random r = new Random();
16: int ind;
17: //double ramp;
18: double theta = -1;
19: int cnt = 0;
20: m_img_copy = m_img_color.Copy(); 21: imageBox11.Image = m_img_copy; 22: listBox1.Items.Clear();23: LineSegment2D otherline = new LineSegment2D();
24: otherline.P1 = new Point(0, 0);
25: otherline.P2 = new Point(m_img_gray.Width, 0);
26: foreach (LineSegment2D line in lines)
27: { 28: ind = r.Next() % 11 +1; 29: theta = line.GetExteriorAngleDegree(otherline); 30: 31: 32: //this.Text = theta.ToString();
33: //if (theta > 0)
34: {35: m_img_copy.Draw(line, new Bgr(colorArray[ind]), 12);
36: imageBox11.Image = m_img_copy; 37: cnt++;38: listBox1.Items.Add(string.Format("#{0} Angle = {1:###.##}",cnt, theta));
39: //imageBox11.Image = m_img_copy;
40: Application.DoEvents(); 41: Thread.Sleep(2000);42: //if (cnt > 0) break;
43: } 44: 45: 46: 47: 48: } 49: imageBox11.Image = m_img_copy; 50: 51: //MessageBox.Show("Done");
52: }

