>>4.我的qry:Tquery是定义我窗体(fserver)的全局变量,并在线程执行的时候动态生成、消失。
qry要定义为局部变量,在线程中使用qry时,qry连接的TDatabase也要动态生成。

解决方案 »

  1.   

    谢谢!!
    可是我在线程中定义qry:Tquery也不可以的,在qry:=Tquery.create(对象);中这个“对象”好象只能是窗体(fserver),不能是线程的。
    谢谢你的关注。可以联系吗?我的OICQ:31374226
      

  2.   

    If you create a single application that uses multiple threads to perform database operations, you must create one additional session for each thread. The Data Access page on the Component palette contains a session component that you can place in a data module or on a form at design time.Important: When you place a session component, you must also set its SessionName property to a unique value so that it does not conflict with the default session抯 SessionName property.Placing a session component at design time presupposes that the number of threads (and therefore sessions) required by the application at runtime is static. More likely, however, is that an application needs to create sessions dynamically. To create sessions dynamically, call the global function Sessions.OpenSession at runtime.
    Sessions.OpenSession requires a single parameter, a name for the session that is unique across all session names for the application. The following code dynamically creates and activates a new session with a uniquely generated name:Sessions.OpenSession('RunTimeSession' + IntToStr(Sessions.Count + 1));This statement generates a unique name for a new session by retrieving the current number of sessions, and adding one to that value. Note that if you dynamically create and destroy sessions at runtime, this example code will not work as expected. Nevertheless, this example illustrates how to use the properties and methods of Sessions to manage multiple sessions.
    Sessions is a variable of type TSessionList that is automatically instantiated for database applications. You use the properties and methods of Sessions to keep track of multiple sessions in a multi-threaded database application. The following table summarizes the properties and methods of the TSessionList component:Property or 
    Method Purpose
    Count Returns the number of sessions, both active and inactive, in the sessions list.
    FindSession Searches the session list for a session with a specified name, and returns a pointer to the session component, or nil if there is no session with the specified name. If passed a blank session name, FindSession returns a pointer to the default session, Session.
    GetSessionNames Populates a string list with the names of all currently instantiated session components. This procedure always adds at least one string, 揇efault?for the default session (note that the default session抯 name is actually a blank string).
    List Returns the session component for a specified session name. If there is no session with the specified name, an exception is raised.
    OpenSession Creates and activates a new session or reactivates an existing session for a specified session name.
    Sessions Accesses the session list by ordinal value.
    As an example of using Sessions properties and methods in a multi-threaded application, consider what happens when you want to open a database connection. To determine if a connection already exists, use the Sessions property to walk through each session in the sessions list, starting with the default session. For each session component, examine its Databases property to see if the database in question is open. If you discover that another thread is already using the desired database, examine the next session in the list.If an existing thread is not using the database, then you can open the connection within that session.
    If, on the other hand, all existing threads are using the database, you must open a new session in which to open another database connection.
    If you are replicating a data module that contains a session in a multi-threaded application, where each thread contains its own copy of the data module, you can use the AutoSessionName property to make sure that all datasets in the data module use the correct session. Setting AutoSessionName to True causes the session to generate its own unique name dynamically when it is created at runtime. It then assigns this name to every dataset in the data module, overriding any explicitly set session names. This ensures that each thread has its own session, and each dataset uses the session in its own data module.
      

  3.   

    >>qry:=Tquery.create(对象);
    可以写成这样:
    qry:=Tquery.create(nil);