Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Multithreading 为什么在线程调用asynchron方法时接收系统外资源_Multithreading_Delphi_Asynchronous_Bitmap - Fatal编程技术网

Multithreading 为什么在线程调用asynchron方法时接收系统外资源

Multithreading 为什么在线程调用asynchron方法时接收系统外资源,multithreading,delphi,asynchronous,bitmap,Multithreading,Delphi,Asynchronous,Bitmap,我有一个TThread,它调用下一个函数: //------------------------------------------------------------------------------ procedure TfrImageShow.Load(bmBitmap:TBitmap); begin self.bmBitmapCopy.Width:=bmBitmap.Width; self.bmBitmapCopy.Height:=bmBitmap.Height;

我有一个TThread,它调用下一个函数:

//------------------------------------------------------------------------------
procedure TfrImageShow.Load(bmBitmap:TBitmap);
begin

   self.bmBitmapCopy.Width:=bmBitmap.Width;
   self.bmBitmapCopy.Height:=bmBitmap.Height;
   self.bmBitmapCopy.Canvas.Draw(0,0,bmBitmap);
end;
我执行中的线程调用了这个方法,一段时间后,它给出了一个错误:系统资源不足?为什么?因为位图不是VCL组件。它还使用临界截面

TThread.execute
begin
 ....
   csCriticalSection.Enter;
       frImage.Load(bmBitmap);
   csCriticalSection.Leave;
....
end;

另外,我还有一个问题,就是这个关键部分是否有用(我向您提到,没有任何其他线程),只是有时候应用程序线程从读取bmBitmapCopy,保护从具有
关键secion的线程访问GUI(主线程VCL)不是正确的处理方法

由于无法从其他线程直接访问VCL,因此该线程必须同步对VCL的调用

示例(如果您的Delphi版本支持
匿名方法
):

Synchronize
方法将在执行期间交换到主线程

如果您有较旧的Delphi版本,请向
TThread
类中添加一个方法,并从中调用Load:

TThread.CallLoad;
begin
  frImage.Load(bmBitmap);
end;

TThread.Execute;
begin
  ...
  Synchronize( Self.CallLoad);
  ...
end;

使用
关键部分
将两个(或多个)线程对公共对象/变量等的访问序列化。但在这种情况下,严格禁止从线程调用VCL资源

同步只接受无参数的方法。@NGLN,用一个使用匿名方法解决参数调用的示例更新了答案。谢谢。@NGLN,还有另一个使用线程方法与参数同步的示例。
TThread.CallLoad;
begin
  frImage.Load(bmBitmap);
end;

TThread.Execute;
begin
  ...
  Synchronize( Self.CallLoad);
  ...
end;