Vala 线程:调用 void 方法不允许作为表达式
Vala Threading: invocation of void method not allowed as expression
嘿,我一直在编写一个应用程序,我需要在其中创建线程以在加载 GUI 时执行后台任务。但是,无论我做什么,我都可以找到解决此错误的方法:
error: invocation of void method not allowed as expression
Thread<void> thread = new Thread<void>.try("Conntections Thread.", devices_online(listmodel));
有问题的行是创建一个调用 "devices_online" 方法的新线程。
正在执行的完整代码是:
try {
Thread<void> thread = new Thread<void>.try("Conntections Thread.", devices_online(listmodel));
}catch(Error thread_error){
//console print thread error message
stdout.printf("%s", thread_error.message);
}
方法是:
private void devices_online(Gtk.ListStore listmodel){
//clear the listview
listmodel.clear();
//list of devices returned after connection check
string[] devices = list_devices();
//loop through the devices getting the data and adding the device
//to the listview GUI
foreach (var device in devices) {
string name = get_data("name", device);
string ping = get_data("ping", device);
listmodel.append (out iter);
listmodel.set (iter, 0, name, 1, device, 2, ping);
}
}
我用 Google 搜索了很多,但 Vala 并不是最流行的语言。有帮助吗?
就像编译器错误所说的那样,您通过调用方法得到了一个空值。然后你试图将 void 值传递给线程构造函数。
Thread<void> thread = new Thread<void>
.try ("Conntections Thread.", devices_online (listmodel));
Thread<T>.try ()
的第二个 cunstructor 参数需要一个 ThreadFunc<T>
类型的委托,您不满意。
您混淆了方法调用和方法委托。
您可以传递一个匿名函数来解决这个问题:
Thread<void> thread = new Thread<void>
.try ("Conntections Thread.", () => { devices_online (listmodel); });
嘿,我一直在编写一个应用程序,我需要在其中创建线程以在加载 GUI 时执行后台任务。但是,无论我做什么,我都可以找到解决此错误的方法:
error: invocation of void method not allowed as expression
Thread<void> thread = new Thread<void>.try("Conntections Thread.", devices_online(listmodel));
有问题的行是创建一个调用 "devices_online" 方法的新线程。
正在执行的完整代码是:
try {
Thread<void> thread = new Thread<void>.try("Conntections Thread.", devices_online(listmodel));
}catch(Error thread_error){
//console print thread error message
stdout.printf("%s", thread_error.message);
}
方法是:
private void devices_online(Gtk.ListStore listmodel){
//clear the listview
listmodel.clear();
//list of devices returned after connection check
string[] devices = list_devices();
//loop through the devices getting the data and adding the device
//to the listview GUI
foreach (var device in devices) {
string name = get_data("name", device);
string ping = get_data("ping", device);
listmodel.append (out iter);
listmodel.set (iter, 0, name, 1, device, 2, ping);
}
}
我用 Google 搜索了很多,但 Vala 并不是最流行的语言。有帮助吗?
就像编译器错误所说的那样,您通过调用方法得到了一个空值。然后你试图将 void 值传递给线程构造函数。
Thread<void> thread = new Thread<void>
.try ("Conntections Thread.", devices_online (listmodel));
Thread<T>.try ()
的第二个 cunstructor 参数需要一个 ThreadFunc<T>
类型的委托,您不满意。
您混淆了方法调用和方法委托。
您可以传递一个匿名函数来解决这个问题:
Thread<void> thread = new Thread<void>
.try ("Conntections Thread.", () => { devices_online (listmodel); });