2012-10-04 2 views
0

내 스레드를 백그라운드 스레드로 구성하려고합니다. 스레드에서이 속성이 누락 된 이유는 무엇입니까?C# 배경 스레드 속성이 누락되었습니다.

Thread thread = new Thread(openAdapterForStatistics(_device)); 

하지만 내가 가지고 2 컴파일 오류 :

  1. System.Threading.Thread.Thread '에 가장 적합한 오버로드 된 메서드 (시스템

    ThreadStart starter = delegate { openAdapterForStatistics(_device); }; 
        new Thread(starter).Start(); 
    
    public void openAdapterForStatistics(PacketDevice selectedOutputDevice) 
    { 
        using (PacketCommunicator statCommunicator = selectedOutputDevice.Open(100, PacketDeviceOpenAttributes.Promiscuous, 1000)) //open the output adapter 
        { 
         statCommunicator.Mode = PacketCommunicatorMode.Statistics; //put the interface in statstics mode     
         statCommunicator.ReceiveStatistics(0, statisticsHandler); 
    
        } 
    } 
    

    내가 시도가 .Threading.ThreadStart) '에 잘못된 인수가 있습니다.

  2. 인수 1 :'void '에서'System.Threading.ThreadStar '로 변환 할 수 없습니다. t '

및 배경 일에 대해

+0

매개 변수가있는 함수에 대한 대리자를 전달해야하기 때문이라고 생각합니다. 기능이 아닙니다. 당신의 경우 초보자. – Juvil

+0

'Thread thread = new Thread (() => openAdapterForStatistics (_device)); ' –

답변

1

, 나는 당신이 스레드에 대한 참조를 유지하지 않을 이후를 설정하는 기대 표시되지 않습니다 이유를 모르겠어요. 다음과 같아야합니다

ThreadStart starter = delegate { openAdapterForStatistics(_device); }; 
Thread t = new Thread(starter); 
t.IsBackground = true; 
t.Start(); 

을이

Thread thread = new Thread(openAdapterForStatistics(_device)); 
당신은 매개 변수로 object 걸리는 방법을 전달하기로되어 있기 때문에 당신이 실제로 결과를 전달하는 동안

가 작동하지 않습니다 메서드 호출. 그래서 당신이 할 수 있습니다 :

public void openAdapterForStatistics(object param) 
{ 
    PacketDevice selectedOutputDevice = (PacketDevice)param; 
    using (PacketCommunicator statCommunicator = selectedOutputDevice.Open(100, PacketDeviceOpenAttributes.Promiscuous, 1000)) //open the output adapter 
    { 
     statCommunicator.Mode = PacketCommunicatorMode.Statistics; //put the interface in statstics mode     
     statCommunicator.ReceiveStatistics(0, statisticsHandler); 

    } 
} 

과 :

Thread t = new Thread(openAdapterForStatistics); 
t.IsBackground = true; 
t.Start(_device); 
+0

당신의 대답에 탱크가 많이! –

0

당신은 특별히 귀하의 경우처럼 사용을 위해 설계되었습니다 BackgroundWorker 클래스를 사용해야합니다. 백그라운드에서 수행하려는 작업.

0
PacketDevice selectedOutputDeviceValue = [some value here]; 
Thread wt = new Thread(new ParameterizedThreadStart(this.openAdapterForStatistics)); 
wt.Start(selectedOutputDeviceValue); 
+0

'ParameterizedThreadStart'가'object' 매개 변수를 기대하기 때문에 실제로는 작동하지 않습니다. – Tudor

+0

간단하게, openAdapterForStatistics (object _obj) 메소드를 수정하여 object를 받아 들인다. – Juvil

관련 문제