@@ -33,6 +33,11 @@ namespace SQLitePCL.pretty
3333 /// </summary>
3434 public static partial class DatabaseConnection
3535 {
36+ // FIXME: I picked this number fairly randomly. It would be good to do some experimentation
37+ // to determine if its a good default. The goal should be supporting cancelling of queries that are
38+ // actually blocking use of the database for a measurable period of time.
39+ private static readonly int defaultInterruptInstructionCount = 100 ;
40+
3641 private static IScheduler defaultScheduler = TaskPoolScheduler . Default ;
3742
3843 /// <summary>
@@ -54,6 +59,13 @@ public static IScheduler DefaultScheduler
5459 }
5560 }
5661
62+ internal static IAsyncDatabaseConnection AsAsyncDatabaseConnection ( this SQLiteDatabaseConnection This , IScheduler scheduler , int interruptInstructionCount )
63+ {
64+ Contract . Requires ( This != null ) ;
65+ Contract . Requires ( scheduler != null ) ;
66+ return new AsyncDatabaseConnectionImpl ( This , scheduler , interruptInstructionCount ) ;
67+ }
68+
5769 /// <summary>
5870 /// Returns an <see cref="IAsyncDatabaseConnection"/> instance that delegates database requests
5971 /// to the provided <see cref="IDatabaseConnection"/>.
@@ -66,9 +78,7 @@ public static IScheduler DefaultScheduler
6678 /// <returns>An <see cref="IAsyncDatabaseConnection"/> instance.</returns>
6779 public static IAsyncDatabaseConnection AsAsyncDatabaseConnection ( this SQLiteDatabaseConnection This , IScheduler scheduler )
6880 {
69- Contract . Requires ( This != null ) ;
70- Contract . Requires ( scheduler != null ) ;
71- return new AsyncDatabaseConnectionImpl ( This , scheduler ) ;
81+ return AsAsyncDatabaseConnection ( This , scheduler , defaultInterruptInstructionCount ) ;
7282 }
7383
7484 /// <summary>
@@ -82,7 +92,6 @@ public static IAsyncDatabaseConnection AsAsyncDatabaseConnection(this SQLiteData
8292 /// <returns>An <see cref="IAsyncDatabaseConnection"/> instance.</returns>
8393 public static IAsyncDatabaseConnection AsAsyncDatabaseConnection ( this SQLiteDatabaseConnection This )
8494 {
85- Contract . Requires ( This != null ) ;
8695 return AsAsyncDatabaseConnection ( This , defaultScheduler ) ;
8796 }
8897 }
@@ -520,6 +529,7 @@ internal sealed class AsyncDatabaseConnectionImpl : IAsyncDatabaseConnection
520529 {
521530 private readonly OperationsQueue queue = new OperationsQueue ( ) ;
522531 private readonly IScheduler scheduler ;
532+ private readonly int interruptInstructionCount ;
523533
524534 private readonly SQLiteDatabaseConnection conn ;
525535 private readonly IObservable < DatabaseTraceEventArgs > trace ;
@@ -528,10 +538,11 @@ internal sealed class AsyncDatabaseConnectionImpl : IAsyncDatabaseConnection
528538
529539 private bool disposed = false ;
530540
531- internal AsyncDatabaseConnectionImpl ( SQLiteDatabaseConnection conn , IScheduler scheduler )
541+ internal AsyncDatabaseConnectionImpl ( SQLiteDatabaseConnection conn , IScheduler scheduler , int interruptInstructionCount )
532542 {
533543 this . conn = conn ;
534544 this . scheduler = scheduler ;
545+ this . interruptInstructionCount = interruptInstructionCount ;
535546
536547 this . trace = Observable . FromEventPattern < DatabaseTraceEventArgs > ( conn , "Trace" ) . Select ( e => e . EventArgs ) ;
537548 this . profile = Observable . FromEventPattern < DatabaseProfileEventArgs > ( conn , "Profile" ) . Select ( e => e . EventArgs ) ;
@@ -632,7 +643,7 @@ public IObservable<T> Use<T>(Func<IDatabaseConnection, CancellationToken, IEnume
632643
633644 return queue . EnqueueOperation ( ct =>
634645 {
635- // this.conn.RegisterProgressHandler(100 , () => ct.IsCancellationRequested);
646+ this . conn . RegisterProgressHandler ( interruptInstructionCount , ( ) => ct . IsCancellationRequested ) ;
636647 try
637648 {
638649 ct . ThrowIfCancellationRequested ( ) ;
@@ -653,7 +664,7 @@ public IObservable<T> Use<T>(Func<IDatabaseConnection, CancellationToken, IEnume
653664 }
654665 finally
655666 {
656- // this.conn.RemoveProgressHandler();
667+ this . conn . RemoveProgressHandler ( ) ;
657668 }
658669 } , scheduler , cancellationToken ) ;
659670 } ) ;
0 commit comments