Threads Using a thread to backup a database/fr: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Threads Using a thread to backup a database/fr
| Old | New | Line | |
|---|---|---|---|
| 1 | + | ## Exempel de code illustrant l'utilisation de threads | |
| 2 | + | ||
| 3 | + | \- par l'utilisateur du forum ALAU2007 | |
| 4 | + | ||
| 5 | + | ``` pascal | |
| 6 | + | { | |
| 7 | + | *************************************************************************** | |
| 8 | + | * * | |
| 9 | + | * This source is free software; you can redistribute it and/or modify * | |
| 10 | + | * it under the terms of the GNU General Public License as published by * | |
| 11 | + | * the Free Software Foundation; either version 2 of the License, or * | |
| 12 | + | * (at your option) any later version. * | |
| 13 | + | * * | |
| 14 | + | * This code is distributed in the hope that it will be useful, but * | |
| 15 | + | * WITHOUT ANY WARRANTY; without even the implied warranty of * | |
| 16 | + | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * | |
| 17 | + | * General Public License for more details. * | |
| 18 | + | * * | |
| 19 | + | * A copy of the GNU General Public License is available on the World * | |
| 20 | + | * Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also * | |
| 21 | + | * obtain it by writing to the Free Software Foundation, * | |
| 22 | + | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * | |
| 23 | + | * * | |
| 24 | + | *************************************************************************** | |
| 25 | + | ||
| 26 | + | Abstract: | |
| 27 | + | Demo to show, how to start a thread and how synchronize with the main | |
| 28 | + | thread. | |
| 29 | + | Important: The cthread unint must be added to the uses section of the .lpr | |
| 30 | + | file. See multithreadingexample1.lpr. | |
| 31 | + | } | |
| 32 | + | unit MainUnit; | |
| 33 | + | ||
| 34 | + | {$mode objfpc}{$H+} | |
| 35 | + | ||
| 36 | + | interface | |
| 37 | + | ||
| 38 | + | uses | |
| 39 | + | Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ComCtrls, | |
| 40 | + | sqldb, mssqlconn; | |
| 41 | + | ||
| 42 | + | type | |
| 43 | + | ||
| 44 | + | { TMyBackupThread } | |
| 45 | + | ||
| 46 | + | TMyBackupThread = class(TThread) | |
| 47 | + | private | |
| 48 | + | fStatusText: string; | |
| 49 | + | procedure ShowStatus; | |
| 50 | + | protected | |
| 51 | + | procedure Execute; override; | |
| 52 | + | public | |
| 53 | + | constructor Create(CreateSuspended: boolean); | |
| 54 | + | end; | |
| 55 | + | ||
| 56 | + | { TForm1 } | |
| 57 | + | ||
| 58 | + | TForm1 = class(TForm) | |
| 59 | + | Button1: TButton; | |
| 60 | + | Label1: TLabel; | |
| 61 | + | MSSQLConnection1: TMSSQLConnection; | |
| 62 | + | ProgressBar1: TProgressBar; | |
| 63 | + | SQLQuery1: TSQLQuery; | |
| 64 | + | SQLTransaction1: TSQLTransaction; | |
| 65 | + | procedure Button1Click(Sender: TObject); | |
| 66 | + | private | |
| 67 | + | public | |
| 68 | + | end; | |
| 69 | + | ||
| 70 | + | var | |
| 71 | + | Form1: TForm1; | |
| 72 | + | ||
| 73 | + | implementation | |
| 74 | + | ||
| 75 | + | {$R *.lfm} | |
| 76 | + | ||
| 77 | + | { TForm1 } | |
| 78 | + | ||
| 79 | + | procedure TForm1.Button1Click(Sender: TObject); | |
| 80 | + | var | |
| 81 | + | MyBackupThread : TMyBackupThread; | |
| 82 | + | begin | |
| 83 | + | ||
| 84 | + | MyBackupThread := TMyBackupThread.Create(True); // With the True parameter it doesn't start automatically | |
| 85 | + | if Assigned(MyBackupThread.FatalException) then | |
| 86 | + | raise MyBackupThread.FatalException; | |
| 87 | + | ||
| 88 | + | // Here the code initialises anything required before the threads starts executing | |
| 89 | + | ||
| 90 | + | //Test_Dummy | |
| 91 | + | MyBackupThread.Start; | |
| 92 | + | ||
| 93 | + | Label1.Caption := 'Start'; | |
| 94 | + | SQLQuery1.Active := False; | |
| 95 | + | while ( ProgressBar1.Position < 100 ) do begin | |
| 96 | + | SQLQuery1.Active := true; | |
| 97 | + | SQLQuery1.ExecSQL; | |
| 98 | + | ProgressBar1.Position := SQLQuery1.FieldByName('Percent Complete').AsInteger; | |
| 99 | + | Label1.Caption := FormatFloat( '##0%', SQLQuery1.FieldByName('Percent Complete').AsInteger ) | |
| 100 | + | + '%, Estimated completion Time ' + FormatFloat( '#0.00', SQLQuery1.FieldByName('ETA Min').AsFloat ) + ' min.'; | |
| 101 | + | Application.ProcessMessages; | |
| 102 | + | SQLQuery1.Active := False; | |
| 103 | + | end; | |
| 104 | + | end; | |
| 105 | + | ||
| 106 | + | {TBackupThread} | |
| 107 | + | ||
| 108 | + | procedure TMyBackupThread.ShowStatus; | |
| 109 | + | // this method is only called by Synchronize(@ShowStatus) and therefore | |
| 110 | + | // executed by the main thread | |
| 111 | + | // The main thread can access GUI elements, for example Form1.Caption. | |
| 112 | + | begin | |
| 113 | + | Form1.Label1.Caption := fStatusText; | |
| 114 | + | end; | |
| 115 | + | ||
| 116 | + | procedure TMyBackupThread.Execute; | |
| 117 | + | var | |
| 118 | + | Conn: TMSSQLConnection; | |
| 119 | + | Tran: TSQLTransaction; | |
| 120 | + | begin | |
| 121 | + | fStatusText := 'Backup Starting ...'; | |
| 122 | + | Synchronize(@Showstatus); //If I remark this, it causes "access violation" error | |
| 123 | + | Conn:=TMSSQLConnection.create(nil); | |
| 124 | + | Tran:=TSQLTransaction.create(nil); | |
| 125 | + | try | |
| 126 | + | Conn.HostName:='127.0.0.1\sqlexpress'; | |
| 127 | + | Conn.UserName:=''; //trusted authentication/SSPI | |
| 128 | + | Conn.Password:=''; //trusted authentication/SSPI | |
| 129 | + | Conn.DatabaseName:='Test_Dummy'; | |
| 130 | + | Conn.Params.Add('AutoCommit=true'); | |
| 131 | + | Conn.Transaction:=Tran; | |
| 132 | + | Conn.Open; | |
| 133 | + | Conn.ExecuteDirect('backup database Test_Dummy to disk = N''C:\TEMP\TEST_Dummy.bak'' with format, init'); | |
| 134 | + | Conn.Close; | |
| 135 | + | fStatusText := 'Backup Completed'; | |
| 136 | + | Synchronize(@Showstatus); | |
| 137 | + | finally | |
| 138 | + | Tran.Free; | |
| 139 | + | Conn.Free; | |
| 140 | + | end; | |
| 141 | + | end; | |
| 142 | + | ||
| 143 | + | constructor TMyBackupThread.Create(CreateSuspended: boolean); | |
| 144 | + | begin | |
| 145 | + | FreeOnTerminate := True; | |
| 146 | + | inherited Create(CreateSuspended); | |
| 147 | + | end; | |
| 148 | + | ||
| 149 | + | end. | |
| 150 | + | ``` | |
| 151 | + | ||
| 152 | + | --- | |
| 153 | + | *From the Free Pascal wiki, page "Threads Using a thread to backup a database/fr", as it stood on 2020-03-01 after a change by GAN, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
