文档章节

多线程编程(18) - 再从一个小例子出发

涂孟超
 涂孟超
发布于 2014/09/26 15:32
字数 914
阅读 5
收藏 0

前面的例子都是让若干线程做同样的事情, 下面这个例子中的三个线程将分别在三个画板上随机画不同颜色的椭圆.
接下来的很多事情我想要基于这个例子来做.

本例效果图:



代码文件:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    PaintBox1: TPaintBox;
    PaintBox2: TPaintBox;
    PaintBox3: TPaintBox;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

var
  h1,h2,h3: THandle;

{第一个线程的入口函数: 画红色椭圆}
function ThreadFun1(p: Pointer): Integer; stdcall;
var
  i, x1,y1,x2,y2: Integer;
begin
  Form1.PaintBox1.Canvas.Brush.Color := clRed;
  for i := 0 to 50000 do with Form1.PaintBox1 do
  begin
    x1 := Random(Width); y1 := Random(Height);
    x2 := Random(Width); y2 := Random(Height);
    Canvas.Lock;
    Canvas.Ellipse(x1,y1,x2,y2);
    Canvas.Unlock;
    Sleep(0);
  end;
  Result := 0;
end;

{第二个线程的入口函数: 画绿色椭圆}
function ThreadFun2(p: Pointer): Integer; stdcall;
var
  i, x1,y1,x2,y2: Integer;
begin
  Form1.PaintBox2.Canvas.Brush.Color := clGreen;
  for i := 0 to 50000 do with Form1.PaintBox2 do
  begin
    x1 := Random(Width); y1 := Random(Height);
    x2 := Random(Width); y2 := Random(Height);
    Canvas.Lock;
    Canvas.Ellipse(x1,y1,x2,y2);
    Canvas.Unlock;
    Sleep(0);
  end;
  Result := 0;
end;

{第三个线程的入口函数: 画蓝色椭圆}
function ThreadFun3(p: Pointer): Integer; stdcall;
var
  i, x1,y1,x2,y2: Integer;
begin
  Form1.PaintBox3.Canvas.Brush.Color := clBlue;
  for i := 0 to 50000 do with Form1.PaintBox3 do
  begin
    x1 := Random(Width); y1 := Random(Height);
    x2 := Random(Width); y2 := Random(Height);
    Canvas.Lock;
    Canvas.Ellipse(x1,y1,x2,y2);
    Canvas.Unlock;
    Sleep(0);
  end;
  Result := 0;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  ID: DWORD;
begin
  h1 := CreateThread(nil, 0, @ThreadFun1, nil, 0, ID);
  h2 := CreateThread(nil, 0, @ThreadFun2, nil, 0, ID);
  h3 := CreateThread(nil, 0, @ThreadFun3, nil, 0, ID);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  CloseHandle(h1);
  CloseHandle(h2);
  CloseHandle(h3);
end;

end.

 
 
 
 
 

 

 

  

窗体文件:
object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 206
  ClientWidth = 371
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnDestroy = FormDestroy
  PixelsPerInch = 96
  TextHeight = 13
  object PaintBox1: TPaintBox
    Left = 8
    Top = 8
    Width = 114
    Height = 153
  end
  object PaintBox2: TPaintBox
    Left = 128
    Top = 8
    Width = 114
    Height = 153
  end
  object PaintBox3: TPaintBox
    Left = 248
    Top = 8
    Width = 114
    Height = 153
  end
  object Button1: TButton
    Left = 288
    Top = 172
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 0
    OnClick = Button1Click
  end
end

 
 
 
 
 

 

 

  

可以借助入口函数的参数, 把这个程序简化一下(窗体和运行效果不变):
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    PaintBox1: TPaintBox;
    PaintBox2: TPaintBox;
    PaintBox3: TPaintBox;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

var
  h1,h2,h3: THandle;

{线程的入口函数: 不同的线程画不同颜色的椭圆}
function ThreadFun(p: Pointer): Integer; stdcall;
var
  i,x1,y1,x2,y2: Integer;
  paint: TPaintBox;
begin
  case Integer(p) of
    1: begin
      paint := Form1.PaintBox1;
      paint.Canvas.Brush.Color := clRed;
    end;
    2: begin
      paint := Form1.PaintBox2;
      paint.Canvas.Brush.Color := clGreen
    end;
    3: begin
      paint := Form1.PaintBox3;
      paint.Canvas.Brush.Color := clBlue;
    end;
  end;

  for i := 0 to 5000 do with paint do
  begin
    x1 := Random(Width); y1 := Random(Height);
    x2 := Random(Width); y2 := Random(Height);
    Canvas.Lock;
    Canvas.Ellipse(x1,y1,x2,y2);
    Canvas.Unlock;
    Sleep(0);
  end;
  Result := 0;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  ID: DWORD;
begin
  h1 := CreateThread(nil, 0, @ThreadFun, Ptr(1), 0, ID);
  h2 := CreateThread(nil, 0, @ThreadFun, Ptr(2), 0, ID);
  h3 := CreateThread(nil, 0, @ThreadFun, Ptr(3), 0, ID);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  CloseHandle(h1);
  CloseHandle(h2);
  CloseHandle(h3);
end;

end.

 
 
 
 
 

 

 

  

多用点数组, 再简化一下(窗体与效果一样):
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    PaintBox1: TPaintBox;
    PaintBox2: TPaintBox;
    PaintBox3: TPaintBox;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

const
  colors: array[0..2] of TColor = (clRed, clGreen, clBlue);
var
  hArr: array[0..2] of THandle;
  panitArr: array[0..2] of TPaintBox;

function ThreadFun(p: Pointer): Integer; stdcall;
var
  i,n,x1,y1,x2,y2: Integer;
begin
  n := Integer(p);
  panitArr[n].Color := colors[n];

  for i := 0 to 5000 do with panitArr[n] do
  begin
    x1 := Random(Width); y1 := Random(Height);
    x2 := Random(Width); y2 := Random(Height);
    Canvas.Lock;
    Canvas.Ellipse(x1,y1,x2,y2);
    Canvas.Unlock;
    Sleep(0);
  end;
  Result := 0;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  ID: DWORD;
  i: Integer;
begin
  panitArr[0] := PaintBox1;
  panitArr[1] := PaintBox2;
  panitArr[2] := PaintBox3;
  for i := 0 to Length(hArr) - 1 do
    hArr[i] := CreateThread(nil, 0, @ThreadFun, Ptr(i), 0, ID);
end;

procedure TForm1.FormDestroy(Sender: TObject);
var
  i: Integer;
begin
  for i := 0 to Length(hArr) - 1 do CloseHandle(hArr[i]);
end;

end.

 
 
 
 
 

 

 

  

本文转载自:http://www.cnblogs.com/del/archive/2009/02/19/1394317.html

涂孟超
粉丝 12
博文 2011
码字总数 14107
作品 0
深圳
程序员
私信 提问
python浓缩(18)多线程编程

线程和进程 线程和 Python thread 模块 threading 模块 生产者-消费者问题和 Queue 模块 相关模块 本节将探索用多线程编程技术实现代码并行性的几种不同的方法。进程与线程的区别->多线程编程...

人生如梦19
2016/04/25
74
0
java学习计划

源于传智播客毕向东老师的教学计划: day01-01-基本常识 day01-02-Java的跨平台性 day01-03-Java环境搭建(安装) day01-04-Java环境搭建(环境变量配置) day01-05-Java环境搭建(环境变量配置技...

Bony
2016/05/14
64
0
Python核心编程:第18章 多线程编程

18.1 引言/动机 1.多线程编程对于某些任务是最理想的。这些任务具有以下特点:它们本质就是异步的,需要有多个并发事务,各个事务的运行顺序可以是不确定的、随机的、 不可预测的。这样的编程...

finndai
2016/12/08
49
0
介绍一个基于 Java 的并发编程语言 —— JR

JR 是一种专为解决并行编程问题的编程语言。JR 是在 Java 语言的基础上增加了并行编程(例如进程和信号量等)概念的一个新的语言。 JR 经常在学校中用来教授并行编程课程。 在本文中我们主要...

红薯
2010/06/28
1K
3
Linux多线程并发服务器编程(线程池,FTP服务器)

分享网盘下载:https://pan.baidu.com/s/1gfNCcXt 密码: irfk 内容简介 本课程从最基础的进程、线程概念讲起逐步深入,通过理论与实践结合的方式,使学员快说掌握linux多线程网络编程技术,并...

人气王子333
2018/06/26
0
0

没有更多内容

加载失败,请刷新页面

加载更多

Nginx 快速安装详解

一、Nginx Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。Nginx是由伊戈尔·赛索耶夫为俄罗斯访问量第二的Rambler.ru站点(俄文:Рамбле...

网络小虾米
13分钟前
4
0
技术分享 | slave_relay_log_info 表认知的一些展开

作者:胡呈清 slave_relay_log_info 表是这样的: mysql> select * from mysql.slave_relay_log_info\G *************************** 1. row *************************** Number_of_lin......

爱可生
15分钟前
3
0
nginx配置http访问自动跳转到https

server {listen 80;server_name www.域名.com;rewrite ^(.*) https://$server_name$1 permanent;}server {listen 443;server_name www.域名.com;root /home/www;ssl on;......

很好亦平凡ms
15分钟前
2
0
SpreadJS:一款中国研发的类Excel开发工具,功能涵盖Excel的 95% 以上

Excel 作为一款深受用户喜爱的电子表格工具,借助其直观的界面、出色的计算性能、数据分析和图表,已经成为数据统计领域不可或缺的软件之一。 基于Excel对数据处理与分析的卓越表现,把Excel...

葡萄城技术团队
15分钟前
2
0
用javafx框架tornadofx做了个天气预报的程序

class WeatherApp : App(WeatherView::class)class WeatherView : View("十五天天气预报") { val weatherVM: WeatherViewModel by inject() val controller: WeatherController by......

oschina4cyy
19分钟前
3
1

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部