public void OnceNonBlockingTest() { bool isRunning = false; Ajob.Once(j => isRunning = true).Wait(); Assert.True(isRunning); }
/// <summary> /// 删除本地已存在的目录。 /// </summary> /// <param name="path">目录路径。</param> /// <param name="recursive">若要移除 <paramref name="path"/> 中的目录、子目录和文件,则为 true;否则为 false。</param> public static IAsyncJob DeleteDirectory(string path, bool recursive = true) { if (!Directory.Exists(path)) { return(null); } var job = Ajob.Once(j => { while (true) { try { Directory.Delete(path, recursive); break; } catch (Exception) { j.Delay(300); } } }); job.Wait(300); return(job); }
public void CancelTest() { Exception ex = null; GA.GlobalError += (ss, ee) => { ex = ee.Exception; }; int testCount = 0; var token = Ajob.Loop(j => { j.Delay(5000); if (!j.IsCanceled) { testCount++; } }, 300); Threading.Thread.Sleep(1000); token.Cancel(); Assert.Equal(0, testCount); Threading.Thread.Sleep(5000); Assert.True(token.IsCanceled); Assert.Equal(0, testCount); Assert.Null(ex); }
public void LockSlimSampleTest2() { List <int> list = new List <int>(); Random random = new Random((int)DateTime.Now.Ticks & 100); using (var ls = new LockSlim()) { AsyncJobHandler job = j => { using (ls.UpgradeableRead()) { for (int i = 0; i < 100; i++) { int item; while (!list.Contains(item = random.Next(0, (int)j.State))) { list.Add(item); } } } }; var job1 = Ajob.Once(job, state: 100); var job2 = Ajob.Once(job, state: 200); Ajob.Once(j => { job1.Wait(); job2.Wait(); Assert.Equal(list.Count, list.Distinct().Count()); Console.WriteLine(list.Count); }).Wait(); } }
public void OnceTest() { bool isRunning = false; var token = Ajob.Once(j => isRunning = true, 1000); token.Wait(); Assert.True(isRunning); }
public void OnceCancelTest() { var token = Ajob.Once(j => j.Delay(5000), 1000); if (token.Wait(1000)) { Assert.True(false); } token.Cancel(); }
public void LoopTest() { int testCount = 0; var token = Ajob.Loop(j => testCount++, 300); token.Wait(1000); token.Cancel(); Assert.Equal(3, testCount); Threading.Thread.Sleep(1000); Assert.Equal(3, testCount); }
public void OnceExceptionTest() { Assert.Throws <AggregateException>(() => { var token = Ajob.Once(j => { throw new ArgumentNullException(); }); token.Wait(); }); }
public void LockSlimSampleTest1() { List <int> list = new List <int>(); using (var ls = new LockSlim()) { var job1 = Ajob.Once(j => { using (ls.Write()) { for (int i = 0; i < 10; i++) { list.Add(i); } } }); var job2 = Ajob.Once(j => { using (ls.Write()) { for (int i = 10; i < 20; i++) { list.Add(i); } } }); Ajob.Once(j => { while (true) { using (ls.Read()) { if (list.Count != 20) { j.Delay(100); } else { break; } } } Assert.Equal(20, list.Count); list.Sort(); for (int i = 0; i < 20; i++) { Assert.Equal(i, list[i]); } }).Wait(); } }