A classic tutorial on accessing a database using the VB. NET thread method

Source: Internet
Author: User
Tags net thread

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:

 
 
  1. Private sub GetDataFromDataBase()  
  2. …  
  3. m_table.Clear()  
  4. m_sqlDataAdapter.Fill(m_table)  
  5. …  
  6. 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:

 
 
  1. Dim myThreadStart as ThreadStart =New ThreadStart(AddressOf GetDataFromDataBase)  
  2. Dim myThread as Thread=New Thread(myThreadStart)  
  3. 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:

 
 
  1. Public Class dealDataBase  
  2. Public Event GetDataComplete(ByVal e As DtatTable)  
  3. …  
  4. End Class  

Add the event-issuing code to the getdatafromdatabase () method of the dealdatabase class, after m_sqldataadapter.fill (m_table:

 
 
  1. Public sub GetDataFromDataBase()  
  2. …  
  3. m_sqlDataAdapter.Fill(m_table)  
  4. RasiseEvent GetDataComplete(m_table)  
  5. …  
  6. End Sub   

The following is a proxy generated in the Form class.

 
 
  1. Private sub dealdata (byval e as datatable)
  2. 'Process Data Tables
  3. 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:

 
 
  1. 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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.