读书人

Delphi 多线程 出错

发布时间: 2012-07-28 12:25:13 作者: rapoo

【求助】Delphi 多线程 出错,
本来是 在一个按钮的 单击事件中执行的代码,因为太复杂,运行时会导致出口假死,
所以改用多线程,

没改之前运行没问题,

改为后 一运行 就会有个 全屏窗口出现在屏幕上(这个窗口正常是不该出现的,)
线程中的代码也没有正常执行。

下面是 线程中的 部分代码,请高手帮忙看看是 什么问题 ;

Delphi(Pascal) code
CreateThread(nil, 0, @CreatNcList, nil, 0, ID);  //掉用 线程 的 语句





线程 代码

Delphi(Pascal) code
try      Excel:=CreateOleObject('Excel.Application');      Excel.visible:=false;      WorkBook:=CreateOleobject('Excel.Sheet');   except      ShowMessage('无法打开xls文件,请确认已经安装EXCEL');      Exit;   end;   i:=0;   while i<FormNcList.ListBox_NcFileList.Count do   Begin       iRow:=2;       RowCellName:='';       RowCellCount:=0;       WorkSheetsNum:=1;       WorkBook:= Excel.workBooks.Open(FormNcList.ListBox_NcFileList.Items.Strings[I]);       //ShowMessage(IntToStr(Excel.Workbooks[1].WorkSheets.Count));       //ShowMessage(Excel.Workbooks[1].WorkSheets[1].Name);       while (Excel.Workbooks[1].WorkSheets.Count > 0) and (WorkSheetsNum <= Excel.Workbooks[1].WorkSheets.Count) do       Begin          if Excel.Workbooks[1].WorkSheets[WorkSheetsNum].Name <> '' then          Begin             WorkSheetsName:=Excel.Workbooks[1].WorkSheets[WorkSheetsNum].Name;             WorkSheetsName:=StringReplace(WorkSheetsName,chr(32),'',[rfIgnoreCase,rfReplaceAll]);             if CompareText(WorkSheetsName,'NcList') = 0 then             Begin                QTY:=StringReplace(Excel.Workbooks[1].WorkSheets[WorkSheetsNum].Cells[1,7].Value,chr(32),'',[rfIgnoreCase,rfReplaceAll]);                REFDES:=StringReplace(Excel.Workbooks[1].WorkSheets[WorkSheetsNum].Cells[1,8].Value,chr(32),'',[rfIgnoreCase,rfReplaceAll]);                if CompareText(REFDES,'REFDES') = 0 then                Begin                   while Excel.Workbooks[1].WorkSheets[WorkSheetsNum].Cells[iRow,8].Value <> '' do                   Begin                      RowCellName:=RowCellName + Excel.Workbooks[1].WorkSheets[WorkSheetsNum].Cells[iRow,8].Value;                      //Row:= Row + Excel.Workbooks[1].WorkSheets[WorkSheetsNum].Cells[iRow,7].Value;                      iRow:=iRow + 1;                   End;                End;                if CompareText(QTY,'QTY') = 0 then                Begin                   while iRow >= 2 do                   Begin                      RowCellCount:=RowCellCount + Excel.Workbooks[1].WorkSheets[WorkSheetsNum].Cells[iRow,7].Value;                      iRow:=iRow - 1;                   End;                End;                ShowMessage(IntToStr(RowCellCount));                ShowMessage(RowCellName);                Break;             End;          End;          WorkSheetsNum:=WorkSheetsNum+1;       End;       Excel.WorkBooks[1].Close(False,FormNcList.ListBox_NcFileList.Items.Strings[I]);    //取文件名退出


[解决办法]
探讨
CoInitialize(nil);
你的线程代码
CoUnInitialize 加入这两句代码会出错,说 E2003 Undeclared identifier: 'CoInitialize'

另外,在线程中访问界面 时 可以不同步吗,我这里是有一个线程(除主线程外)

[解决办法]
因为ShowMessage实际上就是要创建一个form窗体,然后showmodal显示出来,线程运行到这里当然就出错了

访问界面/VCL要用同步 (有时可以不用,但要看是访问是什么东西了)

读书人网 >.NET

热点推荐