2017-02-27 45 views
0

嘿,我一直在编写一个应用程序,在该应用程序中,我需要创建线程来执行加载GUI时的后台任务。但是不管我做我能找到解决这个错误的方式:Vala Threading:不允许调用void方法作为表达式

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); 
    } 

} 

香港专业教育学院做了这么多Googleing但瓦拉不正是最流行的语言。任何帮助?

回答

2

就像编译器错误说的,你通过调用一个方法来得到一个void。然后你试图将void值传递给线程构造函数。

Thread<void> thread = new Thread<void> 
    .try ("Conntections Thread.", devices_online (listmodel)); 

Thread<T>.try()第二cunstructor参数预计ThreadFunc<T>类型的delagate你是不是满意。

您正在将方法调用与方法委托混淆。

你可以传递一个匿名函数来解决这个问题:

Thread<void> thread = new Thread<void> 
    .try ("Conntections Thread.",() => { devices_online (listmodel); }); 
+0

感谢您的答复。我试过你的修复,虽然它抛出了一些错误,我可以通过以下操作绕过这些错误: 错误:'void'不是受支持的泛型类型参数,请使用?到盒值类型' 修复:'线程线程=新线程 .try(“Conntections Thread。”,()=> {devices_online(listmodel); return null;});' –