Before giving you a detailed introduction to the VB. NET thread, first let everyone know about the thread, and then give a comprehensive introduction to the VB. NET thread method. A thread is the basic unit for the operating system to allocate a processor time. A thread can run multiple activities at the same time as a single execution thread, operating systems that support preemptive multi-task processing can create multiple threads and run them simultaneously through time slice rotation. In applications that require good user interaction and communications with networks and databases, multithreading can provide a good interaction experience and quickly respond to user requirements. This article mainly introduces the specific application of. Net threads in Database Programming (implemented using VB. NET ).
1. Create a database access thread
In database applications, especially network database access, a large amount of data may be accessed, so it takes a long time to get results. A good program should be highly interactive, when accessing the database, your application should be allowed to respond to user activities as soon as possible to provide a rich user experience. The multi-threaded mechanism allows a large number of operations to run in the background to quickly respond to user activities. The following code accesses the database and returns the data table:
- Private sub GetDataFromDataBase()
- …
- m_table.Clear()
- m_sqlDataAdapter.Fill(m_table)
- …
- End Sub
To create a new instance of the thread object, you must create a new thread proxy. the threadstart thread proxy can specify the name of the method to be executed when the thread is generated, but the thread proxy does not actually run the thread. when creating a threadstart object, you must specify the pointer to the method to run when the thread starts execution. This method cannot accept any parameters. Next we will allocate the above Code to a thread for processing and start it:
- Dim myThreadStart as ThreadStart =New ThreadStart(AddressOf GetDataFromDataBase)
- Dim myThread as Thread=New Thread(myThreadStart)
- myThread.Start()
In this way, the user can continue to process the database access.
2. VB. NET thread method usage event
The start method of the thread is called, and the method in the thread cannot be executed immediately. To obtain the data access result, you must wait for the method to be executed. If the cyclic query method is used after the running thread obviously affects the interaction, the event is a good way to return data from the thread method. Define an event in the class where the VB. NET thread method is located, issue an event in the VB. NET thread method, and generate a proxy in the Form class.
First, add the event after the dealdatabase class declaration:
- Public Class dealDataBase
- Public Event GetDataComplete(ByVal e As DtatTable)
- …
- End Class
Add the event-issuing code to the getdatafromdatabase () method of the dealdatabase class, after m_sqldataadapter.fill (m_table:
- Public sub GetDataFromDataBase()
- …
- m_sqlDataAdapter.Fill(m_table)
- RasiseEvent GetDataComplete(m_table)
- …
- End Sub
The following is a proxy generated in the Form class.
- Private sub dealdata (byval e as datatable)
- 'Process Data Tables
- End sub
Perform event connection in the code that creates a thread and runs the thread. The event connection code is placed before the running thread. After the dealdatabase class is instantiated:
- AddHandler myDB. GetDataComplete,AddressOf dealData
In this way, an event is triggered when the thread method is executed, and the dealdata method responds to the event and processes it.
Http://www.qqread.com/vbdotnet/i479742.html