Using windows 8.1 - Lazarus 1.8.4
From this project:
https://forum.lazarus.freepascal.org/index.php/topic,41618.0.htmlAlmost all the project is single thread. But a new thread is called to run the PoW if the user wants:
if ((OptionsData.Mining) and (not MINER_IsMinerOn) and (STATUS_Updated) and (not MINER_BlockFound) and (MINER_TargetHash = copy(LOCAL_LastBlockHash,1,MINER_TargetChars))) then
begin
OutputText('Mining. Target: '+MINER_TargetHash);
MINER_IsMinerOn := true;
Beginthread(tthreadfunc(@RunMiner));
end
This is the function:
function RunMiner():String;
var
HashValue : String = '';
begin
MINER_ResultHash := '';
if MINER_HashCounter = 2147483647 then
begin
MINER_HashCounter := 0;
MINER_HashSeed := GetHashSeed();
end;
while ((MINER_HashCounter < 2147483647) and (OptionsData.Mining) and (STATUS_Updated) and (MINER_IsMinerOn)) do
begin
HashValue := HashMD5String(MINER_HashSeed+IntToStr(MINER_HashCounter));
if AnsiContainsStr(HashValue,MINER_TargetHash) then
begin
MINER_ResultHash := MINER_HashSeed+IntToStr(MINER_HashCounter);
OutputText('Hash Found for '+MINER_TargetHash+' : '+MINER_ResultHash);
MINER_HashSeed := GetHashSeed();
MINER_HashCounter := 0;
MINER_BlockFound := true;
break;
end;
MINER_HashCounter := MINER_HashCounter+1;
end;
MINER_IsMinerOn := false;
OutputText('Miner is OFF');
end;
OptionsData.Mining,
STATUS_Updated and
MINER_IsMinerOn are global variables, so changing this values will end the function (and the thread).
Works well most of time, but sometimes the program hangs and windows closes it reporting a kernel.dll error. If the miner is not started, the program never had that issue (even managing multiple indy TCP connections).
Maybe i need a safer way to call the new thread?
The projects is open source, so feel free to check it.
NOTE: Outputtext do not modify the visual componnets. (forbidden in threads)
This is the function:
Procedure Outputtext(TextToShow:String;showhour:boolean=true);
Begin
if showhour then texttoshow := timetostr(now)+' '+TextToShow;
MainMemoStrings.Add(TextToShow);
End;