thread线程类,哪里有问题,求助

qq_41565650 2018-04-23 09:50:09
unit Unit1;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ComCtrls, Data.DB,
Vcl.Grids, Vcl.DBGrids;

type
TForm1 = class(TForm)
OpenDialog1: TOpenDialog;
ListView1: TListView;
Edit1: TEdit;
procedure ListView1DblClick(Sender: TObject);


private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

type
thread=class(TThread)
public
front:integer; //读取线程的开始于中止位置
rear:integer;
constructor Create(a:integer;b:integer); //构造函数不能写在protected里面,只能写在public中
protected
procedure Execute; override;

end;

var
mstream:TmemoryStream;
filepath:string;
thread1,thread2,thread3,thread4:thread;
i:integer;




procedure TForm1.ListView1DblClick(Sender: TObject);
begin
opendialog1.Options:=[offilemustexist,ofpathmustexist];
if opendialog1.Execute then filepath:=opendialog1.FileName;
mStream:=TmemoryStream.Create;
mStream.loadfromfile(filepath); //把文件读入内存
thread1.Create(0,180); //创建就立即执行调用 ,错误在哪里
thread1.Resume ;
end;




constructor thread.Create(a:integer;b:integer);
begin
//front:=a;
//rear:=b;
inherited create(true);
end;

procedure thread.Execute;
var
strlist1:Tstringlist;
s:string;
c:char;
begin
inherited;
s:=' ';
i:=1;
// while (mStream.position>=front ) and (Mstream.Position<=rear ) do
while mStream.Position<=mStream.Size do
begin
mStream.Read(c,1);
s:=s+c;
if length(s)=36 then //切割后的字符串是从下标0开始存放的
begin
ExtractStrings([' '], [], PChar(s), strlist1);
with Form1.ListView1.Items.Add do
begin
caption:=inttostr(i);
subitems.Add(strlist1[0]);
subitems.Add(strlist1[1]);
subitems.Add(strlist1[2]);
end;
form1.Edit1.Text :='当前行是第'+inttostr(i)+'行,一共有141047行';
inc(i);
end;
end;
strlist1.Free;
end;

end.
我每次一运行到thread1.create(0,180)的地方,就出现地址非法访问的错误,可是我感觉的没错误呀,求助各位大佬啦,本人实在是改不出来了
...全文
947 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
nebula845026 2018-04-25
  • 打赏
  • 举报
回复
thread1.Create(0,180); 你执行这一行的时候thread1还是nil,但让报内存错误了 应该改成 thread1 := TThread.Create(0,180);
qq_41565650 2018-04-24
  • 打赏
  • 举报
回复
引用 1 楼 bigc2001 的回复:
thread1:=thread.create(...
谢谢啦
qq_41565650 2018-04-24
  • 打赏
  • 举报
回复
引用 4 楼 xiaocongzhi 的回复:
Thread才是类名
我这是新版本的,Delphi10.1的,线程类就是TThread,我一改就报错了
xiaocongzhi 2018-04-24
  • 打赏
  • 举报
回复
Thread才是类名
qq_41565650 2018-04-24
  • 打赏
  • 举报
回复
本人是小白一个,不太懂怎样将主线程与子线程同步,可以讲仔细点吗?真是万分感谢
引用 2 楼 lynmison 的回复:
子线程和主线程需要同步,子线程里用了主线程的对象。
天行归来 2018-04-24
  • 打赏
  • 举报
回复
子线程和主线程需要同步,子线程里用了主线程的对象。
比特灵 2018-04-23
  • 打赏
  • 举报
回复
thread1:=thread.create(...

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧