共用方式為


IWatchService Interface

Definition

A watch service that <em>watches</em> registered objects for changes and events.

[Android.Runtime.Register("java/nio/file/WatchService", "", "Java.Nio.FileNio.IWatchServiceInvoker", ApiSince=26)]
public interface IWatchService : IDisposable, Java.Interop.IJavaPeerable, Java.IO.ICloseable
[<Android.Runtime.Register("java/nio/file/WatchService", "", "Java.Nio.FileNio.IWatchServiceInvoker", ApiSince=26)>]
type IWatchService = interface
    interface ICloseable
    interface IJavaObject
    interface IDisposable
    interface IJavaPeerable
Attributes
Implements

Remarks

A watch service that <em>watches</em> registered objects for changes and events. For example a file manager may use a watch service to monitor a directory for changes so that it can update its display of the list of files when files are created or deleted.

A Watchable object is registered with a watch service by invoking its Watchable#register register method, returning a WatchKey to represent the registration. When an event for an object is detected the key is <em>signalled</em>, and if not currently signalled, it is queued to the watch service so that it can be retrieved by consumers that invoke the #poll() poll or #take() take methods to retrieve keys and process events. Once the events have been processed the consumer invokes the key's WatchKey#reset reset method to reset the key which allows the key to be signalled and re-queued with further events.

Registration with a watch service is cancelled by invoking the key's WatchKey#cancel cancel method. A key that is queued at the time that it is cancelled remains in the queue until it is retrieved. Depending on the object, a key may be cancelled automatically. For example, suppose a directory is watched and the watch service detects that it has been deleted or its file system is no longer accessible. When a key is cancelled in this manner it is signalled and queued, if not currently signalled. To ensure that the consumer is notified the return value from the reset method indicates if the key is valid.

A watch service is safe for use by multiple concurrent consumers. To ensure that only one consumer processes the events for a particular object at any time then care should be taken to ensure that the key's reset method is only invoked after its events have been processed. The #close close method may be invoked at any time to close the service causing any threads waiting to retrieve keys, to throw ClosedWatchServiceException.

File systems may report events faster than they can be retrieved or processed and an implementation may impose an unspecified limit on the number of events that it may accumulate. Where an implementation <em>knowingly</em> discards events then it arranges for the key's WatchKey#pollEvents pollEvents method to return an element with an event type of StandardWatchEventKinds#OVERFLOW OVERFLOW. This event can be used by the consumer as a trigger to re-examine the state of the object.

When an event is reported to indicate that a file in a watched directory has been modified then there is no guarantee that the program (or programs) that have modified the file have completed. Care should be taken to coordinate access with other programs that may be updating the file. The java.nio.channels.FileChannel FileChannel class defines methods to lock regions of a file against access by other programs.

<h2>Platform dependencies</h2>

The implementation that observes events from the file system is intended to map directly on to the native file event notification facility where available, or to use a primitive mechanism, such as polling, when a native facility is not available. Consequently, many of the details on how events are detected, their timeliness, and whether their ordering is preserved are highly implementation specific. For example, when a file in a watched directory is modified then it may result in a single StandardWatchEventKinds#ENTRY_MODIFY ENTRY_MODIFY event in some implementations but several events in other implementations. Short-lived files (meaning files that are deleted very quickly after they are created) may not be detected by primitive implementations that periodically poll the file system to detect changes.

If a watched file is not located on a local storage device then it is implementation specific if changes to the file can be detected. In particular, it is not required that changes to files carried out on remote systems be detected.

Added in 1.7.

Java documentation for java.nio.file.WatchService.

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Properties

Handle

Gets the JNI value of the underlying Android object.

(Inherited from IJavaObject)
JniIdentityHashCode

Returns the value of java.lang.System.identityHashCode() for the wrapped instance.

(Inherited from IJavaPeerable)
JniManagedPeerState

State of the managed peer.

(Inherited from IJavaPeerable)
JniPeerMembers

Member access and invocation support.

(Inherited from IJavaPeerable)
PeerReference

Returns a JniObjectReference of the wrapped Java object instance.

(Inherited from IJavaPeerable)

Methods

Close()

Closes this watch service.

Disposed()

Called when the instance has been disposed.

(Inherited from IJavaPeerable)
DisposeUnlessReferenced()

If there are no outstanding references to this instance, then calls Dispose(); otherwise, does nothing.

(Inherited from IJavaPeerable)
Finalized()

Called when the instance has been finalized.

(Inherited from IJavaPeerable)
Poll()

Retrieves and removes the next watch key, or null if none are present.

Poll(Int64, TimeUnit)

Retrieves and removes the next watch key, waiting if necessary up to the specified wait time if none are yet present.

SetJniIdentityHashCode(Int32)

Set the value returned by JniIdentityHashCode.

(Inherited from IJavaPeerable)
SetJniManagedPeerState(JniManagedPeerStates) (Inherited from IJavaPeerable)
SetPeerReference(JniObjectReference)

Set the value returned by PeerReference.

(Inherited from IJavaPeerable)
Take()

Retrieves and removes next watch key, waiting if none are yet present.

UnregisterFromRuntime()

Unregister this instance so that the runtime will not return it from future Java.Interop.JniRuntime+JniValueManager.PeekValue invocations.

(Inherited from IJavaPeerable)

Extension Methods

JavaCast<TResult>(IJavaObject)

Performs an Android runtime-checked type conversion.

JavaCast<TResult>(IJavaObject)
GetJniTypeName(IJavaPeerable)

Applies to