2012-07-12 6 views
0

내가 안드로이드에 대한 모노의 그릇에 깊은 통찰력을 얻기 위해이 부탁 해요, 내 모바일 앱을 지원할 때 더 나은 길을 준비 할 스레딩 변종 ...MonoDroid - StartService

나는 눈치 때 "기본 실행 프로그램"활동에서 시작된 작업자 스레드에서 StartService를 호출하는 것은 서비스의 OnStartCommand에있는 스레드 ID가 활동 스레드 ID (1)와 같고 시작된 작업자 스레드의 ID (3)가 아니라는 것입니다.

myActivity : Activity 
{ 
    OnCreate 
    { 
    print_thread_id() // 1 

    start(thread_func) 
    } 

    thread_func() 
    { 
    print_thread_id() // 3 (not 2!, but anyway ...) 

    StartService(myService) 
    } 
} 

myService : Service 
{ 
    OnStartCommand 
    { 
    print_thread_id() // 1 also!!! 
    } 
} 

나는 또한 것으로 나타났습니다 내 작업자 스레드 내에서, thread_func, StartService이 서비스가 계속 실행에도 불구하고 반환 : 여기

는보다 추상적 인 표현입니다. myService가 종료 될 때까지 StartService에 대한 호출이 차단 될 것이라고 생각했습니다.

myService.OnStartCommand의 스레드 ID가 thread_func의 스레드 ID와 다른 이유는 무엇입니까? ThreadService.OnStartCommand의 스레드 ID가 thread_func와 같지 않은 이유는 무엇입니까?

감사합니다.

+0

어떻게 시작 하시겠습니까? System.ComponentModel.BackgroundWorker를 사용하여 System.Threading.Thread 인스턴스를 만들고 있습니까? – joates

+0

System.Threading.Thread – samosaris

답변

1

설명하는 동작이 표시되지 않습니다. 새 스레드의 메소드는 자신의 ID를 정확하게보고합니다.

namespace ThreadTest 
{ 
    [Activity (Label = "ThreadTest", MainLauncher = true)] 
    public class Activity1 : Activity 
    { 
     int count = 1; 

     protected override void OnCreate (Bundle bundle) 
     { 
      base.OnCreate (bundle); 

      SetContentView (Resource.Layout.Main); 

      MyService service = new MyService(); 
      Console.WriteLine("First Thread id: " + Thread.CurrentThread.ManagedThreadId); 
      Thread oThread = new Thread(new ThreadStart(service.StartCommand)); 
      oThread.Start(); 
     } 
    } 

    public class MyService : Android.App.Service 
    { 
     public override IBinder OnBind(Intent intent) 
     { 
      return null; 
     } 
     public void StartCommand() 
     { 
      Console.WriteLine("New Thread id: " + Thread.CurrentThread.ManagedThreadId); 
     } 
    } 
} 

출력은 다음과 같습니다 : 다음은 환경의 차이를 해결하는 데 도움이 될 수 있습니다 매우 간단한 클래스입니다

첫 번째 스레드 ID : 1

새로운 스레드 ID : 4

+0

감사합니다. 아래에서 내 게시물을 볼 수 있습니까? – samosaris

0

감사합니다 joates ... 나는 적절한 서비스 사용법에 대한 오해가있을 수 있습니다. 여기 제가하고있는 것이 있습니다 :

namespace ThreadTest 
{ 
    [Activity (Label = "ThreadTest", MainLauncher = true)] 
    public class myActivity : Activity 
    { 
    protected override void OnCreate(Bundle savedInstanceState) 
    { 
     base.OnCreate(savedInstanceState); 
     SetContentView(Resource.Layout.Main); 

     Console.WriteLine("Main Thread id: " + Thread.CurrentThread.ManagedThreadId); 

     Thread serviceWorker = new Thread(new ThreadStart(serviceThread)); 
     serviceWorker.Start(); 
    } 

    private void serviceThread() 
    { 
     Console.WriteLine("Child Thread id: " + Thread.CurrentThread.ManagedThreadId); 

     StartService(new Intent(this, typeof(myService))); 
    } 
    } 

    public class myService : Service 
    { 
    public override IBinder OnBind(Intent intent) 
    { 
     throw new NotImplementedException(); 
    } 

    public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId) 
    { 
     Console.WriteLine("Service Thread id: " + Thread.CurrentThread.ManagedThreadId); 
    } 
    } 
} 

이 방법이 잘못 되었습니까? 아니면 다른 접근 방식입니까? 위에서 시작한 방법 대신 StartService 메서드를 사용하여 서비스를 시작하는 경우 이것이 올바른 방법일까요?

당신이 제안한 방식대로 Service 개체의 메서드를 주 작업 내 작업자 스레드에 할당 한 다음 ThreadStart()를 통해 시작합니다. 내 접근 방식은 실제로 StartService를 통해 서비스 자체를 시작합니다 (UI에서 차단되지 않도록 UI에서 자식 스레드 내에서 호출하는 유일한 이유는 해당 b/c가 필요하지 않음).

내가 제시 한 방식은 adroid/mono가 서비스의로드 및 호출을 관리하도록한다는 의미에서 적절하게 보입니다 (StartService는 결국 OnStartCommand가 호출되도록 재정의합니다). 귀하의 방법으로 실제로 서비스가 명시 적으로 만들어 지는지 확신 할 수는 없지만 (나는 이것을 테스트 할 것이지만), 나의 접근 방식이 있다는 것을 알고 있습니다.

이 문제에 대해 더 적절한 "적절한 방법"이라고 생각합니까?

+0

나는 서비스 작업을 시작하는 위의 두 가지 방법을 생각합니다. 서비스에 대해 기억해야 할 몇 가지 사항 : * 서비스는 별도의 프로세스가 아닙니다. Service 객체 자체는 자신의 프로세스에서 실행 중임을 의미하지는 않습니다. 달리 지정되지 않는 한, 응용 프로그램과 동일한 프로세스에서 실행됩니다. * 서비스가 스레드가 아닙니다.주 스레드에서 작업을 수행하는 것은 자체가 아닙니다 (응용 프로그램이 응답하지 않는 오류를 방지하기 위해). Vogel 자습서 http://www.vogella.com/articles/AndroidServices/article.html – joates

+0

Vogella의 서비스 자습서는 매우 훌륭하며 나에게 도움이 될 것입니다. 고마워, 이건 정말 큰 도움이된다. – samosaris

+0

서비스를 별도로 실행하기로 결정한 사람은 http://mono-for-android.1047100.n5.nabble.com/Creating-service-in-a-new-process-td5710256.html을 참조하십시오. 및 4 번 의견 (https://bugzilla.xamarin.com/show_bug.cgi?id=763) – samosaris