Форум по Delphi программированию

Delphi Sources



Вернуться   Форум по Delphi программированию > Все о Delphi > [ "Начинающим" ]
Ник
Пароль
Регистрация <<         Правила форума         >> FAQ Пользователи Календарь Поиск Сообщения за сегодня Все разделы прочитаны

Ответ
 
Опции темы Поиск в этой теме Опции просмотра
  #1  
Старый 12.02.2014, 22:53
alyamus alyamus вне форума
Прохожий
 
Регистрация: 21.05.2012
Сообщения: 28
Репутация: 10
По умолчанию Thread error handle invalid (6) ?

Hi Friedns , What is this handle is invalid error (6). , I press the button suspended , but fails after 30 seconds.

Код:
unit Unit1;
 
 
interface
 
 
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, ExtCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdHTTP, IdAntiFreezeBase, IdAntiFreeze, AdvSmoothProgressBar,
  BusinessSkinForm, bsSkinData, bsSkinCtrls;
 
 
type
  TForm1 = class(TForm)
    Button4: TButton;
    IdAntiFreeze1: TIdAntiFreeze;
    Label13: TLabel;
    Label12: TLabel;
    Label11: TLabel;
    AdvProgressBar1: TAdvSmoothProgressBar;
    bsSkinData1: TbsSkinData;
    bsCompressedStoredSkin1: TbsCompressedStoredSkin;
    bsBusinessSkinForm1: TbsBusinessSkinForm;
    Button1: TbsSkinSpeedButton;
    Button2: TbsSkinSpeedButton;
    button3: TbsSkinSpeedButton;
    IdHTTP: TIdHTTP;
    procedure Button4Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure IdHTTPWork(Sender: TObject; AWorkMode: TWorkMode;
      const AWorkCount: Integer);
    procedure IdHTTPWorkBegin(Sender: TObject; AWorkMode: TWorkMode;
      const AWorkCountMax: Integer);
    procedure bsSkinSpeedButton1Click(Sender: TObject);
    procedure bsSkinSpeedButton2Click(Sender: TObject);
    procedure bsSkinSpeedButton3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
 
var
  Form1: TForm1;

  f: TFileStream;
  success: Boolean;
 
 
implementation
 
 
uses Unit2;
 
 
var
  NewThread: TTestThread;

 
{$R *.DFM}
 
 
procedure TForm1.Button4Click(Sender: TObject);

begin

 
 F := TFileStream.Create('dd.exe', fmCreate);
 F.Seek(0,soFromEnd);
 try
    IdHTTP.Get('http://mydezo.com/deneme.exe', F);
  except
    showmessage('Dosya Bulunamadı');
end;
 FreeAndNil(F);
  F.Free;
   end;
 
 
 
 
 
 
procedure TForm1.FormCreate(Sender: TObject);
begin
application.ProcessMessages;
end;
 
 
procedure TForm1.IdHTTPWork(Sender: TObject; AWorkMode: TWorkMode;
  const AWorkCount: Integer);
begin
if AWorkMode = wmRead then
 begin
   AdvProgressBar1.Position := AWorkCount ;
   Label12.Caption := floatToStr(round(advProgressBar1.Position * 100 / advProgressBar1.Maximum)) + ' ' + '%'  ; //yüzde
   Label13.Caption := IntToStr(AWorkCount div 1024) + ' ';
 
 
end;
end;
 
 
procedure TForm1.IdHTTPWorkBegin(Sender: TObject; AWorkMode: TWorkMode;
  const AWorkCountMax: Integer);
begin
 if AWorkMode = wmRead then
 begin
   AdvProgressBar1.Maximum := AWorkCountMax ;
   AdvProgressBar1.Position := 0 ;
   if AWorkCountMax>0 then Label11.Caption:=Inttostr(AWorkCountMax div 1024) + ' ';
 
 
 end
end;
 
 
procedure TForm1.bsSkinSpeedButton1Click(Sender: TObject);
begin
NewThread:=TTestThread.Create(False);
end;
 
 
procedure TForm1.bsSkinSpeedButton2Click(Sender: TObject);
begin

Button3.Enabled:=True;
Button2.Enabled:=False;
NewThread.Suspend;
end;

 
procedure TForm1.bsSkinSpeedButton3Click(Sender: TObject);
begin

Button2.Enabled:=True;
Button3.Enabled:=False;
NewThread.Resume;
end;
 
 
end.

Код:
unit Unit2;
 
 
interface
 
 
uses
  Classes, Unit1, Windows, Messages, SysUtils;
 
 
type
  TTestThread = class(TThread)
  private
    
  protected
 
 
    procedure Execute; override;  
  end;
 
 
implementation
 

procedure TTestThread.Execute;
begin
  FreeOnTerminate:=true;
  form1.Button4.Click;

end;
 

end.


please help me friends

Последний раз редактировалось alyamus, 12.02.2014 в 22:57.
Ответить с цитированием
  #2  
Старый 13.02.2014, 00:06
Аватар для cotseec
cotseec cotseec вне форума
Активный
 
Регистрация: 16.07.2008
Сообщения: 353
Версия Delphi: D7,TDE06,RAD09
Репутация: 1443
По умолчанию

use TThread.Synchronize or user message
(Is it better to use TThread's ”Synchronize” or use Window Messages for IPC between main and child thread?)
when working with VCL components from child thread
__________________
Понять, что хочет заказчик - бесценно, ведь он платит MasterCard
Ответить с цитированием
  #3  
Старый 13.02.2014, 06:36
lmikle lmikle вне форума
Модератор
 
Регистрация: 17.04.2008
Сообщения: 8,045
Версия Delphi: 7, XE3, 10.2
Репутация: 49089
По умолчанию

The problem is diferent.
It is in your thread.
You created thread and set it to terminate on finish...
...and you do not have loop inside. The main thread procedure (Execute) went to the finish and thread commit the suicide
The code in the thread should look like the following:
Код:
procedure TTestThread.Execute;
begin
  FreeOnTerminate:=true;
  While not Terminated do
    begin
      form1.Button4.Click;
    end;
end;
And, off cause, it is better to move the actual code from main form to the thread and build the event-driven or synchronized callback interface to update indicators on the main form. For example, you can leave the thread to run constantly and pass the tasks from main form. The other option is to build the thread pool and task queue. In this case mainj form put tasks to the queue and each thread (use critical section to sync between the threads) will grab tasks from this queue. As soon as no tasks in the queue, each thread inform the pool (to remove the pointer from it) and die. I did this architecture in one of my application and it works fine.
Ответить с цитированием
Ответ


Delphi Sources

Опции темы Поиск в этой теме
Поиск в этой теме:

Расширенный поиск
Опции просмотра

Ваши права в разделе
Вы не можете создавать темы
Вы не можете отвечать на сообщения
Вы не можете прикреплять файлы
Вы не можете редактировать сообщения

BB-коды Вкл.
Смайлы Вкл.
[IMG] код Вкл.
HTML код Выкл.
Быстрый переход


Часовой пояс GMT +3, время: 20:20.


 

Сайт

Форум

FAQ

RSS лента

Прочее

 

Copyright © Форум "Delphi Sources" by BrokenByte Software, 2004-2023

ВКонтакте   Facebook   Twitter