Merge branch 'multithreading_with_postsharp' into develop

This commit is contained in:
mgroves 2011-02-16 23:04:51 -05:00
commit cc72b84404
4 changed files with 88 additions and 23 deletions

View file

@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Android.App;
using Android.Content;
using Android.OS;
@ -90,41 +89,40 @@ namespace MonoStockPortfolio.Activites
private void Refresh()
{
var pd = new ProgressDialog(this);
pd.SetMessage("Loading...Please wait...");
pd.SetProgressStyle(ProgressDialogStyle.Spinner);
pd.Show();
RefreshWorker();
Action refresh = () =>
{
var tickers = _svc.GetDetailedItems(ThisPortofolioId, GetStockItemsFromConfig());
if (tickers.Any())
{
RunOnUiThread(() => RefreshUI(tickers));
}
else
{
RunOnUiThread(() => ShowMessage("Please add positions!"));
}
RunOnUiThread(pd.Dismiss);
};
var background = new Thread(() => refresh());
background.Start();
UpdateHeader(GetStockItemsFromConfig());
}
private void ShowMessage(string message)
[OnWorkerThread]
private void RefreshWorker()
{
var listAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, new[] { message });
QuoteListview.Adapter = listAdapter;
var tickers = _svc.GetDetailedItems(ThisPortofolioId, GetStockItemsFromConfig());
if (tickers.Any())
{
RefreshUI(tickers);
}
else
{
ShowMessage("Please add positions!");
}
}
[OnGuiThread]
private void RefreshUI(IEnumerable<PositionResultsViewModel> tickers)
{
var listAdapter = new PositionArrayAdapter(this, tickers.ToArray());
QuoteListview.Adapter = listAdapter;
}
[OnGuiThread]
private void ShowMessage(string message)
{
var listAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, new[] { message });
QuoteListview.Adapter = listAdapter;
}
[OnGuiThread]
private void UpdateHeader(IEnumerable<StockDataItem> items)
{
QuoteListviewHeader.RemoveAllViews();

View file

@ -0,0 +1,23 @@
using Android.App;
using Android.Util;
using PostSharp.Aspects;
namespace MonoStockPortfolio.Framework
{
public class OnGuiThreadAttribute : MethodInterceptionAspect
{
public override void OnInvoke(MethodInterceptionArgs args)
{
var activity = args.Instance as Activity;
if (activity == null)
{
Log.Error("OnGuiThreadAttribute", "OnGuiThreadAttribute can only be used on methods within an Activity");
args.Proceed();
}
else
{
activity.RunOnUiThread(args.Proceed);
}
}
}
}

View file

@ -0,0 +1,42 @@
using System;
using System.Threading;
using Android.App;
using PostSharp.Aspects;
namespace MonoStockPortfolio.Framework
{
public class OnWorkerThreadAttribute : MethodInterceptionAspect
{
private ProgressDialog _progressDialog;
public override void OnInvoke(MethodInterceptionArgs args)
{
var activity = args.Instance as Activity;
if(activity == null) throw new Exception("OnWorkerThread can only be used on methods in Activity classes");
ShowProgressDialog(activity);
ThreadPool.QueueUserWorkItem(delegate
{
args.Proceed();
activity.RunOnUiThread(DismissProgressDialog);
});
}
private void ShowProgressDialog(Activity activity)
{
if (_progressDialog == null)
{
var pd = new ProgressDialog(activity);
pd.SetMessage("Loading...Please wait...");
pd.SetProgressStyle(ProgressDialogStyle.Spinner);
_progressDialog = pd;
}
_progressDialog.Show();
}
private void DismissProgressDialog()
{
_progressDialog.Dismiss();
}
}
}

View file

@ -70,6 +70,8 @@
</Compile>
<Compile Include="Framework\IoCAttribute.cs" />
<Compile Include="Framework\IttyBittyIoC.cs" />
<Compile Include="Framework\OnGuiThreadAttribute.cs" />
<Compile Include="Framework\OnWorkerThreadAttribute.cs" />
<Compile Include="Framework\ServiceLocator.cs" />
<Compile Include="Resources\Resource.Designer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />