vb .net tutorial - 10

38
Performing File I/O Operations and Implementing Multithreading in Visual Basic .NET ©NII T Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/ Slide 1 of 38 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

Upload: mathes99994840202

Post on 15-Nov-2014

38 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: VB .net tutorial - 10

Performing File I/O Operations and Implementing Multithreading in Visual Basic .NET

©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/ Slide 1 of 38

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

Page 2: VB .net tutorial - 10

Performing File I/O Operations and Implementing Multithreading in Visual Basic .NET

©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/ Slide 2 of 38

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

Page 3: VB .net tutorial - 10

Performing File I/O Operations and Implementing Multithreading in Visual Basic .NET

©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/ Slide 3 of 38

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

Page 4: VB .net tutorial - 10

Performing File I/O Operations and Implementing Multithreading in Visual Basic .NET

©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/ Slide 4 of 38

The File Hierarchy in the System.IO Model

Page 5: VB .net tutorial - 10

Performing File I/O Operations and Implementing Multithreading in Visual Basic .NET

©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/ Slide 5 of 38

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.

Page 6: VB .net tutorial - 10

Performing File I/O Operations and Implementing Multithreading in Visual Basic .NET

©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/ Slide 6 of 38

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.

Page 7: VB .net tutorial - 10

Performing File I/O Operations and Implementing Multithreading in Visual Basic .NET

©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/ Slide 7 of 38

FileMode Enumerations (Contd.)

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.

Members Description

Page 8: VB .net tutorial - 10

Performing File I/O Operations and Implementing Multithreading in Visual Basic .NET

©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/ Slide 8 of 38

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.

Page 9: VB .net tutorial - 10

Performing File I/O Operations and Implementing Multithreading in Visual Basic .NET

©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/ Slide 9 of 38

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.

Page 10: VB .net tutorial - 10

Performing File I/O Operations and Implementing Multithreading in Visual Basic .NET

©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/ Slide 10 of 38

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.

Page 11: VB .net tutorial - 10

Performing File I/O Operations and Implementing Multithreading in Visual Basic .NET

©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/ Slide 11 of 38

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.

Page 12: VB .net tutorial - 10

Performing File I/O Operations and Implementing Multithreading in Visual Basic .NET

©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/ Slide 12 of 38

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.

Page 13: VB .net tutorial - 10

Performing File I/O Operations and Implementing Multithreading in Visual Basic .NET

©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/ Slide 13 of 38

Just a Minute…

1. The code snippet generates a build error. Identify the error.

2. What is the difference between the CreateNew and Create constants of the FileMode enumeration?

3. Complete the code to ensure that the file Product.txt is opened with no share specification.

Page 14: VB .net tutorial - 10

Performing File I/O Operations and Implementing Multithreading in Visual Basic .NET

©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/ Slide 14 of 38

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.

Page 15: VB .net tutorial - 10

Performing File I/O Operations and Implementing Multithreading in Visual Basic .NET

©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/ Slide 15 of 38

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.

Page 16: VB .net tutorial - 10

Performing File I/O Operations and Implementing Multithreading in Visual Basic .NET

©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/ Slide 16 of 38

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.

Page 17: VB .net tutorial - 10

Performing File I/O Operations and Implementing Multithreading in Visual Basic .NET

©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/ Slide 17 of 38

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)

Page 18: VB .net tutorial - 10

Performing File I/O Operations and Implementing Multithreading in Visual Basic .NET

©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/ Slide 18 of 38

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)

Page 19: VB .net tutorial - 10

Performing File I/O Operations and Implementing Multithreading in Visual Basic .NET

©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/ Slide 19 of 38

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)

Page 20: VB .net tutorial - 10

Performing File I/O Operations and Implementing Multithreading in Visual Basic .NET

©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/ Slide 20 of 38

Functions Used to Write Data to and Read Data From Files

Access type Functions for writing data

Functions for reading data

Sequential Print, PrintLine Input, InputString

Random FilePut FileGet

Binary FilePut FileGet

Page 21: VB .net tutorial - 10

Performing File I/O Operations and Implementing Multithreading in Visual Basic .NET

©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/ Slide 21 of 38

Just a Minute…

Complete the code to display the contents of the file named Product.txt in the output window.

Page 22: VB .net tutorial - 10

Performing File I/O Operations and Implementing Multithreading in Visual Basic .NET

©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/ Slide 22 of 38

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.

Page 23: VB .net tutorial - 10

Performing File I/O Operations and Implementing Multithreading in Visual Basic .NET

©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/ Slide 23 of 38

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.

Page 24: VB .net tutorial - 10

Performing File I/O Operations and Implementing Multithreading in Visual Basic .NET

©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/ Slide 24 of 38

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.

Page 25: VB .net tutorial - 10

Performing File I/O Operations and Implementing Multithreading in Visual Basic .NET

©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/ Slide 25 of 38

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.

Page 26: VB .net tutorial - 10

Performing File I/O Operations and Implementing Multithreading in Visual Basic .NET

©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/ Slide 26 of 38

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.

Page 27: VB .net tutorial - 10

Performing File I/O Operations and Implementing Multithreading in Visual Basic .NET

©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/ Slide 27 of 38

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.

Page 28: VB .net tutorial - 10

Performing File I/O Operations and Implementing Multithreading in Visual Basic .NET

©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/ Slide 28 of 38

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

Page 29: VB .net tutorial - 10

Performing File I/O Operations and Implementing Multithreading in Visual Basic .NET

©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/ Slide 29 of 38

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.

Page 30: VB .net tutorial - 10

Performing File I/O Operations and Implementing Multithreading in Visual Basic .NET

©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/ Slide 30 of 38

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

Page 31: VB .net tutorial - 10

Performing File I/O Operations and Implementing Multithreading in Visual Basic .NET

©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/ Slide 31 of 38

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()

Page 32: VB .net tutorial - 10

Performing File I/O Operations and Implementing Multithreading in Visual Basic .NET

©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/ Slide 32 of 38

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

Page 33: VB .net tutorial - 10

Performing File I/O Operations and Implementing Multithreading in Visual Basic .NET

©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/ Slide 33 of 38

Thread Lifecycle (Contd.)

The following diagram illustrates the thread states and the methods that would cause the thread to leave each

state.

Page 34: VB .net tutorial - 10

Performing File I/O Operations and Implementing Multithreading in Visual Basic .NET

©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/ Slide 34 of 38

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.

Page 35: VB .net tutorial - 10

Performing File I/O Operations and Implementing Multithreading in Visual Basic .NET

©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/ Slide 35 of 38

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.

Page 36: VB .net tutorial - 10

Performing File I/O Operations and Implementing Multithreading in Visual Basic .NET

©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/ Slide 36 of 38

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

Page 37: VB .net tutorial - 10

Performing File I/O Operations and Implementing Multithreading in Visual Basic .NET

©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/ Slide 37 of 38

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.

Page 38: VB .net tutorial - 10

Performing File I/O Operations and Implementing Multithreading in Visual Basic .NET

©NIIT Performing File I/O Operations and Implementing Multithreading in VB .NET/Lesson 10/ Slide 38 of 38

Summary (Contd.)

The methods that are mostly used to manipulate a thread are:

Start()

Sleep()

Suspend()

Resume()

Abort()