First post - I have been tinkering with Clojure for a while somehow missed ClojureCLR.
=> (defn worker [state]
(println (.ManagedThreadId Thread/CurrentThread))
(Thread/Sleep 2000)
(println "Worker done processing =>" state))
=> (ThreadPool/QueueUserWorkItem worker)
Execution error (InvalidCastException) at System.Runtime.CompilerServices.CastHelpers/ChkCast_Helper (NO_FILE:0).
Unable to cast object of type 'Call_dot_net$worker__24966' to type 'System.Threading.WaitCallback'
On JVM, any clojure function can be passed to a ThreadPool object AFAIK. In .Net, I am getting this error because the method expects a Delegate. Is there any documentation on interfaces supported by all functions? How do I get this code to work? (Reading Joy of Clojure's chapter on concurrency)
Thanks!
Is there a page that lists the Java vs .Net differences w.r.t. Interop?
https://github.com/clojure/clojure-clr/blob/master/Clojure/Clojure/Lib/IFn.cs
public interface IFn // Callable, Runnable -- no equivalents
e.g. How would one go about calling an API which has out parameters ?
public static void GetMaxThreads(out int workerThreads, out int completionPortThreads);
The wiki page has some good info about this https://github.com/clojure/clojure-clr/wiki/ByRef-and-params#byref-parameters
I think you'd want:
(let [workerThreads 0 completionPortThreads 0]
(System.Threading.ThreadPool/GetMaxThreads (by-ref workerThreads) (by-ref completionPortThreads))
[workerThreads completionPortThreads])You can use gen-delegate like this:
=> (import '[System.Threading Thread ThreadPool WaitCallback])
=> (def worker
(gen-delegate WaitCallback [state]
(println (.ManagedThreadId Thread/CurrentThread))
(Thread/Sleep 2000)
(println "Worker done processing =>" state)))
=> (ThreadPool/QueueUserWorkItem worker)