CATEGORII DOCUMENTE |
Asp | Autocad | C | Dot net | Excel | Fox pro | Html | Java |
Linux | Mathcad | Photoshop | Php | Sql | Visual studio | Windows | Xml |
At this point we are ready to concentrate our attention on a practical example that will be useful to demonstrate what we have seen thus far. The DataImport example, included here, is a typical application that waits for files to arrive in a specific directory before importing them into a SQL Server database. The code for this application, as with the rest of the code in this book, can be found at the Apress web site. Below we outline the classes that will be used in this example:
FileSystemWatcher: This allows developers to specify the directory to monitor and to raise an event when something changes (for example, a new file is created or removed). This class is contained in the System.IO namespace of the .NET Framework class library.
TextWriterTraceListener: This implements our own tracing functionality.
Thread: This, which you've seen many times before, allows us to start a new thread to import data into the database.
Many classes from the SqlClient namespace necessary to manage the SQL Server database connection and update.
The first release of the DataImport application contains some logical errors that you will discover using tracing functionality. In that way you can have a good example about log (trace) files and their importance.
To learn more about the ADO.NET classes, please refer to Professional ADO.NET Programming (ISBN 1-86100-527-X), or ADO.NET Programmer's Reference (ISBN 1-86100-558-X).
Let's start analyzing the code of the DataImport example:
using System;The above code implements an infinite loop, which waits for the file creation event to be raised. The WaitForChangedResult object will contain information about the file created. For example, the code uses the Name property to trace the name of the discovered file.
catch (Exception e)The
above
Then, after writing the code for catching and dealing with any exceptions that may occur, the code is complete.
To test the application you have to follow these steps:
Create a C:temp directory to contain the XML file
Run the DataImport application
Copy the authors.xml file into the C:temp directory
As a final result you should find the DataImport.log file in the C:directory having content similar to this:
01/05/2002 12:23:01 - Found: authors.xml fileThe authors.xml file is not that large so the total time is less than one second.
All seems to be working well, but obviously, everything hasn't been accounted for. So far, we have tested our application with a very small file size, so when the application receives the file creation event and opens the file, the process that copies it into the directory finishes its task of closing the file. What happens when you receive a huge file? Well, when the thread tries to access the XML file and fill the DataSet object, it receives an access-denied error caused by attempting to open a file already in use by the copier task. Try to test the application again by copying the huge_authors.xml file instead. Since you have used tracing messages, you may find the following error in the log file:
4/14/2002 1:29:00 PM - Found: huge_authors.xml fileThis is a kind of error that the debugger often fails to catch because the time used to launch it and the time to step through the code is often sufficient to copy the file. It may also not occur on your machine. It depends on the speed of your disk access and the amount of memory you have (so how much the application is slowed down).
The error message suggests a possible solution that you should add to the application to resolve the error. Before calling the ReadXml() method, you should try to open the file with exclusive access. If an error occurs, then you can suspend the thread for few seconds, trying again when the file can be processed. Let's see how the code changes in DataImport2, by adding the GetFileAccess() method:
private bool GetFileAccess()The GetFileAccess() method has been added in order to return a Boolean value indicating whether you can have exclusive access to the file or not. The method simply tries to open the file with the share access property set to None:
public void Import()The Import() method provided by the ImportData class will try to get exclusive access to the file. If the file is still opened by the copier task, the thread will be suspended for five seconds. So, the GetFileAccess() method will be called until the source file can be opened.
We have seen practically how the tracing functionalities can be useful to understand the application behavior during run-time execution.
Politica de confidentialitate | Termeni si conditii de utilizare |
Vizualizari: 771
Importanta:
Termeni si conditii de utilizare | Contact
© SCRIGROUP 2024 . All rights reserved