Reputation: 3079
I am using monodroid for making an android app. I want to disable a button in the view after 3 seconds. I am using the following code:
t = new System.Timers.Timer();
t.Interval = 3000;
t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
t.Start();
protected void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
try{
t.Stop();
button.Enabled = false;
catch(){
Console.WriteLine("Exception: "+ex);
}
}
Because of this code i am getting following exception:
Android.Util.AndroidRuntimeException: Exception of type 'Android.Util.AndroidRuntimeException' was thrown.
at Android.Runtime.JNIEnv.CallVoidMethod (IntPtr jobject, IntPtr jmethod, Android.Runtime.JValue[] parms) [0x00023] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.8.0-branch/3f1c339b/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:368
at Android.Views.View.set_Enabled (Boolean value) [0x00043] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.8.0-branch/3f1c339b/source/monodroid/src/Mono.Android/platforms/android-8/src/generated/Android.Views.View.cs:2341
at MyProject.MainActivity.t_Elapsed (System.Object sender, System.Timers.ElapsedEventArgs e) [0x0001e] in /Users/Piscean/Documents/Xamarin Development/MyProject/MyProject/MainActivity.cs:361
--- End of managed exception stack trace ---
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4607)
at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:867)
at android.view.ViewGroup.invalidateChild(ViewGroup.java:4066)
at android.view.View.invalidate(View.java:10193)
at android.widget.TextView.invalidateDrawable(TextView.java:4584)
at android.graphics.drawable.DrawableContainer.invalidateDrawable(DrawableContainer.java:250)
at android.graphics.drawable.Drawable.invalidateSelf(Drawable.java:350)
at android.graphics.drawable.Drawable.setVisible(Drawable.java:546)
at android.graphics.drawable.DrawableContainer.selectDrawable(DrawableContainer.java:307)
at android.graphics.drawable.StateListDrawable.onStateChange(StateListDrawable.java:106)
at android.graphics.drawable.Drawable.setState(Drawable.java:462)
at android.view.View.drawableStateChanged(View.java:13998)
at android.widget.TextView.drawableStateChanged(TextView.java:3164)
at android.view.View.refreshDrawableState(View.java:14012)
at android.view.View.setEnabled(View.java:5543)
at android.widget.TextView.setEnabled(TextView.java:1201)
at dalvik.system.NativeStart.run(Native Method)
Any idea how can i disable button after 3 seconds. Thanks in advance.
Upvotes: 1
Views: 2473
Reputation: 200
The key is this line: "Only the original thread that created a view hierarchy can touch its views."
Since your timer is running on a background thread, if you want modify the UI you'll need to use the activity's RunOnUiThread method.
protected void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
try{
t.Stop();
MainActivity.RunOnUiThread(() => { button.Enabled = false; });
catch(){
Console.WriteLine("Exception: "+ex);
}
}
Upvotes: 4