Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add timeouts to semaphoreslim waits #58

Merged
merged 8 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"isRoot": true,
"tools": {
"cake.tool": {
"version": "2.2.0",
"version": "4.0.0",
"commands": [
"dotnet-cake"
]
Expand Down
10 changes: 8 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@ jobs:

steps:
- uses: actions/checkout@v1

- name: Setup Java JDK
uses: actions/setup-java@v4
with:
distribution: 'microsoft'
java-version: '17'

- name: Setup .NET 7
- name: Setup .NET 8
uses: actions/setup-dotnet@v1
with:
dotnet-version: 7.0.x
dotnet-version: 8.0.x

- name: Install workload
run: dotnet workload install android
Expand Down
2 changes: 0 additions & 2 deletions AndHUD.sln
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SolutionItems", "SolutionIt
build.cake = build.cake
.github\workflows\build.yml = .github\workflows\build.yml
Directory.build.props = Directory.build.props
Directory.build.targets = Directory.build.targets
global.json = global.json
README.md = README.md
EndProjectSection
EndProject
Expand Down
48 changes: 34 additions & 14 deletions AndHUD/AndHUD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
namespace AndroidHUD
{
public class AndHUD
{
{
private const string TagName = nameof(AndHUD);

static AndHUD shared;

public static AndHUD Shared
Expand Down Expand Up @@ -86,7 +88,11 @@

public void Dismiss()
{
_semaphoreSlim.Wait();
if (!_semaphoreSlim.Wait(1000))
{
Log.Warn(TagName, "Timed out getting semaphore on Dismiss()");
return;
}
try
{
DismissCurrent ();
Expand All @@ -106,7 +112,7 @@
else
{
#pragma warning disable CS0618 // Type or member is obsolete
return context.Resources.GetDrawable(drawableResourceId);

Check warning on line 115 in AndHUD/AndHUD.cs

View workflow job for this annotation

GitHub Actions / build

This call site is reachable on: 'Android' 22.0 and later. 'Resources.GetDrawable(int)' is obsoleted on: 'Android' 22.0 and later. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1422)

Check warning on line 115 in AndHUD/AndHUD.cs

View workflow job for this annotation

GitHub Actions / build

This call site is reachable on: 'Android' 22.0 and later. 'Resources.GetDrawable(int)' is obsoleted on: 'Android' 22.0 and later. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1422)

Check warning on line 115 in AndHUD/AndHUD.cs

View workflow job for this annotation

GitHub Actions / build

This call site is reachable on: 'Android' 22.0 and later. 'Resources.GetDrawable(int)' is obsoleted on: 'Android' 22.0 and later. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1422)
#pragma warning restore CS0618 // Type or member is obsolete
}
}
Expand All @@ -116,10 +122,15 @@
if (timeout == null)
timeout = TimeSpan.Zero;

if (CurrentDialog != null && statusObj == null)
DismissCurrent ();

_semaphoreSlim.Wait();
if (!_semaphoreSlim.Wait(1000))
{
Log.Warn(TagName, "Timed out getting semaphore on showStatus()");
return;
}

if (CurrentDialog != null && statusObj == null)
DismissCurrent ();

try
{
if (CurrentDialog == null)
Expand Down Expand Up @@ -190,10 +201,15 @@
{
timeout ??= TimeSpan.Zero;

if (CurrentDialog != null && progressWheel == null)
DismissCurrent ();
if (!_semaphoreSlim.Wait(1000))
{
Log.Warn(TagName, "Timed out getting semaphore on showProgress()");
return;
}

if (CurrentDialog != null && progressWheel == null)
DismissCurrent ();

_semaphoreSlim.Wait();
try
{
if (CurrentDialog == null)
Expand Down Expand Up @@ -243,10 +259,14 @@
if (timeout == null)
timeout = TimeSpan.Zero;

if (CurrentDialog != null && imageView == null)
DismissCurrent ();
if (!_semaphoreSlim.Wait(1000))
{
Log.Warn(TagName, "Timed out getting semaphore on showImage()");
return;
}

_semaphoreSlim.Wait();
if (CurrentDialog != null && imageView == null)
DismissCurrent ();
try
{
if (CurrentDialog == null)
Expand Down Expand Up @@ -302,7 +322,7 @@
}
catch (Exception e)
{
Log.Error("AndHUD", e.ToString());
Log.Error(TagName, e.ToString());
}
}
}
Expand Down Expand Up @@ -358,7 +378,7 @@
}
catch (Exception ex)
{
Log.Error("AndHUD", "Failed to dismiss dialog {0}", ex.ToString());
Log.Error(TagName, "Failed to dismiss dialog {0}", ex.ToString());
}
finally
{
Expand Down
6 changes: 5 additions & 1 deletion AndHUD/AndHUD.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.Net.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0-android;net7.0-android</TargetFrameworks>
<TargetFrameworks>net7.0-android;net8.0-android</TargetFrameworks>
<AssemblyName>AndHUD</AssemblyName>
<RootNamespace>AndroidHUD</RootNamespace>
<PackageId>AndHUD</PackageId>
Expand All @@ -14,5 +14,9 @@
<AndroidResource Include="Resources\**\*.xml" />
<AndroidResource Include="Resources\**\*.png" />
</ItemGroup>

<ItemGroup>
<PackageReference Update="Microsoft.SourceLink.GitHub" Version="8.0.0" />
</ItemGroup>

</Project>
25 changes: 23 additions & 2 deletions Sample/MainActivity.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using AndroidHUD;
using Android.Content;
using AndroidHUD;
using AndroidX.AppCompat.App;

namespace SampleNet6;
Expand Down Expand Up @@ -26,7 +27,7 @@
"Deadlock"
};

private ListView _listView;

Check warning on line 30 in Sample/MainActivity.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field '_listView' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

protected override void OnCreate(Bundle? savedInstanceState)
{
Expand All @@ -40,15 +41,15 @@

var adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, _demos);

_listView = FindViewById<ListView>(Sample.Resource.Id.listview);

Check warning on line 44 in Sample/MainActivity.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference assignment.
_listView.Adapter = adapter;

Check warning on line 45 in Sample/MainActivity.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
_listView.ItemClick += OnItemClick;

Check warning on line 46 in Sample/MainActivity.cs

View workflow job for this annotation

GitHub Actions / build

Nullability of reference types in type of parameter 'sender' of 'void MainActivity.OnItemClick(object sender, ItemClickEventArgs e)' doesn't match the target delegate 'EventHandler<AdapterView.ItemClickEventArgs>' (possibly because of nullability attributes).
}

protected override void OnDestroy()
{
if (_listView != null)
_listView.ItemClick -= OnItemClick;

Check warning on line 52 in Sample/MainActivity.cs

View workflow job for this annotation

GitHub Actions / build

Nullability of reference types in type of parameter 'sender' of 'void MainActivity.OnItemClick(object sender, ItemClickEventArgs e)' doesn't match the target delegate 'EventHandler<AdapterView.ItemClickEventArgs>' (possibly because of nullability attributes).

base.OnDestroy();
}
Expand Down Expand Up @@ -116,8 +117,28 @@
private async void DeadlockScenario()
{
AndHUD.Shared.ShowToast(this, "Deadlocking!", MaskType.None, null, true, null, () => AndHUD.Shared.Dismiss());


StartActivity(new Intent(this, typeof(SecondActivity)));

await Task.WhenAll([
Task.Run(() => AndHUD.Shared.Dismiss()),
Task.Run(() => AndHUD.Shared.Dismiss()),
Task.Run(() => AndHUD.Shared.Dismiss()),
Task.Run(() => AndHUD.Shared.Dismiss()),
Task.Run(() => AndHUD.Shared.Dismiss()),
Task.Run(() => AndHUD.Shared.Dismiss()),
Task.Run(() => AndHUD.Shared.Dismiss()),
Task.Run(() => AndHUD.Shared.Dismiss()),
Task.Run(() => AndHUD.Shared.Dismiss()),
Task.Run(() => AndHUD.Shared.Dismiss()),
Task.Run(() => AndHUD.Shared.Dismiss()),
Task.Run(() => AndHUD.Shared.Dismiss()),
Task.Run(() => AndHUD.Shared.Dismiss()),
Task.Run(() => AndHUD.Shared.Dismiss()),
Task.Run(() => AndHUD.Shared.Dismiss()),
]);

Task.Run(() => AndHUD.Shared.Dismiss());

Check warning on line 141 in Sample/MainActivity.cs

View workflow job for this annotation

GitHub Actions / build

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.
await Task.Delay(1);
AndHUD.Shared.Dismiss();
}
Expand Down
9 changes: 9 additions & 0 deletions Sample/Resources/layout/activity_second.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="I am second" android:layout_gravity="center" />
</LinearLayout>
3 changes: 1 addition & 2 deletions Sample/Resources/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<resources>
<string name="app_name">SampleNet6</string>
<string name="app_text">Hello, Android!</string>
<string name="app_name">AndHUD</string>
</resources>
13 changes: 5 additions & 8 deletions Sample/Sample.csproj
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0-android</TargetFramework>
<SupportedOSPlatformVersion>22</SupportedOSPlatformVersion>
<TargetFramework>net8.0-android</TargetFramework>
<OutputType>Exe</OutputType>
<SupportedOSPlatformVersion>22</SupportedOSPlatformVersion>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<ApplicationId>com.companyname.SampleNet6</ApplicationId>
<ApplicationVersion>1</ApplicationVersion>
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
<LangVersion>latest</LangVersion>
<IsPackable>False</IsPackable>
<AndroidGenerateResourceDesigner>true</AndroidGenerateResourceDesigner>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\AndHUD\AndHUD.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Xamarin.AndroidX.Activity" Version="1.6.1.1" />
<PackageReference Include="Xamarin.AndroidX.Fragment" Version="1.5.5.1" />
<PackageReference Include="Xamarin.Google.Android.Material" Version="1.7.0.2" />
<PackageReference Include="Xamarin.AndroidX.Activity" Version="1.8.2.1" />
<PackageReference Include="Xamarin.AndroidX.Fragment" Version="1.6.2.2" />
<PackageReference Include="Xamarin.Google.Android.Material" Version="1.10.0.3" />
</ItemGroup>

</Project>
15 changes: 15 additions & 0 deletions Sample/SecondActivity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using AndroidX.AppCompat.App;
using Sample;

namespace SampleNet6;

[Activity(Label = "Second")]
public class SecondActivity : AppCompatActivity
{
protected override void OnCreate(Bundle? savedInstanceState)
{
base.OnCreate(savedInstanceState);

SetContentView(Resource.Layout.activity_second);
}
}
4 changes: 2 additions & 2 deletions build.cake
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#tool nuget:?package=GitVersion.CommandLine&version=5.10.3
#addin nuget:?package=Cake.Figlet&version=2.0.1
#tool dotnet:https://api.nuget.org/v3/index.json?package=GitVersion.Tool&version=5.12.0
#addin nuget:https://api.nuget.org/v3/index.json?package=Cake.Figlet&version=2.0.1

var target = Argument("target", "Default");
var configuration = Argument("configuration", "Release");
Expand Down
Loading