[mono-android] threads in c# vs java example, translated from mark murphy android tutorials
Michael Isbell
michael.isbell at gmail.com
Fri Mar 18 23:59:23 EDT 2011
I'm reworking example number 8 (Threads) from Mark Murphy's Android Java
tutorials. Got it working the first time.
The code's so much more elegant, just wanted to share it
--------------------------------------------------------
public override bool OnOptionsItemSelected(IMenuItem item)
{
if (item.ItemId == Resource.Id.toast) {
string message = "no restaurant selected";
if (current != null) {
message = current.Notes;
}
Toast.MakeText(this, message, ToastLength.Long).Show();
return true;
} else if (item.ItemId == Resource.Id.run) {
SetProgressBarVisibility(true);
progress=0;
new Java.Lang.Thread (delegate {
for (int i=0;i<20;i++) {
RunOnUiThread(() => { progress+=500; SetProgress(progress); });
Thread.Sleep(550);
}
RunOnUiThread(() => SetProgressBarVisibility(false));
}
).Start();
return true;
}
return base.OnOptionsItemSelected(item);
}
--------------------------------------------
>From this Java code:
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId()==R.id.toast) {
String message="No restaurant selected";
if (current!=null) {
message=current.getNotes();
}
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
return(true);
}
else if (item.getItemId()==R.id.run) {
setProgressBarVisibility(true);
progress=0;
new Thread(longTask).start();
return(true);
}
return(super.onOptionsItemSelected(item));
}
private void doSomeLongWork(final int incr) {
runOnUiThread(new Runnable() {
public void run() {
progress+=incr;
setProgress(progress);
}
});
SystemClock.sleep(250); // should be something more useful!
}
private Runnable longTask=new Runnable() { public void run() { for (int
i=0;i<20;i++) { doSomeLongWork(500); } runOnUiThread(new Runnable() { public
void run() { setProgressBarVisibility(false); } }); } };
--
Mike Isbell
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/mailman/private/monodroid/attachments/20110318/3d7d92a1/attachment.html
More information about the Monodroid
mailing list