2

我目前正在尝试学习一些 vala。阅读教程后,我想将一些用 java 编写的应用程序移植到 vala。

因此,我尝试编写一个监视文件夹更改的应用程序。此监视器应作为单独的线程生成,以便应用程序可以执行其他监视操作。在java中轻松工作。

在对如何监控进行了一些研究之后(顺便说一句,stackoverflow 受到了影响),我最终得到了以下代码:

亚军:

public class App : Object 
    {
    private const string FOLDER_TO_WATCH = "/home/chr/Work/vala-monitor-test/tmp/";

    public App() {

        GLib.MainLoop loop = new GLib.MainLoop();

        stdout.printf("Folder: %s\n", FOLDER_TO_WATCH);

        try {
            var daemon = new MonitorDaemon( File.new_for_path( FOLDER_TO_WATCH ) );
            unowned Thread<void*> t = Thread.create<void*>( daemon.watch, true );
        }
        catch (ThreadError err) {
            stderr.printf("Error occured: %s", err.message);
        }
        catch (MonitorError err) {
            stderr.printf("Error occured: %s", err.message);
        }

        stdout.printf("waiting..\n");

        loop.run();

    }

    static int main( string[] args ) 
    {           

        if (!Thread.supported ()) {
            stderr.printf ("Cannot run without thread support.\n");
            return 1;
        }

        new App();
        return 0;

    }

}

监控类:

errordomain MonitorError {
    NOT_A_DIRECTORY
}

public class MonitorDaemon : Object {

    // properties
    public File watched_folder { get; construct set; }

    // internal
    private bool _stop = false;


    public MonitorDaemon ( File folder_to_watch ) throws MonitorError {

        if ( folder_to_watch.query_file_type ( FileQueryInfoFlags.NONE ) != FileType.DIRECTORY ) {
            stderr.printf("Unable to watch: Not a directory\n");
            throw new MonitorError.NOT_A_DIRECTORY("Not a directory");
        }

        this.watched_folder = folder_to_watch;

        stdout.printf("Ready to watch %s\n", this.watched_folder.get_path() );

    }

    public void* watch() {

        try {
            FileMonitor monitor = this.watched_folder.monitor_directory ( FileMonitorFlags.NONE );
            monitor.changed.connect( event_happened );
        }
        catch (IOError err) {
            stderr.printf("Error occured: %s", err.message);
        }

        stdout.printf( "Started watching folder: %s\n", this.watched_folder.get_path() );

        while (!this._stop) { }

        stdout.printf( "Stopped watching folder: %s\n", this.watched_folder.get_path() );

        return null;

    }

    public void stop() {
        // cleanup..            
        this._stop = true;
    }           

    private void event_happened ( File file, File? other_file, FileMonitorEvent event_type ) {
        stdout.printf( "File '%s': %s\n", file.get_path(), event_type.to_string() );
    }

}

当为 MonitorDaemon.watch 方法创建线程时,构造函数中设置的属性再次为 null,从而在命令行上产生以下输出:

Folder: /home/chr/Work/vala-monitor-test/tmp/
Ready to watch /home/chr/Work/vala-monitor-test/tmp/
waiting..

(process:7159): GLib-GIO-CRITICAL **: g_file_monitor_directory: assertion `G_IS_FILE (file)' failed

(process:7159): GLib-GObject-WARNING **: invalid (NULL) pointer instance

(process:7159): GLib-GObject-CRITICAL **: g_signal_connect_object: assertion `G_TYPE_CHECK_INSTANCE (instance)' failed

(process:7159): GLib-GIO-CRITICAL **: g_file_get_path: assertion `G_IS_FILE (file)' failed
Started watching folder: (null)
4

1 回答 1

3

Vala 是引用计数的。退出 try{} 块时,它会被取消引用,然后被释放。您必须让 MonitorDaemon 在某个地方保持活动状态。

于 2012-04-30T21:37:00.270 回答