• Save
Vb net xp_10
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

Vb net xp_10

on

  • 485 views

 

Statistics

Views

Total Views
485
Views on SlideShare
471
Embed Views
14

Actions

Likes
0
Downloads
0
Comments
0

1 Embed 14

http://niitcourseslides.blogspot.in 14

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Vb net xp_10 Presentation Transcript

  • 1. Performing File I/O Operations and ImplementingMultithreading in Visual Basic .NETObjectivesIn this lesson, you will learn to:Identify the benefits of the .NET System.IO modelIdentify the benefits of the Visual Basic .NET run-timefunctionsIdentify the benefits of multithreadingImplement 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 ImplementingMultithreading in Visual Basic .NETFileIs a collection of bytes that has a persistent storage.I/O operations in Visual Basic .NET are handled in twodifferent 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 ImplementingMultithreading in Visual Basic .NETSystem.IO ModelSpecifies 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 ImplementingMultithreading in Visual Basic .NETThe 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 ImplementingMultithreading in Visual Basic .NETThe FileStream ClassProvides access to files and file-related information.Is used with the File class, which is contained in theSystem.IO namespace, to create, copy, delete, move, and open files.Can also be used with another class called Path, tomanipulate 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 iscreated, 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 ImplementingMultithreading in Visual Basic .NETFileMode 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 ImplementingMultithreading in Visual Basic .NETFileMode 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 ImplementingMultithreading in Visual Basic .NETFileAccess 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 ImplementingMultithreading in Visual Basic .NETFileShare 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 ImplementingMultithreading in Visual Basic .NETBinaryReader and BinaryWriter ClassesAre used to read from and write to a binary file.StreamReader and StreamWriter ClassesAre used to read and write data as streams ofcharacters.Use specific encoding to convert characters to and frombytes.©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 ImplementingMultithreading in Visual Basic .NETThe Directory ClassEnables 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 ImplementingMultithreading in Visual Basic .NETDirectory 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 ImplementingMultithreading in Visual Basic .NETJust 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 ImplementingMultithreading in Visual Basic .NETVisual Basic .NET Run-time FunctionsProvide compatibility with earlier versions of Visual Basic.Allow three types of file access:  Sequential  Random  BinaryAre 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 ImplementingMultithreading in Visual Basic .NETFunctions 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 ImplementingMultithreading in Visual Basic .NETFunctions Used to Access Files (Contd.)FreeFile – It is used to retrieve the next file numberavailable for use by the FileOpen() function.GetAttr – It is used to retrieve a FileAttribute valuerepresenting the attributes of a file or directory.Loc – It is used to retrieve a value specifying the currentread/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 theFileOpen()function or sets the position for the nextread/write operation within a file opened using theFileOpen() 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 ImplementingMultithreading in Visual Basic .NETFunctions Used to Access Data from a FileWith sequential access, a file can be opened in the Input, Output, or Append mode. In the Input mode, a file isopened for reading data from the file. In the Output orAppend mode, a file is opened for writing data to thefile. The syntax for using the FileOpen() function withsequential 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 ImplementingMultithreading in Visual Basic .NETFunctions 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 iteasy to locate relevant information quickly. The syntax forusing the FileOpen() function with random access is asfollows: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 ImplementingMultithreading in Visual Basic .NETFunctions 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 syntaxfor using the FileOpen()function with binary access isas 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 ImplementingMultithreading in Visual Basic .NETFunctions Used to Write Data to and Read Data FromFiles 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 ImplementingMultithreading in Visual Basic .NETJust a Minute…Complete the code to display the contents of the file namedProduct.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 ImplementingMultithreading in Visual Basic .NETProblem Statement 10.D.1The users at the call centers of Diaz Telecommunicationsshould be able to create a text file for storing customerfeedback on the services provided by DiazTelecommunications. The users should also be able to openthe 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 ImplementingMultithreading in Visual Basic .NETTask ListIdentify 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 ImplementingMultithreading in Visual Basic .NETTask 1: Identify the mechanism to implement theuser requirement.Result: You can fulfill the user requirement by using either theclasses in the System.IO model or the Visual Basic .NETrun-time functions. However, since the System.IO model isa part of the .NET Framework class library, you shouldimplement the relevant classes of the System.IO model to make full use of the .NET Framework features of VisualBasic .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 ImplementingMultithreading in Visual Basic .NETTask 2: Identify the controls needed to provide theuser interface.Result:As per the plan, you need to add a RichTextBox control, a label control, and two buttons, Read Data and WriteData, to the form named frmCustomerFeedback.In addition, a Reset button should be used to clear thecontents 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. Toclear the contents of the RichTextBox, the Reset button willbe 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 ImplementingMultithreading in Visual Basic .NETTask 3: Perform the appropriate steps to design theform.Task 4: Write the code to implement the identifiedmechanism.Task 5: Save the application.Task 6: Run the application to validate the conditionsapplied 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 ImplementingMultithreading in Visual Basic .NETProblem Statement 10.P.1The users at the call centers of Diaz Telecommunicationsshould be able to create a text file for storing product detailsthat are available for sale. The users should be able to readfrom or write data to the file as a sequence of characters. Theusers should also be able to open the file for futureassessment.©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 ImplementingMultithreading in Visual Basic .NETThreadsHelp in performing multiple activities simultaneously.MultiThreadingIs 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 ImplementingMultithreading in Visual Basic .NETCreating a ThreadRequires declaring a variable of the System.Threading. Thread class and calling the constructor of the threadclass with the AddressOf operator and the name of theprocedure you want to execute on the new thread. Example Dim MyThread As NewSystem.Threading.Thread(AddressOf MySub)Starting Execution of a ThreadYou can start a thread by using the Start()method.Stopping Execution of a ThreadYou can stop the execution of a thread by calling theAbort()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 ImplementingMultithreading in Visual Basic .NETThread PrioritiesAre defined by using the Priority property.By default set to Normal.Can be changed by using the ThreadPriorityenumeration. 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 ImplementingMultithreading in Visual Basic .NETThread StateIs accessed by using the ThreadState property.Can be a combination of the values in theSystem.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 ImplementingMultithreading in Visual Basic .NETThread LifecycleStarts when an object of the System.Threading.Thread class is created and stops when any of the followingoccurs:  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 ImplementingMultithreading in Visual Basic .NETThread Lifecycle (Contd.)The following diagram illustrates the thread states and the methods that would cause the thread to leave eachstate.©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 ImplementingMultithreading in Visual Basic .NETMultithreaded ProceduresCan accept arguments.Can return values.Thread SynchronizationIs done by using events in an application.The SyncLock StatementEnsures 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 ImplementingMultithreading in Visual Basic .NETJust a Minute…There is a class called CalcClass, which has a Subprocedure called CalculateArea(). You need to executethis procedure in a separate thread. To execute theCalculateArea() procedure, you need to declare avariable of the thread type. Complete the declaration of thevariable 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 ImplementingMultithreading in Visual Basic .NETSummaryIn 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 functionsThe System.IO model specifies the use of the commonclasses 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 ImplementingMultithreading in Visual Basic .NETSummary (Contd.)  BinaryWriter  StreamReader  StreamWriter  DirectoryVisual Basic .NET run-time functions allow three types of file access:  Sequential  Random  BinaryThreads 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 ImplementingMultithreading in Visual Basic .NETSummary (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