Home > Back-end >  Multithreaded question (about the custom number of threads to handle N tasks) essence discussion (mu
Multithreaded question (about the custom number of threads to handle N tasks) essence discussion (mu

Time:10-02

For example:

Task number="memo1. Lines. The count - 1 (each row of a url (N))
Number of threads="edit1. Text (N)

Function: for each url title and return memo2. Lines. The add
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

Before my method is: according to the number of tasks directly create threads,

 For I:=0 to memo1. Lines. Do the count - 1 
The begin
Threadlist [I] :=getthread. Create (memo1, I);
end;


But found that the program is easy to appear problem, beginning of the progress bar soon (95%), but in the end is always waiting for a few threads, and a few threads easily can't return a result,

Question 1? How to make the thread to synchronous work? When a thread did not get the result set time (more than), all threads will wait after try again? Can't get again, will be quickly released,

Question 2? Such as task quantity, 39, define the number of threads 2, how to let two threads to the division of the number of single task?

More hope to have a code, to be able to share with you, thank you very much!

CodePudding user response:

This kind of problem has been discussed many times repeatedly, see more predecessors' achievements,
According to your need, AsyncCalls suit to you
http://delphi.about.com/od/kbthread/a/delphi-thread-pool-example-using-asynccalls.htm
Download demo and see inside,

CodePudding user response:

There's a thread pool to use summary, list a OminiThreadLibrary and AsyncCalls two thread pool,
http://delphi.about.com/od/kbthread/a/threaded-delphi-tasks-thread-pool-otl-omnithreadlibrary-example.htm

CodePudding user response:

Finish see this advanced goods, such as stun away,
Simple point, for thread and controls related,
Directly using PostMessage processing,
Use Windows message mechanism, to sync interface,
Also, don't you think, interface, is composed of a message, and you?

CodePudding user response:

{-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Author: Duo
Date: 2014/4/13 9:19:51
Note: a custom message, convert message into individual events,
SendMessage (WinHwnd, WinMsg wParam, lParam)
1. WinHwnd founded by class self, release,
2. WinMsg by providing the name of the generated,
3. The wParam said type,
4. The lParam parameter,

Can be used for screen display, and thread execution, the conflict between,
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -}
The unit uDuo. BindMessage;

Interface

USES SysUtils, Windows, Classes, Messages;

Type
TOnBindMessage=procedure (const AKind, AParam: Integer) of the object.

TBindMessage=class
Private
FMessageName: string;
FOnMessage: TOnBindMessage;
FWindowMessage: Cardinal;
FWindowHandle: THandle;
Protected
Procedure WndProc (var Msg: TMessage); Virtual;//if you want to get the Msg. The Result, covering method,
Public
The constructor Create (const AMessageNameOrWindowMessage: string);//will try, if it is digital, WindowMessage, otherwise according to the string, a custom WindowMessage,
The destructor Destroy; Override.
The function PostMsg (const AKind, AParam: Integer) : Boolean;
The function SendMsg (const AKind, AParam: Integer) : Integer;
The property MessageName: string read FMessageName;
The property WindowHandle: THandle read FWindowHandle;
The property WindowMessage: Cardinal read FWindowMessage write FWindowMessage;
The property OnMessage: TOnBindMessage read FOnMessage write FOnMessage;
end;

Implementation

{TBindMessage}

The constructor TBindMessage. Create (const AMessageNameOrWindowMessage: string).
The begin
FWindowHandle:=Classes. AllocateHWnd (WndProc);
If not TryStrToInt (AMessageNameOrWindowMessage, Integer (FWindowMessage)) then
The begin
FMessageName:=AMessageNameOrWindowMessage;
If FMessageName & lt;> "' then
FWindowMessage:=RegisterWindowMessage (PAnsiChar (FMessageName));
end;
end;

Destructor TBindMessage. Destroy;
The begin
Classes. DeallocateHWnd (FWindowHandle);
Inherited;
end;

The function TBindMessage. PostMsg (const AKind, AParam: Integer) : Boolean;
The begin
Result:=PostMessage (FWindowHandle FWindowMessage, AKind, AParam);
end;

The function TBindMessage. SendMsg (const AKind, AParam: Integer) : Integer;
The begin
Result:=SendMessage (FWindowHandle FWindowMessage, AKind, AParam);
end;

Procedure TBindMessage. WndProc (var Msg: TMessage);
The begin
If Msg, Msg=FWindowMessage then
The begin
If Assigned (FOnMessage) then
FOnMessage (Msg) WParam, Msg. LParam);
end;
end;

end.

CodePudding user response:

{-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Author: Duo
Date: 2014/5/13 16:49:52
Description: common writing log in TMemo, prevent interference of message, simply write a,
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -}
The unit uDuo. BindMemo;

Interface

USES StdCtrls, uDuo BindMessage, uDuo ListEx;

Type
TBindMemo=class
Private
FMemo: TMemo;
FBindMessage: TBindMessage;
FStringList: TLockedStringList;
Protected
Procedure DoBindMessage (const AKind, AParam: Integer);
The property Memo: TMemo read FMemo;
Public
The constructor Create (const AMemo: TMemo);
The destructor Destroy; Override.
The procedure to the Add (const AStr: string);
end;

Implementation

{TBindMemo}

Procedure TBindMemo. Add (const AStr: string).
The begin
FStringList. Add (AStr);
FBindMessage. PostMsg (0, 0);
end;

Constructor TBindMemo. Create (const AMemo: TMemo);
The begin
FMemo:=AMemo;
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related