2011-08-15 51 views
0

少し問題が残っています: データが到着するとOnResponseDataが発生する通信クラスを作成しました。 これで、呼び出し元がアクティビティ自体かクラスかどうかを確認する必要があります。RunOnUiThreadが必要かどうか確認してください。

は、このコードを参照してください:

private void OnResponseData(ushort ID, byte function, byte[] values) 
{ 
#if (winm || win7) // windows mobile or phone 7 
    if (this.m_Container.Form.InvokeRequired) 
     { 
      this.m_Container.Form.BeginInvoke(new ModbusTCP.Master.ResponseData(OnResponseData), new object[] { id, function, values }); 
      return; 
     } 
#else 
    if (??) // well this is the problem, what i need to check here? 
    { 
     Action newAc; 
     newAc = delegate { OnResponseData(ID, function, values); }; 
     this.m_Container.Form.RunOnUiThread(newAc); 
     return; 
    } 
#endif 
... 

this.m_Container.Formは、私は基本的にはAndroidのためInvokeRequiredを必要とする私のActivity です。

これまでのところありがとうございます。

答えて

0
(this.m_Container instanceOf Activity) 

これで問題は解決しますか?

+0

instanceOfはC#で定義されていないので、代わりにtryedが使用されます。しかし、効果はありません。誰か別のアイデアを持っている? – Eun

+0

Javaの 'instanceof'キーワードに相当するC#は' is'キーワードです: 'if(this.m_Container is Activity)...' – jonp

2

Android.OS.Looperのインスタンスを確認できます。 Android.OS.Looper.MyLooper()は、現在のスレッドに関連付けられたLooperを返します。 Looperがない場合は、nullが返されます。一方、Looper.MainLooper(さらにContext.MainLooper)は、UIスレッドのLooperです。したがって:

if (Looper.MyLooper() != Looper.MainLooper) 
{ 
    Action newAc; 
    newAc = delegate { OnResponseData(ID, function, values); }; 
    this.m_Container.Form.RunOnUiThread(newAc); 
    return; 
} 
関連する問題