내 용어를 용서하십시오. 나는에 익숙하지 System.Threading
않지만 아래와 같은 것이 있다면 :
private static int _index;
private static List<int> _movieIds = new List<int>();
static void Main(string[] args)
{
// the below call populates the _movieIds list variable with around 130,000 ints
GetListOfMovieIdsFromDatabase();
_index = 0;
Thread myThread = new Thread(DoWork);
myThread.Start();
}
public static void DoWork()
{
// do something with the value of _index (iterate through the _movieIds list) then recursively call DoWork() again
Thread.Sleep(400);
_index++;
DoWork();
}
이것은 나쁜 습관입니까? int's
클래스 수준에서 멤버로 정의 된 비공개 정적 목록을 반복하고 있으므로의 첫 번째 반복 DoWork()
은 _index
(간단 함을 위해 설명하지 않은 방식으로) 첫 번째 값을 사용 하고 두 번째 반복 ( 재귀 호출)은 두 번째 값과 함께 작동 _index
합니다.
내가 이것을 묻는 이유는이 앱을 실행 한 지 약 12 시간 후에 스택 오버플로 예외가 발생하고 재귀 호출 때문이라고 생각하기 때문입니다.
예. 호출 스택이 언 롤링 할 기회가 없기 때문에 결국 항상 스택 오버플로가 발생합니다.
index
재귀 호출을 통해 변수를 증가시키는 대신 스레드에서 루프를 사용하십시오.
public static void DoWork()
{
while(true)
{
// do something with the value of _index (iterate through the _movieIds list) then recursively call DoWork() again
Thread.Sleep(400);
_index++;
// break when a condition is met
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다