2017-02-27 22 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;});' –

相關問題