2017-02-27 9 views
0

안녕하세요, GUI를로드하는 동안 백그라운드 작업을 수행하기 위해 스레드를 만들어야하는 응용 프로그램을 작성했습니다. 나는이 오류 주위에 방법을 찾을 수 있습니다 할 상관없이 그러나 :Vala 스레딩 : 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'는 지원되는 제네릭 형식 인수가 아니며 '?'를 사용한다. 상자 값 유형 ' 수정 :'Thread thread = new Thread .try ("Conntections Thread.",() => {devices_online (listmodel); return null;});' –