2013-04-15 5 views
2

그래서 나는 이러한 변수 C#을 멤버는

List<string> files, images = new List<string>(); 
string rootStr; 

그리고이 스레드 기능이 참조하는 인스턴스에 액세스 할 수 없습니다

private static int[] thread_search(string root,List<string> files, List<string> images) 

하지만 스레드를 시작하려고하면

trd = new Thread(new ThreadStart(this.thread_search(rootStr,files,images))); 

이 오류가 발생합니다 :

Error 1 Member 'UnusedImageRemover.Form1.thread_search(string, System.Collections.Generic.List, System.Collections.Generic.List)' cannot be accessed with an instance reference; qualify it with a type name instead E:\Other\Projects\UnusedImageRemover\UnusedImageRemover\Form1.cs 149 46 UnusedImageRemover

내가 뭘 잘못하고 있는지 말해 줄 수 있습니까?

답변

7

정적 메서드가 있습니다. 인스턴스에 속하지 않습니다. this은 현재 인스턴스를 참조하지만 정적이어서 의미가 없습니다.

this.을 제거하면 좋을 것입니다.

편집

this. 제거하는 것은 당신에게 다른 예외를 가져옵니다. 대리자를 ThreadStart 생성자에 전달해야하며이 메서드를 너무 일찍 호출하고 결과 (int[])를 전달합니다. 당신이 있다면 스레드와 폐쇄에 읽어 싶을 것이다 - 이제 스레드가 매개 변수에 대한 폐쇄와 함께, 검색 기능에 대리인을 가지고

static void Main(string[] args) { 
    List<string> files = new List<string>(), images = new List<string>(); 
    string rootStr = ""; 

    var trd = new Thread(new ThreadStart(() => thread_search(rootStr, files, images))); 
    trd.Start(); 
} 

private static int[] thread_search(string root, List<string> files, List<string> images { 
    return new[] { 1, 2, 3 }; 
} 

: 당신처럼, 대신 람다에 전달할 수 있습니다 이미 그들에게 익숙하지 않다.

+0

'TRD = 새 스레드 (newThreadStart (thread_search (rootStr, 파일, 이미지)));' 이 다음과 같은 오류가 발생합니다 방법 이름 지금 당신 때문에 다른 문제가있는 것 \t @Joe 이노스 –

+0

예상을 너무 빨리 메서드를 호출합니다. 거기에 도움이되는 편집을 추가하겠습니다. –

+0

고마워요 :) –