Tải bản đầy đủ - 0 (trang)
4-17. Ensure That Only One Instance of an Application Can Execute Concurrently

4-17. Ensure That Only One Instance of an Application Can Execute Concurrently

Tải bản đầy đủ - 0trang

CHAPTER 4 ■ THREADS, PROCESSES, AND SYNCHRONIZATION



exiting if the Mutex cannot be acquired, you can ensure that only one instance of your application is

running.



The Code

This example uses a Mutex named MutexExample to ensure that only a single instance of the example can

execute:

using System;

using System.Threading;

namespace Apress.VisualCSharpRecipes.Chapter04

{

class Recipe04_17

{

public static void Main()

{

// A Boolean that indicates whether this application has

// initial ownership of the Mutex.

bool ownsMutex;

// Attempt to create and take ownership of a Mutex named

// MutexExample.

using (Mutex mutex =

new Mutex(true, "MutexExample", out ownsMutex))

{

// If the application owns the Mutex it can continue to execute;

// otherwise, the application should exit.

if (ownsMutex)

{

Console.WriteLine("This application currently owns the" +

" mutex named MutexExample. Additional instances of" +

" this application will not run until you release" +

" the mutex by pressing Enter.");

Console.ReadLine();

// Release the mutex.

mutex.ReleaseMutex();

}

else

{

Console.WriteLine("Another instance of this application " +

" already owns the mutex named MutexExample. This" +

" instance of the application will terminate.");

}

}



202



www.it-ebooks.info



CHAPTER 4 ■ THREADS, PROCESSES, AND SYNCHRONIZATION



// Wait to continue.

Console.WriteLine("Main method complete. Press Enter.");

Console.ReadLine();

}

}

}



■ Note If you do not construct the Mutex in a using statement and encapsulate the body of your application in the

body of the using block as shown in this example, in long-running applications the garbage collector may dispose

of the Mutex if it is not referenced after initial creation. This will result in releasing the Mutex and allow additional

instances of the application to execute concurrently. In these circumstances, you should include the statement

System.GC.KeepAlive(mutex) to ensure the Mutex is not garbage collected. Thanks to Michael A. Covington for

highlighting this possibility.



203



www.it-ebooks.info



www.it-ebooks.info



CHAPTER 5

■■■



Files, Directories, and I/O

The Microsoft .NET Framework I/O classes fall into two basic categories. First are the classes that

retrieve information from the file system and allow you to perform file system operations such as

copying files and moving directories. Two examples include the FileInfo and the DirectoryInfo classes.

The second and possibly more important category includes a broad range of classes that allow you to

read and write data from all types of streams. Streams can correspond to binary or text files, a file in an

isolated store, a network connection, or even a memory buffer. In all cases, the way you interact with a

stream is the same. This chapter describes how to use the file system classes and a wide range of streambased classes.

The recipes in this chapter describe how to do the following:





Retrieve or modify information about a file, directory, or a drive (recipes 5-1, 5-2,

5-4, 5-5, and 5-16)







Copy, move, and delete files and directories (recipe 5-3)







Show a directory tree in a Microsoft Windows-based application use the common

file dialog boxes and monitor the file system for changes (recipes 5-6, 5-17, and 519)







Create, read, and write text and binary files; create temporary files; and use

isolated storage (recipes 5-7, 5-8, 5-9, 5-15, 5-18, and 5-21)







Search for specific files and test files for equality and work with strings that

contain path information (recipes 5-10, 5-11, 5-12, 5-13, and 5-14)







Write to a COM port (recipe 5-20)







Retrieve or modify the access control lists (ACLs) of a file or directory (recipe 5-22)







Compress and decompress data (recipe 5-23)







Log data to a file and process a log file (recipes 5-24 and 5-25)







Communicate between processes (recipes 5-26)



205



www.it-ebooks.info



CHAPTER 5 ■ FILES, DIRECTORIES, AND I/O



5-1. Retrieve Information About a File, Directory, or Drive

Problem

You need to retrieve information about a file, directory, or drive.



Solution

Create a new System.IO.FileInfo, System.IO.DirectoryInfo, or System.IO.DriveInfo object, depending

on the type of resource about which you need to retrieve information. Supply the path of the resource to

the constructor, and then you will be able to retrieve information through the properties of the class.



How It Works

To create a FileInfo, DirectoryInfo, or DriveInfo object, you supply a relative or fully qualified path in

the constructor. You can retrieve information through the corresponding object properties. Table 5-1

lists some of the key members that are found in these objects.

Table 5-1. Key Members for Files, Directories, and Drives



Member



Applies To



Description



Exists



FileInfo and

DirectoryInfo



Returns true or false, depending on whether a file or a

directory exists at the specified location.



Attributes



FileInfo and

DirectoryInfo



Returns one or more values from the

System.IO.FileAttributes enumeration, which represents

the attributes of the file or the directory.



CreationTime,

LastAccessTime,



FileInfo and

DirectoryInfo



Return System.DateTime and LastWriteTime instances that

describe when a file or a directory was created, last

accessed, and last updated, respectively.



FullName, Name, and

Extension



FileInfo and

DirectoryInfo



Return a string that represents the fully qualified name,

the directory, or the file name (with extension), and the

extension on its own.



IsReadOnly



FileInfo



Returns true or false, depending on whether a file is readonly.



Length



FileInfo



Returns the file size as a number of bytes.



206



www.it-ebooks.info



CHAPTER 5 ■ FILES, DIRECTORIES, AND I/O



Member



Applies To



Description



DirectoryName and

Directory



FileInfo



DirectoryName returns the name of the parent directory as

a string. Directory returns a full DirectoryInfo object that

represents the parent directory and allows you to retrieve

more information about it.



Parent and Root



DirectoryInfo



Return a DirectoryInfo object that represents the parent

or root directory.



CreateSubdirectory



DirectoryInfo



Creates a directory with the specified name in the

directory represented by the DirectoryInfo object. It also

returns a new DirectoryInfo object that represents the

subdirectory.



GetDirectories



DirectoryInfo



Returns an array of DirectoryInfo objects, with one

element for each subdirectory contained in this directory.



GetFiles



DirectoryInfo



Returns an array of FileInfo objects, with one element for

each file contained in this directory.



EnumerateFiles



DirectoryInfo



Returns an IEnumerable of FileInfo objects, with one

element for each file contained in this directory



EnumerateDirectories



DirectoryInfo



Returns an IEnumerable of DirectoryInfo objects, with one

element for each subdirectory.



DriveType



DriveInfo



Returns a DriveType enumeration value that represents

the type of the specified drive; for example, Fixed or CD

Rom.



AvailableFreeSpace



DriveInfo



Returns a long that represents the free space available in

the drive.



GetDrives



DriveInfo



Returns an array of DriveInfo objects that represents the

logical drives in the computer.



The following are a few points to note while working with these objects:





FileInfo and DirectoryInfo classes derive from the abstract FileSystemInfo class,

which defines common methods like CreationTime, Exists, and so on. The

DriveInfo class does not inherit from this base class, so it does not provide some

of the common members available in the other two classes.



207



www.it-ebooks.info



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

4-17. Ensure That Only One Instance of an Application Can Execute Concurrently

Tải bản đầy đủ ngay(0 tr)

×