17,748
社区成员




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CPUParallel
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
long frequency = 0;//计时用的时钟频率
short[] isFinish = new short[10];
long[] time = new long[50];
private Stopwatch watch = new Stopwatch();
private Thread[] MyThread = new Thread[4];
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern bool QueryPerformanceFrequency(ref long performanceFrequency);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern bool QueryPerformanceCounter(ref long performanceCount);
[DllImport("kernel32.dll")]
public static extern uint GetLastError();
//获取系统运行时间毫秒级别
[DllImport("kernel32.dll")]
static extern uint GetTickCount();
//SetThreadAffinityMask 指定hThread 运行在 核心 dwThreadAffinityMask
[DllImport("kernel32.dll")]
static extern UIntPtr SetThreadAffinityMask(IntPtr hThread, UIntPtr dwThreadAffinityMask);
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr GetCurrentProcess();
[DllImport("kernel32.dll", SetLastError = true)]
public static extern int GetCurrentProcessorNumber();
//得到当前线程的handler
[DllImport("kernel32.dll")]
static extern IntPtr GetCurrentThread();
[DllImport("kernel32.dll")]
static extern uint GetCurrentThreadId();
private void CheckMyThreadIsFinish(object obj)
{
bool loop = true;
while (loop)
{
Thread.Sleep(0);
if ((isFinish[0] == 1) && (isFinish[1] == 1))
{
QueryPerformanceCounter(ref time[10]);
double totalTime = Math.Round(1000.0 * (time[10] - time[15]) / frequency, 1);
Trace.WriteLine("最终检测完成时间:" + totalTime + " 时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"));
isFinish[0] = 0;
isFinish[1] = 0;
}
}
}
private short FindShapeModel()
{
//截取当前窗体图像
int Width = 1088;
int Height = 686;
int ClientSizeWidth = 1072;
int ClientSizeHeight = 647;
for (int i = 0; i < 40; i++)
{
Bitmap bitFormImg = new Bitmap(Width, Height);
//this.DrawToBitmap(bitFormImg, new Rectangle(0, 0, Width, Height));
int border = (Width - ClientSizeWidth) / 2;//边框宽度
int caption = (Height - ClientSizeHeight) - border;//标题栏高度
//截取签字区域图像(去边框、标题栏)
Bitmap bitSignatureImg = bitFormImg.Clone(new Rectangle(border, caption, ClientSize.Width, ClientSize.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb);
//创建内存流
MemoryStream Ms = new MemoryStream();
//将图像保存至内存流
bitSignatureImg.Save(Ms, ImageFormat.Bmp);
bitFormImg.Dispose();
bitSignatureImg.Dispose();
byte[] img = new byte[Ms.Length];
Ms.Position = 0;
Ms.Read(img, 0, Convert.ToInt32(Ms.Length));
Ms.Close();
string result = Convert.ToBase64String(img);
int COUNT = GetCurrentProcessorNumber();
//Trace.WriteLine("ID "+i+" FindShapeModel0:" + "CPUID: " + COUNT + " 时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"));
Trace.WriteLine("ID " + i + " FindShapeModel0:" + "CPUID: " + COUNT + " 时间:" + Math.Round(watch.Elapsed.TotalMilliseconds, 3));
}
return 1;
}
private short FindShapeModel1()
{
//截取当前窗体图像
int Width = 1088;
int Height = 686;
int ClientSizeWidth = 1072;
int ClientSizeHeight = 647;
for (int i = 0; i < 40; i++)
{
Bitmap bitFormImg = new Bitmap(Width, Height);
//this.DrawToBitmap(bitFormImg, new Rectangle(0, 0, Width, Height));
int border = (Width - ClientSizeWidth) / 2;//边框宽度
int caption = (Height - ClientSizeHeight) - border;//标题栏高度
//截取签字区域图像(去边框、标题栏)
Bitmap bitSignatureImg = bitFormImg.Clone(new Rectangle(border, caption, ClientSize.Width, ClientSize.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb);
//创建内存流
MemoryStream Ms = new MemoryStream();
//将图像保存至内存流
bitSignatureImg.Save(Ms, ImageFormat.Bmp);
bitFormImg.Dispose();
bitSignatureImg.Dispose();
byte[] img = new byte[Ms.Length];
Ms.Position = 0;
Ms.Read(img, 0, Convert.ToInt32(Ms.Length));
Ms.Close();
string result = Convert.ToBase64String(img);
int COUNT = GetCurrentProcessorNumber();
//Trace.WriteLine("ID "+i+" FindShapeModel0:" + "CPUID: " + COUNT + " 时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"));
Trace.WriteLine("ID " + i + " FindShapeModel1:" + "CPUID: " + COUNT + " 时间:" + Math.Round(watch.Elapsed.TotalMilliseconds, 3));
}
return 1;
}
private void FindShapeModel(object obj)
{
Thread.BeginThreadAffinity();
IntPtr handle = GetCurrentThread();
UIntPtr rtn = SetThreadAffinityMask(handle, new UIntPtr(0x0001));
uint rtn1 = GetLastError();
isFinish[0] = FindShapeModel();
Thread.EndThreadAffinity();
}
private void FindShapeModel1(object obj)
{
Thread.BeginThreadAffinity();
IntPtr handle = GetCurrentThread();
UIntPtr rtn = SetThreadAffinityMask(handle, new UIntPtr(64));
uint rtn1 = GetLastError();
isFinish[1] = FindShapeModel1();
Thread.EndThreadAffinity();
}
private void FindShapeModel2(object obj)
{
QueryPerformanceCounter(ref time[25]);
Thread.BeginThreadAffinity();
IntPtr handle = GetCurrentThread();
UIntPtr rtn = SetThreadAffinityMask(handle, new UIntPtr(64));
uint rtn1 = GetLastError();
isFinish[1] = FindShapeModel();
Thread.EndThreadAffinity();
QueryPerformanceCounter(ref time[26]);
double totalTime = Math.Round(1000.0 * (time[26] - time[25]) / frequency, 1);
Trace.WriteLine("直接调用FindShapeModel()方法:" + totalTime + " 时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff"));
}
private void Form1_Load(object sender, EventArgs e)
{
QueryPerformanceFrequency(ref frequency);
Thread thread = new Thread(CheckMyThreadIsFinish);
int id = thread.ManagedThreadId;
thread.Start();
}
private void button1_Click(object sender, EventArgs e)
{
object obj = null;
watch.Restart();
QueryPerformanceCounter(ref time[15]);
isFinish[0] = 0;
isFinish[1] = 0;
MyThread[0] = new Thread(new ParameterizedThreadStart(FindShapeModel));
MyThread[0].Start(obj);
MyThread[1] = new Thread(new ParameterizedThreadStart(FindShapeModel1));
MyThread[1].Start(obj);
}
private void button2_Click(object sender, EventArgs e)
{
watch.Restart();
QueryPerformanceCounter(ref time[25]);
MyThread[0] = new Thread(new ParameterizedThreadStart(FindShapeModel2));
MyThread[0].Start(null);
}
}
}
Bitmap bitFormImg = new Bitmap(Width, Height);
// 大概要2.7兆内存。
Bitmap bitSignatureImg = bitFormImg.Clone(new Rectangle(border, caption, ClientSize.Width, ClientSize.Height), System.Drawing.Imaging.PixelFormat.Format24bppRgb);
// 大概要2.7兆内存。
MemoryStream Ms = new MemoryStream();
bitSignatureImg.Save(Ms, ImageFormat.Bmp);
// 大概要2.7兆内存。
byte[] img = new byte[Ms.Length];
Ms.Read(img, 0, Convert.ToInt32(Ms.Length));
// 大概要2.7兆内存。
string result = Convert.ToBase64String(img);
// 大概要8兆内存。