Faqs on asp.net,c#,vb.net and sqlserver2005

this blog covers all the Faqs related to .net technologies like asp.net,vb.net,c#.net,ajax,javascript and sqlserver2005.

Jan 29, 2008

Threading

(1)What is Multi-tasking ?
Its a feature of modern operating systems with which we can run multiple programs at
same time example Word,Excel etc.
(2)What is Multi-threading ?
Multi-threading forms subset of Multi-tasking instead of having to switch between programs
this feature switches between different parts of the same program.Example you are writing
in word and at the same time word is doing a spell check in background.
(3)What is a Thread ?
A thread is the basic unit to which the operating system allocates processor time.
(4)Did VB6 support multi-threading ?
While VB6 supports multiple single-threaded apartments, it does not support a freethreading
model, which allows multiple threads to run against the same set of data.
(5)Can we have multiple threads in one App domain ?
One or more threads run in an AppDomain. An AppDomain is a runtime representation
of a logical process within a physical process.Each AppDomain is started with a single
thread, but can create additional threads from any of its threads.
Note :- All threading classes are defined in System.Threading namespace.
(6)Which namespace has threading ?
Systems.Threading has all the classes related to implement threading.Any .NET application
who wants to implement threading has to import this namespace.
Note :- .NET program always has atleast two threads running one the main program and
second the garbage collector.
(7)Can you explain in brief how can we implement threading ?
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim pthread1 As New Thread(AddressOf Thread1)
Dim pthread2 As New Thread(AddressOf Thread2)
pthread1.Start()
pthread2.Start()
End Sub
Public Sub Thread1()
Dim pintcount As Integer
Dim pstr As String
pstr = “This is first thread”
Do Until pintcount > 5
lstThreadDisplay.Items.Add(pstr)
pintcount = pintcount + 1
Loop
End Sub
Public Sub Thread2()
Dim pintcount As Integer
Dim pstr As String
pstr = “This is second thread”
Do Until pintcount > 5
lstThreadDisplay.Items.Add(pstr)
pintcount = pintcount + 1
Loop
End Sub
Above is a sample code which shows simple sample code for threading..Above sample
code can be found in “Threading” folder in CD provided.Above sample has two methods
“Thread1()” and “Thread2()” which are started in multi-threaded mode in Form load
event of the sample.
Note :- If you run the sample you will see that sometimes the first thread runs first and
then the second thread.This happens because of thread priorities .The first thread is run
with highest priority.

0 Comments:

Post a Comment

<< Home