1

I am sorry for asking such a simple question. This is the first time I am using a background thread in VCL. So I was wondering if it is safe to use local variable in Async part?

procedure Tfrm1.ThreadedFillDataset;
var
  ds : TOracleDataSet;
begin

  lblThread.Caption := 'Thread start';
  Async(
    procedure
    begin
      // executed in background thread
      //Sleep(3000);
      ds := TOracleDataSet.Create(Self);
      //SetVariables 
      ds.Open;
    end)
  .Await(
    procedure
    begin
      ds.First;
      while not ds.Eof do
      begin
        //Fill design time dataset on form
        ds.Next;
      end;
        ds.Free;
        lblThread.Caption := 'Thread finished';

    end);
end;

Suppose that I execute this method three times, without first thread being completed. Would this lead to problems?

Would you suggest another approach to this problem? (Using background thread to query database, append results into a VCL dataset.)

Thanks.

Edit: I have examined the answer to question 13348970 which my question was marked a duplicate of. I have come to conclude that if my method is invoked 3 different times, all 3 ds local variables will point to a different memory adress, therefore each thread will access its local copy of TOracleDataSet and there will be no problems. If someone can confirm this I can close the question, thank you.

Community
  • 1
  • 1
akaya
  • 110
  • 4
  • 9
  • Why not populate the TOracleDataSet in the background thread? Since the Await part is executed in the main thread, does the order of the vcl append matter? If it does not, I see no problems. – LU RD Aug 27 '15 at 09:08
  • If TOracleDataSet object is owned by the form, wouldn't it lead to problems if it is accessed at the same time from background thread and VCL thread? – akaya Aug 27 '15 at 10:07
  • The TOracleDataSet object is created with the form as an owner. I suppose you don't need an owner. You can control the life time of this object in your Await method. Calling the ThreadedFillDataSet method multiple times is ok from a thread-safe point of view. – LU RD Aug 27 '15 at 11:15
  • IMO, you shouldn't do something like this. Async / Await anonymous procedures execution is outside of `ThreadedFillDataset` scope (i guess). So using a local variable is conceptually wrong. – Sotirca Mihaita George Aug 27 '15 at 13:39
  • 2
    @SotircaMihaitaGeorge I was just looking around and read an article that states anonymous methods capture local variables. This is done at compiler level. http://edn.embarcadero.com/article/41175 _Because here we’re using an anonymous function, the compiler does all that work for us. It “captures” the lock variable and allows the anonymous function to use it._ – akaya Aug 27 '15 at 14:05
  • Possible duplicate of [How can I capture variables by anonymous method when using it in OTL?](http://stackoverflow.com/questions/13348970/how-can-i-capture-variables-by-anonymous-method-when-using-it-in-otl) – Graymatter Aug 28 '15 at 01:16
  • Yes, the anonymous procedure captures different locals. As I said in an earlier comment, no problem calling ThreadedFillDataSet method repeatedly from a thread-safe point of view. If this is a sound approach to use a background thread, I'm not sure. The only thing you do is to open the database. – LU RD Aug 31 '15 at 07:59
  • By calling .Open in background thread, time spent waiting on database for SQL query does not block the user interface. When it finishes, I put the results into another Dataset. – akaya Aug 31 '15 at 12:28

0 Answers0