⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 threadunit.cpp

📁 Direct Oracle Access 非常好的Oracle数据库直接访问组件包 支持个版本的Delphi及C++ Builder 有源码
💻 CPP
字号:
// Direct Oracle Access - Oracle/Thread demo
// Allround Automations
// support@allroundautomations.nl
// http://www.allroundautomations.nl
//
// This application demonstrates:
// - Using threads to execute queries simultaneous
// - Creating TOracleQuery dynamically
//---------------------------------------------------------------------------
#include <vcl\vcl.h>
#pragma hdrstop

#include "ThreadUnit.h"
//---------------------------------------------------------------------------
#pragma link "Grids"
#pragma link "Oracle"
#pragma resource "*.dfm"
TThreadForm *ThreadForm;
//---------------------------------------------------------------------------
__fastcall TThreadForm::TThreadForm(TComponent* Owner)
	: TForm(Owner)
{
}
//---------------------------------------------------------------------------
__fastcall TQueryThread::TQueryThread(bool CreateSuspended)
	: TThread(CreateSuspended)
{
}
//-Clear the Grid------------------------------------------------------------
void __fastcall TQueryThread::ClearGrid()
{
  int r, c;

  for (c = 0; c < Grid->ColCount; c++)
    for (r = 0; r < Grid->RowCount; r++)
      Grid->Cells[c][r] = "";
  Grid->ColCount = 2;
  Grid->RowCount = 2;
}
//-Functions to indicate if thread is busy or finished-----------------------
void __fastcall TQueryThread::MemoYellow()
{
  Memo->Color = clYellow;
}
//---------------------------------------------------------------------------
void __fastcall TQueryThread::MemoNormal()
{
  Memo->Color = clWindow;
}
//-Init and start the thread-------------------------------------------------
void __fastcall TQueryThread::Start(TOracleSession *ASession, TStringGrid *AGrid, TMemo *AMemo, TStatusBar *AStatusBar)
{
  // If the quaery is finished the thread may be freed
  FreeOnTerminate = True;
  // Set the components
  Session = ASession;
  Grid = AGrid;
  Memo = AMemo;
  StatusBar = AStatusBar;
  Query = new TOracleQuery(0);
  Query->Session = Session;
  // Save the starttime and go
  StopWatchStart = GetTickCount();
  Resume();
}
//-The main Thread function--------------------------------------------------
void __fastcall TQueryThread::Execute()
{
  int Row, Column;

  // Paint Memo yellow to indicate thread is running
  Synchronize(MemoYellow);
  Synchronize(ClearGrid);
  // Copy the SQL statement in the memo to the query and try to execute it
  Query->Clear();
  Query->SQL->Add(Memo->Text);
  try
  {
    Query->Execute();
    if (ThreadForm->DisplayCheck->Checked)
    {
      // Place the fieldnames into the first row of the grid
      if (Query->FieldCount() >= Grid->ColCount) Grid->ColCount = Query->FieldCount() + 1;
      for (Column = 1; Column <= Query->FieldCount(); Column++)
        Grid->Cells[Column][0] = Query->FieldName(Column - 1);
    }
    // Place the data into the grid
    // We should use synchronize() to update the grid and the statusbar
    // but to keep it simple and a bit faster we'll do it directly
    // with a small change of a display getting messed up
    Row = 1;
    while (!(Query->Eof || Terminated))
    {
      if (ThreadForm->DisplayCheck->Checked)
      {
        Grid->Cells[0][Row] = IntToStr(Row);
        for (Column = 1; Column <= Query->FieldCount(); Column++)
          Grid->Cells[Column][Row] = Query->Field(Column - 1);
        Row++;
        if (Row > Grid->RowCount) Grid->RowCount = Row;
        StatusBar->SimpleText = IntToStr(Row);
      }
      Query->Next();
    }
    StatusBar->SimpleText = IntToStr(Query->RowsProcessed()) + " rows in " +
                            IntToStr(GetTickCount() - StopWatchStart) + " ms";
  }
  catch (EOracleError &E)
  {
    // Show Oracle error in status bar
    StatusBar->SimpleText = E.Message;
  }
  // Memo back to normal color
  Synchronize(MemoNormal);
  Query->Free();
}
//---------------------------------------------------------------------------
void __fastcall TThreadForm::LogonBtnClick(TObject *Sender)
{
  if (LogonBtn->Down) OracleLogon->Execute(); else Session1->LogOff();
  LogonBtn->Down = Session1->Connected;
  ExecBtn->Enabled = Session1->Connected;
  BreakBtn->Enabled = Session1->Connected;
  CommitBtn->Enabled = Session1->Connected;
  RollbackBtn->Enabled = Session1->Connected;
  ThreadSafeCheck->Enabled = !Session1->Connected;
  // Set Session2 identical
  Session2->LogonUsername = Session1->LogonUsername;
  Session2->LogonPassword = Session1->LogonPassword;
  Session2->LogonDatabase = Session1->LogonDatabase;
  Session2->Connected = Session1->Connected;
}
//---------------------------------------------------------------------------
void __fastcall TThreadForm::ExecBtnClick(TObject *Sender)
{
  // Set the ThreadSafe property
  Session1->ThreadSafe = ThreadSafeCheck->Checked;
  Session2->ThreadSafe = ThreadSafeCheck->Checked;
  if ((!ThreadSafeCheck->Checked) & (Sessions->ItemIndex == 0))
    ShowMessage("Unless you're using Oracle8's Net8 you may expect errors");
  // Create the threads
  QueryThread1 = new TQueryThread(True);
  QueryThread2 = new TQueryThread(True);
  // Start the threads with our own Start method
  if (Sessions->ItemIndex == 0)
  {
    // Start both threads with the same session
    QueryThread1->Start(Session1, Grid1, Memo1, StatusBar1);
    QueryThread2->Start(Session1, Grid2, Memo2, StatusBar2);
  }
  else
  {
    // Start both threads, each with its own session
    QueryThread1->Start(Session1, Grid1, Memo1, StatusBar1);
    QueryThread2->Start(Session2, Grid2, Memo2, StatusBar2);
  }
}
//---------------------------------------------------------------------------
void __fastcall TThreadForm::BreakBtnClick(TObject *Sender)
{
  // Note that you'll have to break twice if you're running two queries on
  // a single session.
  Session1->BreakExecution();
  Session2->BreakExecution();
}
//---------------------------------------------------------------------------
void __fastcall TThreadForm::CommitBtnClick(TObject *Sender)
{
  Session1->Commit();
  Session2->Commit();
}
//---------------------------------------------------------------------------
void __fastcall TThreadForm::RollbackBtnClick(TObject *Sender)
{
  Session1->Rollback();
  Session2->Rollback();
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -