200分问个多线程序的问题!(200)

  • 主题发起人 主题发起人 Ekin
  • 开始时间 开始时间
E

Ekin

Unregistered / Unconfirmed
GUEST, unregistred user!
比如说界面上有N个按钮,每一个按钮下都有各自的事件任务,目前的方案是循环按钮逐一执行,这样非得等一个执行完了才能执行下一个,现在想通过多线程一步就搞定,之前没用过多线程,那个给个例子学习一下!!
 
delphi5开发人员指南
 
是要找多线程的例子还是想找解决这个问题的方法。如果仅仅是想找多线程例子,Delphi安装后里面有Demo(多线程比较三个排序方法)
 
每个按钮创建一个线程对公共资源进行同步处理
 
谁能根据题目给个例程啊~
 
上网上搜搜,这种例子很多的 找不到我发给你啊
 
没法帮你写代码 大概要用到的是 TButton(FindComponent('Button' + IntToStr(i))).... ......全局变量var flag: integer; IDBegin, IDEnd: integer;....线程内判断要用到的位运算and or ..... for i := 1 to n do MyThread := Txxx.create; for i := 1 to n do MyThread.Resume; //唤醒线程其实多线程 线程池 在数据量很大的情况下, 搞成队列本质也是要"等一个执行完了才能执行下一个".只不过是等几个执行完再执行下几个.
 
单CPU下,多线程最大的用处就是防止在处理阻塞过程时UI被挂起.楼主所言的情况和此情况有点类似,一点按钮就必须等到按钮事件返回后才能点其他按钮是吧.UNIT1.DFMobject Form1: TForm1 Left = 390 Top = 207 Width = 355 Height = 203 Caption = 'Form1' Color = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'MS Sans Serif' Font.Style = [] OldCreateOrder = False OnCreate = FormCreate PixelsPerInch = 96 TextHeight = 13 object LB3: TLabel Left = 0 Top = 163 Width = 347 Height = 13 Align = alBottom end object LB1: TLabel Left = 0 Top = 137 Width = 347 Height = 13 Align = alBottom end object LB2: TLabel Left = 0 Top = 150 Width = 347 Height = 13 Align = alBottom end object Button1: TButton Left = 12 Top = 14 Width = 75 Height = 25 Caption = 'Task1' TabOrder = 0 OnClick = DoBtn1 end object Button2: TButton Left = 114 Top = 16 Width = 75 Height = 25 Caption = 'Task2' TabOrder = 1 OnClick = DoBtn2 end object Button3: TButton Left = 210 Top = 16 Width = 75 Height = 25 Caption = 'Task3' TabOrder = 2 OnClick = DoBtn3 end object Button4: TButton Left = 120 Top = 78 Width = 165 Height = 25 Caption = 'InThread Batch Run All Task' TabOrder = 3 OnClick = Button4Click endend/////UNIT1.PASunit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TExecProc=procedure()of object; TSimpleAsync=class(TThread) private FProcList:TList; protected procedure Execute;override; public constructor Create(IniState:boolean); destructor Destroy;override; public procedure AddExecPoint(P:Pointer); end; TForm1 = class(TForm) Button1: TButton; Button2: TButton; Button3: TButton; Button4: TButton; LB3: TLabel; LB1: TLabel; LB2: TLabel; procedure DoBtn1(Sender: TObject); procedure DoBtn2(Sender: TObject); procedure DoBtn3(Sender: TObject); procedure FormCreate(Sender: TObject); procedure Button4Click(Sender: TObject); private { Private declarations } FSA:TSimpleAsync; public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}{ TSimpleAsync }procedure TSimpleAsync.AddExecPoint(P:Pointer);begin FProcList.Add(P);end;constructor TSimpleAsync.Create(IniState: boolean);begin inherited; FProcList:=TList.Create;end;destructor TSimpleAsync.Destroy;begin while FProcList.Count>0 do begin Dispose(FProcList.Items[0]); FProcList.Delete(0); end; inherited;end;procedure TSimpleAsync.Execute;begin while not Terminated do if (FProcList.Count>0) then try TExecProc(FProcList.Items[0]^)(); Dispose(FProcList.Items[0]); FProcList.Delete(0); except ; end else Suspend;end;procedure TForm1.DoBtn1(Sender: TObject);var I:Integer;begin for I:=1 to 100 do begin Lb1.Caption:=Format('task 1 running %d',); Sleep(10); end;end;procedure TForm1.DoBtn2(Sender: TObject);var I:Integer;begin for I:=1 to 100 do begin Lb2.Caption:=Format('task 2 running %d',); Sleep(10); end;end;procedure TForm1.DoBtn3(Sender: TObject);var I:Integer;begin for I:=1 to 100 do begin Lb3.Caption:=Format('task 3 running %d',); Sleep(10); end;end;type PMethod=^TMethod;procedure TForm1.FormCreate(Sender: TObject);begin FSA:=TSimpleAsync.Create(true);end;procedure TForm1.Button4Click(Sender: TObject);var p:PMethod;begin New(P); P.Data:=Self; P.Code:=@TForm1.DoBtn1; FSA.AddExecPoint(P); New(P); P.Data:=Self; P.Code:=@TForm1.DoBtn2; FSA.AddExecPoint(P); New(P); P.Data:=Self; P.Code:=@TForm1.DoBtn3; FSA.AddExecPoint(P); FSA.Resume;end;end.
 
谢谢各位的精彩回答!明天结贴放分,另,louemusic大虾能否留个QQ什么的,我还有些问题想请教一下你!!QQ:965060387
 
多人接受答案了。
 
后退
顶部