2013-02-16 2 views
6

(내가 MonoDevelop IDE에서 컴파일) 내가 오류가 나타납니다 내 프로그램을 컴파일하는 동안 다음은호출은 다음과 같은 방법 또는 속성 С 번호 사이의 모호

Error CS0121: The call is ambiguous between the following methods or properties: System.Threading.Thread.Thread(System.Threading.ThreadStart)' and System.Threading.Thread.Thread(System.Threading.ParameterizedThreadStart)' (CS0121)

코드의 일부를. 관심

답변

8

delegate { ... }ThreadStart을 포함 모든 대리자 형식을 할당 할 수있는 익명의 방법입니다 및 ParameterizedThreadStart. 스레드 클래스는 두 인수 유형으로 생성자 오버로드를 제공하기 때문에 어느 생성자 오버로드를 의미하는지 모호합니다.

delegate() { ... } (괄호에 유의하십시오.)은 인수가없는 익명 메소드입니다. Action 또는 ThreadStart과 같이 인수가없는 형식을 위임하려면 으로 할당 할 수 있습니다. 당신이 ParameterizedThreadStart 생성자 오버로드를 사용하려는 경우

그래서, 당신은 ThreadStart 생성자 오버로드를 사용하려는 경우

Thread thread = new Thread(delegate() { 

에 코드를 변경하거나

Thread thread = new Thread(delegate(object state) { 

합니다.

2

당신이 과부하가있는 방법이있을 때이 오류가 발생하고 사용에 대한

Thread thread = new Thread(delegate { 
    try 
    { 
     Helper.CopyFolder(from, to); 
     Helper.RunProgram("chown", "-R www-data:www-data " + to); 
    } 
    catch (Exception exception) 
    { 
     Helper.DeactivateThread(Thread.CurrentThread.Name); 
    } 
    Helper.DeactivateThread(Thread.CurrentThread.Name); 
}); 
thread.IsBackground = true; 
thread.Priority = ThreadPriority.Lowest; 
thread.Name = name; 
thread.Start(); 

덕분에 하나 과부하로 일할 수 있습니다. 컴파일러는 어느 오버로드를 호출해야 할지를 모르기 때문에 매개 변수를 형 변환하여 명시 적으로 명시해야합니다. 이렇게하는 한 가지 방법은 다음과 같이이다 : 또는

Thread thread = new Thread((ThreadStart)delegate { 
    try 
    { 
     Helper.CopyFolder(from, to); 
     Helper.RunProgram("chown", "-R www-data:www-data " + to); 
    } 
    catch (Exception exception) 
    { 
     Helper.DeactivateThread(Thread.CurrentThread.Name); 
    } 
    Helper.DeactivateThread(Thread.CurrentThread.Name); 
}); 
0

, 당신은 람다를 사용할 수 있습니다

Thread thread = new Thread(() => 
{ 
    try 
    { 
     Helper.CopyFolder(from, to); 
     Helper.RunProgram("chown", "-R www-data:www-data " + to); 
    } 
    catch (Exception exception) 
    { 
     Helper.DeactivateThread(Thread.CurrentThread.Name); 
    } 
    Helper.DeactivateThread(Thread.CurrentThread.Name); 
}); 

thread.IsBackground = true; 
thread.Priority = ThreadPriority.Lowest; 
thread.Name = name; 
thread.Start();