Created
March 25, 2020 02:13
-
-
Save Miragecore/dddc44a125d35c00377b937a24dad44a to your computer and use it in GitHub Desktop.
Revisions
-
Miragecore created this gist
Mar 25, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,31 @@ //https://stackoverflow.com/questions/18490123/destructor-never-gets-called public class Class : IDisposable { private Task task; private CancellationTokenSource cts = new CancellationTokenSource(); AutoResetEvent arEvtProcRun = new AutoResetEvent(false); Class() { task = new Task(Run, cts.Token, TaskCreationOptions.LongRunning); task.Start(); } public void Dispose() { cts.Cancel(); arEvtProcRun.Set(); } private void Run() { while (!cts.Token.IsCancellationRequested) { arEvtProcRun.WaitOne(); if(cts.Token.IsCancellationRequested) break; // Your stuff goes here. } } }