Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Creating async methods in C# prior to version 5.0
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
//Title of this code //Rextester.Program.Main is the entry point for your code. Don't change it. using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using System.ComponentModel; using System.Threading; using System.Runtime.Remoting.Messaging; namespace Rextester { public class Program { public static void Main(string[] args) { bool exited = false; long number = 111111111; var primes = new AsyncPrimeFactoring(); primes.MyTaskCompleted += (a, b) => //It seems however that delegate BeginInvoke (which is the mechanism behind this) //will use new thread taken from ThreadPool, so work won't happen on one thread (as in javascript) { (a as AsyncPrimeFactoring).Result.ForEach(f => Console.WriteLine(f)); exited = true; }; primes.MyTaskAsync(number); //just so to see the output while(!exited) { Console.WriteLine("Doing some work in the meantime"); Thread.Sleep(100); } } } public class AsyncPrimeFactoring { public List<long > Result = new List<long>(); void MyTask(long number) { for(long i=1; i<number; i++) if(number % i == 0 && IsPrime(i)) Result.Add(i); } bool IsPrime(long number) { if(number == 1) return false; if(number == 2 || number == 3) return true; for(long i=2; i<=Math.Sqrt((double)number); i++) if(number%i == 0) return false; return true; } delegate void MyTaskDelegate(long number); bool _myTaskIsRunning = false; public bool IsBusy { get { return _myTaskIsRunning; } } readonly object _sync = new object(); public event AsyncCompletedEventHandler MyTaskCompleted; public void MyTaskAsync(long number) { MyTaskDelegate worker = new MyTaskDelegate(MyTask); AsyncCallback completedCallback = new AsyncCallback(MyTaskCompletedCallback); lock (_sync) { if (_myTaskIsRunning) throw new InvalidOperationException("This instance is already busy."); AsyncOperation async = AsyncOperationManager.CreateOperation(null); worker.BeginInvoke(number, completedCallback, async); _myTaskIsRunning = true; } } void MyTaskCompletedCallback(IAsyncResult ar) { // get the original worker delegate and the AsyncOperation instance MyTaskDelegate worker = (MyTaskDelegate)((AsyncResult)ar).AsyncDelegate; AsyncOperation async = (AsyncOperation)ar.AsyncState; // finish the asynchronous operation worker.EndInvoke(ar); // clear the running task flag lock (_sync) { _myTaskIsRunning = false; } // raise the completed event AsyncCompletedEventArgs completedArgs = new AsyncCompletedEventArgs(null, false, null); async.PostOperationCompleted(f => OnMyTaskCompleted((AsyncCompletedEventArgs)f), completedArgs); } protected virtual void OnMyTaskCompleted(AsyncCompletedEventArgs e) { if (MyTaskCompleted != null) MyTaskCompleted(this, e); } } }
Show compiler warnings
[
+
]
Show input
Compilation time: 0.11 sec, absolute running time: 1.2 sec, cpu time: 1.11 sec, average memory usage: 20 Mb, average nr of threads: 8
edit mode
|
history
|
discussion