关于软件的架构和多线程同步的问题!
小弟没事的时候喜欢打打网游 闲来无事的时候自己写了个辅助工具 能自动攻击 可是因为水平有限 cpu占用率太高...问了几个朋友 说是架构设计有问题 现在将源码附上 请大侠们不惜赐教!
procedure autoAttack.Execute;
begin
while not Terminated do
begin
Synchronize(attack);//小弟知道这句相当于把线程给'废了'可是如果不加上Synchronize 光写attack; 游戏就不会攻击 小弟不知道是不是因为下面的操作有用到vcl控件的原因
sleep(1000);
end;
end;
procedure autoAttack.attack;
var
mubiaoid:Cardinal;
jinengid1,jinengid2,jinengid3:Integer;
yanshi:Integer;
str:string;
begin
ReadProcessMemory(P_ID,Pointer(r_secondbase+$107c),@mubiaoid,4,num);
if shifoujinengdaguai=True then
begin
if mubiaoid=0 then
begin
if shifouguolvdaguai=true then
begin
mubiaoid:=zuijinguaiid(True);
funIn(P_ID,@idxuanguai,@mubiaoid,True);
// Sleep(1000);
end
else
begin
mubiaoid:=zuijinguaiid(False);
funIn(P_ID,@idxuanguai,@mubiaoid,True);
str:=Format('线程:%4d 选怪',[GetCurrentThreadId]);
form23.listbox1.items.Add(str);//进行对控件的写操作 不知道用不用得进行线程同步 end;
end;
if jineng=1 then
begin
jinengid1:=Cardinal(Form23.cbc_jineng1.Items.Objects[Form23.cbc_jineng1.ItemIndex]);//同样对控件进行读写操作
funIn(P_ID,@jinenggongji,@jinengid1,True);
str:=Format('线程:%4d 技能1',[GetCurrentThreadId]);
form23.listbox1.items.Add(str);
if shifoujineng2=True then
begin
jineng:=jineng+1;
end;
end
else if jineng=2 then
begin
jinengid2:=Cardinal(Form23.cbc_jineng2.Items.Objects[Form23.cbc_jineng2.ItemIndex]);
funIn(P_ID,@jinenggongji,@jinengid2,True);
str:=Format('线程:%4d 技能2',[GetCurrentThreadId]);
form23.listbox1.items.Add(str);
if shifoujineng3=True then
begin
jineng:=jineng+1;
end
else
begin
jineng:=1;
end;
end
else if jineng=3 then
begin
jinengid3:=Cardinal(form23.cbc_jineng3.Items.Objects[form23.cbc_jineng3.ItemIndex]);
funIn(P_ID,@jinenggongji,@jinengid3,True);
sleepx(500);//原来在每个funin函数下都有一个sleepx延时(因为是对另外一个进程的发送信号,不清楚什么时候能操作完毕 sleepx函数是我自己定义的就是Application.ProcessMessages();这种自定义延时)我现在基本不用了 可是cpu的占用率还是很高
jineng:=1;
str:=Format('线程:%4d 技能3',[GetCurrentThreadId]);
form23.listbox1.items.Add(str);
end;
end
end;
现在的问题是 我想用多线程 那么我那些应该线程同步那些不用同步呢? 请高手帮帮忙! 谢谢!
ps:我觉得我这个cpu占用率搞是虚假的 因为我运行其他软件的时候他的cpu占用率就下降 就是cpu剩下的使用率都被他占用了