gpt4 book ai didi

multithreading - Vala:创建线程导致属性丢失

转载 作者:行者123 更新时间:2023-12-03 12:56:39 24 4
gpt4 key购买 nike

我目前正在尝试学习一些 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 创建线程时,构造函数中设置的属性再次为空,导致命令行上出现以下输出:
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)

最佳答案

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

关于multithreading - Vala:创建线程导致属性丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10365004/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com