diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..dfe0770 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +# Auto detect text files and perform LF normalization +* text=auto diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml new file mode 100644 index 0000000..0058967 --- /dev/null +++ b/.github/workflows/dotnet.yml @@ -0,0 +1,28 @@ +# This workflow will build a .NET project +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net + +name: .NET + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +jobs: + build: + + runs-on: macos-latest + + steps: + - uses: actions/checkout@v3 + - name: Setup .NET + uses: actions/setup-dotnet@v3 + with: + dotnet-version: 8.0.x + - name: Build + run: dotnet publish -c Release + - name: Upload Artifact + uses: actions/upload-artifact@v3.1.2 + with: + path: src/bin diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c5b6d2e --- /dev/null +++ b/.gitignore @@ -0,0 +1,405 @@ +# globs +Makefile.in +*.userprefs +*.usertasks +config.make +config.status +aclocal.m4 +install-sh +autom4te.cache/ +*.tar.gz +tarballs/ +test-results/ + +# Mac bundle stuff +*.dmg +*.app + +# content below from: https://github.com/github/gitignore/blob/main/Global/macOS.gitignore +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +# content below from: https://github.com/github/gitignore/blob/main/Global/Windows.gitignore +# Windows thumbnail cache files +Thumbs.db +ehthumbs.db +ehthumbs_vista.db + +# Dump file +*.stackdump + +# Folder config file +[Dd]esktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msix +*.msm +*.msp + +# Windows shortcuts +*.lnk + +# content below from: https://github.com/github/gitignore/blob/master/VisualStudio.gitignore +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# JetBrains Rider +.idea/ +*.sln.iml + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ \ No newline at end of file diff --git a/Hosihikari.Minecraft.Extension.Async.sln b/Hosihikari.Minecraft.Extension.Async.sln new file mode 100644 index 0000000..a711069 --- /dev/null +++ b/Hosihikari.Minecraft.Extension.Async.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.10.34707.107 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Hosihikari.Minecraft.Extension.Async", "src\Hosihikari.Minecraft.Extension.Async.csproj", "{3A97AC58-595D-46CB-B2AB-C675FB192956}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {3A97AC58-595D-46CB-B2AB-C675FB192956}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3A97AC58-595D-46CB-B2AB-C675FB192956}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3A97AC58-595D-46CB-B2AB-C675FB192956}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3A97AC58-595D-46CB-B2AB-C675FB192956}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {147559B0-B791-480F-8E35-2E322EB34DC7} + EndGlobalSection +EndGlobal diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..6fb6a01 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,157 @@ +# GNU LESSER GENERAL PUBLIC LICENSE + +Version 3, 29 June 2007 + +Copyright (C) 2007 Free Software Foundation, Inc. + + +Everyone is permitted to copy and distribute verbatim copies of this +license document, but changing it is not allowed. + +This version of the GNU Lesser General Public License incorporates the +terms and conditions of version 3 of the GNU General Public License, +supplemented by the additional permissions listed below. + +## 0. Additional Definitions. + +As used herein, "this License" refers to version 3 of the GNU Lesser +General Public License, and the "GNU GPL" refers to version 3 of the +GNU General Public License. + +"The Library" refers to a covered work governed by this License, other +than an Application or a Combined Work as defined below. + +An "Application" is any work that makes use of an interface provided +by the Library, but which is not otherwise based on the Library. +Defining a subclass of a class defined by the Library is deemed a mode +of using an interface provided by the Library. + +A "Combined Work" is a work produced by combining or linking an +Application with the Library. The particular version of the Library +with which the Combined Work was made is also called the "Linked +Version". + +The "Minimal Corresponding Source" for a Combined Work means the +Corresponding Source for the Combined Work, excluding any source code +for portions of the Combined Work that, considered in isolation, are +based on the Application, and not on the Linked Version. + +The "Corresponding Application Code" for a Combined Work means the +object code and/or source code for the Application, including any data +and utility programs needed for reproducing the Combined Work from the +Application, but excluding the System Libraries of the Combined Work. + +## 1. Exception to Section 3 of the GNU GPL. + +You may convey a covered work under sections 3 and 4 of this License +without being bound by section 3 of the GNU GPL. + +## 2. Conveying Modified Versions. + +If you modify a copy of the Library, and, in your modifications, a +facility refers to a function or data to be supplied by an Application +that uses the facility (other than as an argument passed when the +facility is invoked), then you may convey a copy of the modified +version: + +- a) under this License, provided that you make a good faith effort + to ensure that, in the event an Application does not supply the + function or data, the facility still operates, and performs + whatever part of its purpose remains meaningful, or +- b) under the GNU GPL, with none of the additional permissions of + this License applicable to that copy. + +## 3. Object Code Incorporating Material from Library Header Files. + +The object code form of an Application may incorporate material from a +header file that is part of the Library. You may convey such object +code under terms of your choice, provided that, if the incorporated +material is not limited to numerical parameters, data structure +layouts and accessors, or small macros, inline functions and templates +(ten or fewer lines in length), you do both of the following: + +- a) Give prominent notice with each copy of the object code that + the Library is used in it and that the Library and its use are + covered by this License. +- b) Accompany the object code with a copy of the GNU GPL and this + license document. + +## 4. Combined Works. + +You may convey a Combined Work under terms of your choice that, taken +together, effectively do not restrict modification of the portions of +the Library contained in the Combined Work and reverse engineering for +debugging such modifications, if you also do each of the following: + +- a) Give prominent notice with each copy of the Combined Work that + the Library is used in it and that the Library and its use are + covered by this License. +- b) Accompany the Combined Work with a copy of the GNU GPL and this + license document. +- c) For a Combined Work that displays copyright notices during + execution, include the copyright notice for the Library among + these notices, as well as a reference directing the user to the + copies of the GNU GPL and this license document. +- d) Do one of the following: + - 0) Convey the Minimal Corresponding Source under the terms of + this License, and the Corresponding Application Code in a form + suitable for, and under terms that permit, the user to + recombine or relink the Application with a modified version of + the Linked Version to produce a modified Combined Work, in the + manner specified by section 6 of the GNU GPL for conveying + Corresponding Source. + - 1) Use a suitable shared library mechanism for linking with + the Library. A suitable mechanism is one that (a) uses at run + time a copy of the Library already present on the user's + computer system, and (b) will operate properly with a modified + version of the Library that is interface-compatible with the + Linked Version. +- e) Provide Installation Information, but only if you would + otherwise be required to provide such information under section 6 + of the GNU GPL, and only to the extent that such information is + necessary to install and execute a modified version of the + Combined Work produced by recombining or relinking the Application + with a modified version of the Linked Version. (If you use option + 4d0, the Installation Information must accompany the Minimal + Corresponding Source and Corresponding Application Code. If you + use option 4d1, you must provide the Installation Information in + the manner specified by section 6 of the GNU GPL for conveying + Corresponding Source.) + +## 5. Combined Libraries. + +You may place library facilities that are a work based on the Library +side by side in a single library together with other library +facilities that are not Applications and are not covered by this +License, and convey such a combined library under terms of your +choice, if you do both of the following: + +- a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities, conveyed under the terms of this License. +- b) Give prominent notice with the combined library that part of it + is a work based on the Library, and explaining where to find the + accompanying uncombined form of the same work. + +## 6. Revised Versions of the GNU Lesser General Public License. + +The Free Software Foundation may publish revised and/or new versions +of the GNU Lesser General Public License from time to time. Such new +versions will be similar in spirit to the present version, but may +differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +as you received it specifies that a certain numbered version of the +GNU Lesser General Public License "or any later version" applies to +it, you have the option of following the terms and conditions either +of that published version or of any later version published by the +Free Software Foundation. If the Library as you received it does not +specify a version number of the GNU Lesser General Public License, you +may choose any version of the GNU Lesser General Public License ever +published by the Free Software Foundation. + +If the Library as you received it specifies that a proxy can decide +whether future versions of the GNU Lesser General Public License shall +apply, that proxy's public statement of acceptance of any version is +permanent authorization for you to choose that version for the +Library. diff --git a/src/AsyncPlayer.cs b/src/AsyncPlayer.cs new file mode 100644 index 0000000..f3ffc1e --- /dev/null +++ b/src/AsyncPlayer.cs @@ -0,0 +1,4 @@ +// namespace Hosihikari.Minecraft.Extension.Async; +// +// public static class AsyncPlayer; + diff --git a/src/Core.cs b/src/Core.cs new file mode 100644 index 0000000..8f59ffc --- /dev/null +++ b/src/Core.cs @@ -0,0 +1,33 @@ +namespace Hosihikari.Minecraft.Extension.Async; + +public static class Core +{ + /// + /// Queue in thread pool. + /// + public static void QueueWorkItem(Action act) + { + ThreadPool.QueueUserWorkItem( + x => + { + try + { + x(); + } + catch (Exception) + { + // try + // { + // Console.WriteLineErr(nameof(Async), ex, methodName, file, line); + // } + // catch + // { + // // ignored + // } + } + }, + act, + true + ); + } +} \ No newline at end of file diff --git a/src/Hosihikari.Minecraft.Extension.Async.csproj b/src/Hosihikari.Minecraft.Extension.Async.csproj new file mode 100644 index 0000000..30402ac --- /dev/null +++ b/src/Hosihikari.Minecraft.Extension.Async.csproj @@ -0,0 +1,9 @@ + + + + net8.0 + enable + enable + + + diff --git a/src/RunInTick.cs b/src/RunInTick.cs new file mode 100644 index 0000000..a6e5190 --- /dev/null +++ b/src/RunInTick.cs @@ -0,0 +1,316 @@ +using System.Runtime.CompilerServices; +using System.Runtime.ExceptionServices; + +namespace Hosihikari.Minecraft.Extension.Async; + +public sealed class RunInTickVoid : INotifyCompletion +{ + private Action? _continuation; + + private Exception? _exception; + + private RunInTickVoid(out Action reportResult, out Action reportException) + { + reportResult = ReportResult; + reportException = ReportException; + } + + /// + /// Get a state that indicates that the operation being asynchronously waited for has been completed (successfully or + /// an exception occurred). + /// + public bool IsCompleted { get; private set; } + + /// + /// When the method that executes the asynchronous task using this type is finished, the compiler will automatically + /// call this method. + /// That is to say, this method will be executed in the thread where the caller is located, which is used to notify the + /// caller that the code in the thread where the caller is located has been executed and request to execute the + /// subsequent task after await. In this type, the subsequent task is executed through + /// . + /// + /// + /// The subsequent task wrapped by the asynchronous task state machine. When executed, it will + /// let the state machine continue to go down one step. + /// + public void OnCompleted(Action continuation) + { + if (IsCompleted) + { + Core.QueueWorkItem(continuation); + } + else + { + _continuation += continuation; + } + } + + public static RunInTickVoid StartAsync(Action func) + { + RunInTickVoid asyncOperation = new(out Action reportResult, out Action reportException); + LevelTick.RunInTick(() => + { + try + { + reportResult(); + } + catch (Exception ex) + { + reportException(ex); + } + }); + return asyncOperation; + } + + /// + /// Get an awaitable object that can be used to await the await keyword asynchronously. + /// This method will be called automatically by the compiler. + /// + /// + /// Return itself for asynchronous waiting for return values. + /// + public RunInTickVoid GetAwaiter() + { + return this; + } + + /// + /// Get the return value of this asynchronous waiting operation. + /// This method will be called automatically by the compiler when await ends to get the return value. + /// Different from , if the operation is not completed, this instance will return the + /// default value of instead of blocking the thread until the task is completed. However, if + /// an exception occurs in the asynchronous operation, calling this method will throw this exception. + /// + public void GetResult() + { + if (_exception is not null) + { + ExceptionDispatchInfo.Capture(_exception).Throw(); + } + } + + private void ReportResult() + { + IsCompleted = true; + if (_continuation is not null) + { + Core.QueueWorkItem(_continuation); + } + } + + private void ReportException(Exception exception) + { + _exception = exception; + if (_exception is not null) + { + //todo log + //Console.WriteLineErr(nameof(RunInTickAsyncVoid), _exception ); + } + + IsCompleted = true; + if (_continuation is not null) + { + Core.QueueWorkItem(_continuation); + } + } + + //public static RunInTickAsync FromResult(T result) + //{ + // var asyncOperation = new RunInTickAsync(); + // asyncOperation.ReportResult(result); + // return asyncOperation; + //} + public void ContinueWith(Func action) + { + OnCompleted(() => action()); + } + + public void ContinueWith(Action action) + { + OnCompleted(action); + } +} + +public sealed class RunInTick : INotifyCompletion //,IAwaitable, IAwaiter +{ + /// + /// action to save the continuation task after await temporarily, so that the task can continue to execute after the + /// task is completed. + /// + private Action? _continuation; + + /// + /// Temporarily save the exception that occurred during the execution of the asynchronous task. It will be thrown after + /// the asynchronous waiting is over to report the error that occurred during the asynchronous execution. + /// + private Exception? _exception; + + private T? _result; + + private RunInTick( + out Action reportResult, + out Action reportException + ) + { + reportResult = ReportResult; + reportException = ReportException; + } + + /// + /// Get a state that indicates that the operation being asynchronously waited for has been completed (successfully or + /// an exception occurred). + /// + public bool IsCompleted { get; private set; } + + /// + /// Get the return value of this asynchronous waiting operation. + /// Different from , if the operation is not completed, this instance will return the + /// default value of instead of blocking the thread until the task is completed. However, if + /// an exception occurs in the asynchronous operation, calling this method will throw this exception. + /// + public T? Result + { + get => IsCompleted ? _result : default; + private set => _result = value; + } + + /// + /// When the method that executes the asynchronous task using this type is finished, the compiler will automatically + /// call this method. + /// That is to say, this method will be executed in the thread where the caller is located, which is used to notify the + /// caller that the code in the thread where the caller is located has been executed and request to execute the + /// subsequent task after await. In this type, the subsequent task is executed through + /// . + /// + /// + /// The subsequent task wrapped by the asynchronous task state machine. When executed, it will + /// let the state machine continue to go down one step. + /// + public void OnCompleted(Action continuation) + { + if (IsCompleted) + { + // if the task has been completed when the await starts, execute the code after the await directly. + // Note that even if _continuation has a value, you don't need to care, because it will be executed when the report ends. + Core.QueueWorkItem(continuation); + } + else + { + // When using multiple await keywords to wait for the same awaitable instance, this OnCompleted method will be executed multiple times. + // When the task is really finished, all the code after these await needs to be executed. + // So, we need to save all the code after await in _continuation, and execute them all when the task is finished. + _continuation += continuation; + } + } + + public static RunInTick StartAsync(Func func) + { + RunInTick asyncOperation = new(out Action reportResult, out Action reportException); + LevelTick.RunInTick(() => + { + try + { + reportResult(func()); + } + catch (Exception ex) + { + reportException(ex); + } + }); + return asyncOperation; + } + + /// + /// Get an awaitable object that can be used to await the await keyword asynchronously. + /// + /// + /// Return itself for asynchronous waiting for return values. + /// + public RunInTick GetAwaiter() + { + return this; + } + + /// + /// Manually get the return value of this asynchronous waiting operation asynchronously. + /// + /// + public async Task GetResultAsync() + { + await this; + return _result!; + } + + /// + /// Get the return value of this asynchronous waiting operation. + /// This method will be called automatically by the compiler when await ends to get the return value. + /// + public T GetResult() + { + if (_exception is not null) + { + // throw the exception that occurred in the asynchronous operation + ExceptionDispatchInfo.Capture(_exception).Throw(); + } + + // return the result of the asynchronous operation + return Result!; + } + + private void ReportResult(T r) + { + Result = r; + IsCompleted = true; + if (_continuation is not null) + { + //Dispatcher.InvokeAsync(_continuation, _priority); + Core.QueueWorkItem(_continuation); + } + } + + private void ReportException(Exception exception) + { + _exception = exception; + if (_exception is not null) + { + //todo print exception? + // Console.WriteLineErr(nameof(RunInTickAsync), _exception, methodName, file, line); + } + + IsCompleted = true; + if (_continuation is not null) + { + // todo ? Dispatcher.InvokeAsync(_continuation); + // Queue the continuation action on the thread pool. + Core.QueueWorkItem(_continuation); + } + } + + //public static RunInTickAsync FromResult(T result) + //{ + // var asyncOperation = new RunInTickAsync(); + // asyncOperation.ReportResult(result); + // return asyncOperation; + //} + public void ContinueWith(Func action) + { + OnCompleted(() => action(Result!)); + } + + public void ContinueWith(Action action) + { + OnCompleted(() => action(Result!)); + } + + public static implicit operator T?(RunInTick v) + { + return v.Result; + } + + //public static RunInTickAsync FromResult(T o) + //{ + // var asyncOperation = new RunInTickAsync(); + // asyncOperation.ReportResult(o); + // return asyncOperation; + //} +} \ No newline at end of file