Contenu connexe
En vedette
tybsc it asp.net full unit 1,2,3,4,5,6 notestybsc it asp.net full unit 1,2,3,4,5,6 notesWE-IT TUTORIALS
Similaire à Vb net xp_10 (20)
Vb net xp_10
- 1. Performing File I/O Operations and Implementing
Multithreading in Visual Basic .NET
Objectives
In this lesson, you will learn to:
Identify the benefits of the .NET System.IO model
Identify the benefits of the Visual Basic .NET run-time
functions
Identify the benefits of multithreading
Implement threads in Visual Basic .NET
©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/
Slide 1 of 38
- 2. Performing File I/O Operations and Implementing
Multithreading in Visual Basic .NET
File
Is a collection of bytes that has a persistent storage.
I/O operations in Visual Basic .NET are handled in two
different ways:
Using the .NET System.IO model
Using the Visual Basic .NET run-time functions
©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/
Slide 2 of 38
- 3. Performing File I/O Operations and Implementing
Multithreading in Visual Basic .NET
System.IO Model
Specifies a set of classes that are available to all the .NET
languages.
Contains classes that are used for creating, copying,
moving, and deleting files.
Contains the following frequently used classes:
FileStream
BinaryReader and BinaryWriter
StreamReader and StreamWriter
Directory
©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/
Slide 3 of 38
- 4. Performing File I/O Operations and Implementing
Multithreading in Visual Basic .NET
The File Hierarchy in the System.IO Model
©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/
Slide 4 of 38
- 5. Performing File I/O Operations and Implementing
Multithreading in Visual Basic .NET
The FileStream Class
Provides access to files and file-related information.
Is used with the File class, which is contained in the
System.IO namespace, to create, copy, delete, move, and
open files.
Can also be used with another class called Path, to
manipulate the strings that represent folder or file paths.
Opens a file either in synchronous or asynchronous mode.
Opens files in synchronous mode by default.
Has constructors that use enumerations, such as FileMode,
FileAccess, and FileShare, to specify how a file is
created, opened, and shared.
©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/
Slide 5 of 38
- 6. Performing File I/O Operations and Implementing
Multithreading in Visual Basic .NET
FileMode Enumerations
Members Description
Append It is used to open a file if it exists and move
the file pointer to the end of the file, or
create a new file. Append can only be used
with the FileAccess.Write.
Create It is used to specify that the operating
system should create a new file. However,
if the file already exists, it will be
overwritten.
CreateNew It is used to specify that the operating
system should create a new file. However,
if the file already exists, it will throw an
exception.
©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/
Slide 6 of 38
- 7. Performing File I/O Operations and Implementing
Multithreading in Visual Basic .NET
FileMode Enumerations (Contd.)
Members Description
Open It is used to specify that the operating
system should open an existing file.
OpenOrCreate It is used to specify that the operating
system should open an existing file.
However, if the file does not exist, a new
file should be created.
Truncate It is used to specify that the operating
system should open an existing file. After
the file is opened, the file should be
truncated so that its size is zero bytes.
©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/
Slide 7 of 38
- 8. Performing File I/O Operations and Implementing
Multithreading in Visual Basic .NET
FileAccess Enumerations
Members Description
Read It is used to specify Read access to a file so
that data can be read from the file.
ReadWrite It is used to specify Read and Write access
to a file so that data can be written to and
read from the file.
Write It is used to specify Write access to a file so
that data can be written to the file.
©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/
Slide 8 of 38
- 9. Performing File I/O Operations and Implementing
Multithreading in Visual Basic .NET
FileShare Enumerations
Members Description
None It is used to decline sharing of the current
file. When this constant is used with the
FileShare enumeration, a request to open the
file will fail until the file is closed.
Read It is used to allow subsequent opening of a
file for reading. If this flag is not used, a
request to open the file for reading will fail
until the file is closed.
ReadWrite It is used to allow subsequent opening of a
file for reading or writing. If this flag is not
used, any request to open the file for writing
or reading will fail until the file is closed.
Write It is used to allow subsequent opening of a
file for writing. If this flag is not used, any
request to open the file for writing will fail
until the file is closed.
©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/
Slide 9 of 38
- 10. Performing File I/O Operations and Implementing
Multithreading in Visual Basic .NET
BinaryReader and BinaryWriter Classes
Are used to read from and write to a binary file.
StreamReader and StreamWriter Classes
Are used to read and write data as streams of
characters.
Use specific encoding to convert characters to and from
bytes.
©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/
Slide 10 of 38
- 11. Performing File I/O Operations and Implementing
Multithreading in Visual Basic .NET
The Directory Class
Enables you to work with drives and folders.
Method Usage
CreateDirectory() It is used to create the directories specified by
a path.
Delete() It is used to delete a directory and all its
contents.
Exists() It is used to determine whether the specified
path refers to an existing directory on the disk.
©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/
Slide 11 of 38
- 12. Performing File I/O Operations and Implementing
Multithreading in Visual Basic .NET
Directory Class (Contd.)
Method Usage
GetDirectoryRoot() It is used to return the root portion of a
specified path.
GetLogicalDrives() It is used to retrieve the names of the logical
drives on the current computer.
Move() It is used to move a directory and its contents
to a new path.
©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/
Slide 12 of 38
- 13. Performing File I/O Operations and Implementing
Multithreading in Visual Basic .NET
Just a Minute…
2. The code snippet generates a build error. Identify the error.
3. What is the difference between the CreateNew and Create
constants of the FileMode enumeration?
4. Complete the code to ensure that the file Product.txt is
opened with no share specification.
©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/
Slide 13 of 38
- 14. Performing File I/O Operations and Implementing
Multithreading in Visual Basic .NET
Visual Basic .NET Run-time Functions
Provide compatibility with earlier versions of Visual Basic.
Allow three types of file access:
Sequential
Random
Binary
Are defined in the System.IO.File namespace.
©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/
Slide 14 of 38
- 15. Performing File I/O Operations and Implementing
Multithreading in Visual Basic .NET
Functions Used to Access Files
Dir – It is used to retrieve the name of a file or directory that
matches a specific pattern based on the file attribute or
volume label of a file.
EOF – It is used to check whether the end of the file is
reached. When a file is opened for random or sequential
access, if the end of the file is reached, the function returns
a Boolean value True.
FileCopy – It is used to create a copy of an existing file.
FileDateTime – It is used to retrieve the date and time when
a file was created or last modified.
FileLen – It is used to retrieve the length of a file in bytes.
©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/
Slide 15 of 38
- 16. Performing File I/O Operations and Implementing
Multithreading in Visual Basic .NET
Functions Used to Access Files (Contd.)
FreeFile – It is used to retrieve the next file number
available for use by the FileOpen() function.
GetAttr – It is used to retrieve a FileAttribute value
representing the attributes of a file or directory.
Loc – It is used to retrieve a value specifying the current
read/write position within an open file.
LOF – It is used to retrieve a value representing the size, in
bytes, of a file opened using the FileOpen()function.
Seek – It is used to retrieve a value specifying the current
read/write position within a file opened using the
FileOpen()function or sets the position for the next
read/write operation within a file opened using the
FileOpen() function.
SetAttr – It sets the attribute information for a file.
©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/
Slide 16 of 38
- 17. Performing File I/O Operations and Implementing
Multithreading in Visual Basic .NET
Functions Used to Access Data from a File
With sequential access, a file can be opened in the Input,
Output, or Append mode. In the Input mode, a file is
opened for reading data from the file. In the Output or
Append mode, a file is opened for writing data to the
file. The syntax for using the FileOpen() function with
sequential access is as follows:
FileOpen(FileNumber, FileName,
OpenMode.mode type)
©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/
Slide 17 of 38
- 18. Performing File I/O Operations and Implementing
Multithreading in Visual Basic .NET
Functions Used to Access Data from a File (Contd.)
With random access, the FileOpen() function is used to
read and write data to a file without closing the file.
Random-access files store data in records, which makes it
easy to locate relevant information quickly. The syntax for
using the FileOpen() function with random access is as
follows:
FileOpen(FileNumber, FileName,
OpenMode.Random,OpenAccess=OpenAccess.Default,
OpenShare=OpenShare.Default,RecordLength=
length of the record)
©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/
Slide 18 of 38
- 19. Performing File I/O Operations and Implementing
Multithreading in Visual Basic .NET
Functions Used to Access Data from a File (Contd.)
With binary access, the FileOpen() function is used to
read or write to any byte position in a file. The syntax
for using the FileOpen()function with binary access is
as follows:
FileOpen(FileNumber, FileName,
OpenMode.Binary)
©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/
Slide 19 of 38
- 20. Performing File I/O Operations and Implementing
Multithreading in Visual Basic .NET
Functions Used to Write Data to and Read Data From
Files
Access type Functions for writing Functions for reading
data data
Sequential Print, PrintLine Input, InputString
Random FilePut FileGet
Binary FilePut FileGet
©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/
Slide 20 of 38
- 21. Performing File I/O Operations and Implementing
Multithreading in Visual Basic .NET
Just a Minute…
Complete the code to display the contents of the file named
Product.txt in the output window.
©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/
Slide 21 of 38
- 22. Performing File I/O Operations and Implementing
Multithreading in Visual Basic .NET
Problem Statement 10.D.1
The users at the call centers of Diaz Telecommunications
should be able to create a text file for storing customer
feedback on the services provided by Diaz
Telecommunications. The users should also be able to open
the file for future assessment.
©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/
Slide 22 of 38
- 23. Performing File I/O Operations and Implementing
Multithreading in Visual Basic .NET
Task List
Identify the mechanism to implement the user requirement.
Identify the controls needed to provide the user interface.
Perform the appropriate steps to design the form.
Write the code to implement the identified mechanism.
Save the application.
Run the application to validate the conditions applied on the
form.
©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/
Slide 23 of 38
- 24. Performing File I/O Operations and Implementing
Multithreading in Visual Basic .NET
Task 1: Identify the mechanism to implement the
user requirement.
Result:
You can fulfill the user requirement by using either the
classes in the System.IO model or the Visual Basic .NET
run-time functions. However, since the System.IO model is
a part of the .NET Framework class library, you should
implement the relevant classes of the System.IO model to
make full use of the .NET Framework features of Visual
Basic .NET.
©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/
Slide 24 of 38
- 25. Performing File I/O Operations and Implementing
Multithreading in Visual Basic .NET
Task 2: Identify the controls needed to provide the
user interface.
Result:
As per the plan, you need to add a RichTextBox control, a
label control, and two buttons, Read Data and Write
Data, to the form named frmCustomerFeedback.
In addition, a Reset button should be used to clear the
contents of the RichTextBox control.
To ensure that data is first written and then read from the
file, initially the Read Data button will be disabled. To
clear the contents of the RichTextBox, the Reset button will
be used.
©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/
Slide 25 of 38
- 26. Performing File I/O Operations and Implementing
Multithreading in Visual Basic .NET
Task 3: Perform the appropriate steps to design the
form.
Task 4: Write the code to implement the identified
mechanism.
Task 5: Save the application.
Task 6: Run the application to validate the conditions
applied on the form.
©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/
Slide 26 of 38
- 27. Performing File I/O Operations and Implementing
Multithreading in Visual Basic .NET
Problem Statement 10.P.1
The users at the call centers of Diaz Telecommunications
should be able to create a text file for storing product details
that are available for sale. The users should be able to read
from or write data to the file as a sequence of characters. The
users should also be able to open the file for future
assessment.
©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/
Slide 27 of 38
- 28. Performing File I/O Operations and Implementing
Multithreading in Visual Basic .NET
Threads
Help in performing multiple activities simultaneously.
MultiThreading
Is a process in which individual activities are executed on
separate threads.
Is also called free threading in Visual Basic .NET.
Enables you to create scalable applications since you can
add threads to an application as the workload increases.
©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/
Slide 28 of 38
- 29. Performing File I/O Operations and Implementing
Multithreading in Visual Basic .NET
Creating a Thread
Requires declaring a variable of the System.Threading.
Thread class and calling the constructor of the thread
class with the AddressOf operator and the name of the
procedure you want to execute on the new thread.
Example
Dim MyThread As New
System.Threading.Thread(AddressOf MySub)
Starting Execution of a Thread
You can start a thread by using the Start()method.
Stopping Execution of a Thread
You can stop the execution of a thread by calling the
Abort()method.
©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/
Slide 29 of 38
- 30. Performing File I/O Operations and Implementing
Multithreading in Visual Basic .NET
Thread Priorities
Are defined by using the Priority property.
By default set to Normal.
Can be changed by using the ThreadPriority
enumeration.
Values Description
AboveNormal Specifies that the thread has a higher
priority than Normal
BelowNormal Specifies that the thread has the lower
priority
Highest Specifies that the thread has the highest
priority
Lowest Specifies that the thread has the lowest
priority
Normal Specifies that the thread has an average
priority
©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/
Slide 30 of 38
- 31. Performing File I/O Operations and Implementing
Multithreading in Visual Basic .NET
Thread State
Is accessed by using the ThreadState property.
Can be a combination of the values in the
System.Threading.Threadstate enumeration, that is,
a thread can be in more than one state at a given time.
Can be changed by the following methods:
Thread.Sleep()
Thread.Suspend()
Thread.Interrupt()
Thread.Resume()
Thread.Abort()
©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/
Slide 31 of 38
- 32. Performing File I/O Operations and Implementing
Multithreading in Visual Basic .NET
Thread Lifecycle
Starts when an object of the System.Threading.Thread
class is created and stops when any of the following
occurs:
The method the thread is running completes
The process containing the thread terminates
The Abort() method is called for the thread object
©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/
Slide 32 of 38
- 33. Performing File I/O Operations and Implementing
Multithreading in Visual Basic .NET
Thread Lifecycle (Contd.)
The following diagram illustrates the thread states and the
methods that would cause the thread to leave each
state.
©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/
Slide 33 of 38
- 34. Performing File I/O Operations and Implementing
Multithreading in Visual Basic .NET
Multithreaded Procedures
Can accept arguments.
Can return values.
Thread Synchronization
Is done by using events in an application.
The SyncLock Statement
Ensures that multiple threads do not access shared data at
the same time.
©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/
Slide 34 of 38
- 35. Performing File I/O Operations and Implementing
Multithreading in Visual Basic .NET
Just a Minute…
There is a class called CalcClass, which has a Sub
procedure called CalculateArea(). You need to execute
this procedure in a separate thread. To execute the
CalculateArea() procedure, you need to declare a
variable of the thread type. Complete the declaration of the
variable in the code snippet.
©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/
Slide 35 of 38
- 36. Performing File I/O Operations and Implementing
Multithreading in Visual Basic .NET
Summary
In this lesson, you learned that:
The file I/O operations in Visual Basic .NET can be done in
two different ways:
Using the .NET System.IO model
Using the Visual Basic .NET run-time functions
The System.IO model specifies the use of the common
classes available to all the .NET languages.
The classes in the System.IO namespace that are mostly
used are:
FileStream
BinaryReader
©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/
Slide 36 of 38
- 37. Performing File I/O Operations and Implementing
Multithreading in Visual Basic .NET
Summary (Contd.)
BinaryWriter
StreamReader
StreamWriter
Directory
Visual Basic .NET run-time functions allow three types of file
access:
Sequential
Random
Binary
Threads can be implemented in Visual Basic by declaring a
variable of the System.Threading.Thread type.
©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/
Slide 37 of 38
- 38. Performing File I/O Operations and Implementing
Multithreading in Visual Basic .NET
Summary (Contd.)
The methods that are mostly used to manipulate a thread
are:
Start()
Sleep()
Suspend()
Resume()
Abort()
©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/
Slide 38 of 38