线程Suspended=false 可是没有在运行? ( 积分: 50 )

  • 主题发起人 主题发起人 squarevictory
  • 开始时间 开始时间
S

squarevictory

Unregistered / Unconfirmed
GUEST, unregistred user!
如题,这种情况时有时无,特别是在线程中发生异常后出现。
 
记得线程挂起是有累计数! 你做了多少次挂起就得唤醒多少次! 他才运行起来
 
还有是不是线程异常的时候线程已经卦了啊
 
对啊 就是线程内部异常挂了 ,做了个try except end;
然后就不动了
 
你的代码看看!
while not termitedo
begin
lock;
try
leaveLock;
except
leaveLock;
continue;
end;
end;
记得做线程同步
 
while not termitedo
begin
lock;
try
try
except
//do
nothing or [red]Notify[/red] your app
end;
finally
leaveLock;
end;
end;
 
lock 和leavelock是甚么?线程没这个方法
 
lock 和leavelock是甚么?线程没这个方法
 
是在线程里操作数据库,当数据库连接断开时候做了异常处理
try
except
try
conn.open;
conn.close;
except
end;

end;
恢复数据库连接后,线程不动了,再次设置线程的resume属性时提示thread error:拒绝访问(5)
 
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB, SyncObjs;
type
TAdoConnThread = class;
TForm1 = class(TForm)
ADOConnection1: TADOConnection;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
private
Flock: TCriticalSection;
FAdoThread: TAdoConnThread;
procedure ConnDataBase;
public
{ Public declarations }
end;
TAdoConnThread = Class(TThread)
private
AdoForm: TForm1;
procedure AdoConnDataBase;
public
constructor Create(CreateSuspended: Boolean;
AForm: TForm1);
procedure Execute;
override;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TAdoConnThread }
procedure TAdoConnThread.AdoConnDataBase;
begin
AdoForm.ConnDataBase;
end;

constructor TAdoConnThread.Create(CreateSuspended: Boolean;
AForm: TForm1);
begin
inherited Create(CreateSuspended);
FreeOnTerminate := True;
AdoForm := AForm;
Resume;
end;

procedure TAdoConnThread.Execute;
begin
while not Terminateddo
begin
Form1.Flock.Enter;
try
Synchronize(AdoConnDataBase);
finally
Sleep(20);
//为了CPU占有率减低
Form1.Flock.Leave;
end;
end;
end;

{ TForm1 }
procedure TForm1.ConnDataBase;
begin
with ADOConnection1do
try
Connected := False;
Connected := True;
except
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
Flock := TCriticalSection.Create;
FAdoThread := TAdoConnThread.Create(True, Self);
end;

procedure TForm1.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
Flock.Free;
end;
end.
我刚写了一段,2006-2007 最后帮忙一贴而努力! 写了. 好久没有这么认真回答过了! ^_^
 
感动 ,二话不说 先试试
 
对啊 就是线程内部异常挂了 ,做了个try except end;
然后就不动了
-----------------------------------------------------------------
我测试了一下不是卦了! 而是连接数据库是所消耗的时间有点过长了.
 
我在主界面和线程都做了同样的异常
try
(dosothing)
try
ADOConn.Close;
ADOConn.Open;
except
end;
except
end;
可是网络连接断开后只触发了主程序的异常,线程的异常没有触发,可是就是不运行了
 
procedure TForm1.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
Flock.Free;
end;
修改为下面主要是停止线程执行!!
procedure TForm1.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
FAdoThread.Terminate;
Sleep(100);
Flock.Free;
Sleep(20);
end;
//放假回去过年了, 新年愉快
 
请你参照我的写吧! 我这里测试是很正常的^_^
 
好的 谢谢 我要17号才能走 新年快乐,一路顺风。
 
kk兄 我知道甚么问题了 ,
如果conn连接是与数据库绑定的,那网络连接断开不会有问题,而我的是在程序启动后通过设置conn.connectionstring来进行连接的,所以当断开网络连接的时候就报错,但为甚么事先绑定的就不会出错,而动态设置的就要出错那?
 
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB, SyncObjs, StdCtrls;
type
TAdoConnThread = class;
TForm1 = class(TForm)
ADOConnection1: TADOConnection;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
private
Flock: TCriticalSection;
FAdoThread: TAdoConnThread;
procedure ConnDataBase;
public
{ Public declarations }
end;
TAdoConnThread = Class(TThread)
private
AdoForm: TForm1;
procedure AdoConnDataBase;
public
constructor Create(CreateSuspended: Boolean;
AForm: TForm1);
procedure Execute;
override;
end;
var
Form1: TForm1;
implementation
const
CCONNSTR = ' Provider=SQLOLEDB.1;Password= ''%s'';Persist Security Info=True;'
+ ' User ID=''%s'';Initial Catalog= ''%s'';Data Source=''%s''' ;
{$R *.dfm}
{ TAdoConnThread }
procedure TAdoConnThread.AdoConnDataBase;
begin
AdoForm.ConnDataBase;
end;

constructor TAdoConnThread.Create(CreateSuspended: Boolean;
AForm: TForm1);
begin
inherited Create(CreateSuspended);
FreeOnTerminate := True;
AdoForm := AForm;
Resume;
end;

procedure TAdoConnThread.Execute;
begin
while not Terminateddo
begin
AdoForm.Flock.Enter;
try
Synchronize(AdoConnDataBase);
finally
AdoForm.Flock.Leave;
Sleep(200);
//为了CPU占有率减低
end;
end;
end;

{ TForm1 }
procedure TForm1.ConnDataBase;
var
vConnStr: String;
begin
with ADOConnection1do
try
Connected := False;
vConnStr := Format(CCONNSTR, [Edit4.Text, Edit3.Text, Edit2.Text, Edit1.Text]) ;
ConnectionString := vConnStr;
Connected := True;
except
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
Flock := TCriticalSection.Create;
FAdoThread := TAdoConnThread.Create(True, Self);
end;

procedure TForm1.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
FAdoThread.Terminate;
Sleep(100);
Flock.Free;
Sleep(20);
end;

end.
//我以为能为你解决问题了! 现在我加个动态设置连接字符串的也没有发现出现什么问题啊。
 
后退
顶部