Page 1 of 1

Multi threading

Posted: Fri 26 Nov 2010 15:57
by sandy771
I have a database that I want to index as a background task (the index creation process takes about 20 mins).

AT the moment I create a separte thread pass it a handle to a TUniQuery, create the indexes (about 20 of them) in the Execute method and then resume the thread.

When I do this sqlite shows that the indexes are created but when I access the database it behaves as if they have not.

I send a message when the thread terminates to indicate when the indexes have been created but this seems to return after about a minute, ether than the 20 I am expecting.

I am thinkingthat I should be createing a separate connection within the thread - would this be better?

Posted: Mon 29 Nov 2010 11:00
by AlexP
Hello,

You can use a general connection for all threads.
Please try to execute the following example:

// Thread class

TIndexThread= class( TThread)
public
UniQuery: TUniQuery;
protected
procedure Execute; override;
end;

// implementation of TIndexThread

procedure TindexThread.Execute;
begin
UniQuery.ExecSQL;
self.Terminate;
end;

// creating and running the thread

var
IndexThread: TindexThread;
begin
UniQuery2.SQL.Text := 'create unique index new_index on test(id)';
IndexThread:= TindexThread.Create(true);
IndexThread.UniQuery:= UniQuery2;
IndexThread.Resume;