RTD server

150 views
Skip to first unread message

Jeff FSharp

unread,
Aug 25, 2020, 5:27:08 AM8/25/20
to Excel-DNA
Hi,

Would someone have an example of how to use XlCall.RTD? I would like to run a Async function. Both F# or C# examples are fine. I am quite new to ExcelDNA and I am a bit confuse.

Thanks a lot

Govert van Drimmelen

unread,
Aug 25, 2020, 5:36:47 AM8/25/20
to exce...@googlegroups.com
Hi Jeff,

You might look at the Excel-DNA Registration extensions - https://github.com/Excel-DNA/Registration - there is also an F# extension library and sample project for F# async functions.

-Govert


--
You received this message because you are subscribed to the Google Groups "Excel-DNA" group.
To unsubscribe from this group and stop receiving emails from it, send an email to exceldna+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/exceldna/fd7841c6-b7ea-4ad3-8b68-af57292bf20en%40googlegroups.com.

Jeff FSharp

unread,
Aug 25, 2020, 5:47:20 AM8/25/20
to Excel-DNA
Thank you. I successfully use F# Async using FsAsync.FsAsyncUtil.excelRunAsync.
However, the problem I have is that I would need to refresh this Async regularly.
I have tried to use the Observable approach but it does not support too many cells this way.
I believe I should use the RTD way but I am very confused by it...

Here is the F# code
// JSON part
type IndexBTC = FSharp.Data.JsonProvider<"""https://www.deribit.com/api/v2/public/index?currency=BTC""">

// function to return a Async<float>
let indexBTC =
    async {
        try
            let! btc = IndexBTC.AsyncLoad("""https://www.deribit.com/api/v2/public/index?currency=BTC""")
            return (float btc.Result.Btc)
        with
        | _ -> return -1.
    }

Now I would like to get this value refreshed from time to time.
I have tried using IsVolatile but then I get a #NA while the value refreshes. If only it could let in the cell the old value the time the Async calc is done...

Not sure if I am clear...

Jeff FSharp

unread,
Aug 25, 2020, 6:40:06 AM8/25/20
to Excel-DNA
Using the IObservable Approach, here is what I have written:
is it the good approach?
Unfortunately, when I use this with many cells it stops working...

Thanks


// to create a Timer
let createTimer (interval: float) =
    let timer = new System.Timers.Timer(interval)
    timer.AutoReset <- true
    timer.Start()
    timer.Elapsed

// helper function to use with excelObserve
let performOnEvent event (f: unit -> 'b) : IObservable<'b>=
    event
    |> Observable.map (fun _ -> f ())
   
// test function
let testAsync =
    async {
        let res = rnd.Next(0,1000)
        return res
    }

let funTest () =
    Async.RunSynchronously testAsync

// example
[<ExcelFunction(Description = "Test")>]
let testObservable (str: string) =
    FsAsync.FsAsyncUtil.excelObserve "test" [|str|]
        (performOnEvent (createTimer 5000.) funTest)

Govert van Drimmelen

unread,
Aug 25, 2020, 10:20:02 AM8/25/20
to exce...@googlegroups.com
Observable approach is the right one, and should scale well.

-Govert

Joffrey d

unread,
Aug 25, 2020, 10:28:32 AM8/25/20
to exce...@googlegroups.com
Unfortunately it seems that the async are not executed like the 
FsAsync.FsAsyncUtil.excelRunAsync.
It seems that they are using all resources.

And so I cannot run quite a few of them.

I tried to use both observe and excelRunAsync at the same time (and avoiding the Async.RunShynchronously that I need to run before giving the function to the obsrrve but it crashes...

When I use just the FsAsync.FsAsyncUtil.excelRunAsync + "IsVolatile=true", it works well but shows #NA the time the cell is recalculated.

Thanks anyway, will continue looking for a way. 


Jeff FSharp

unread,
Aug 25, 2020, 12:06:46 PM8/25/20
to Excel-DNA
the question is, using my code, is there a way to make sure the Async does not go to the thread pool?
it seems that I either choose to use observe which I want but get the Async executed into the thread pool or use the "true F# async but then no oberse/auto refresh.


Thank you

Govert van Drimmelen

unread,
Aug 25, 2020, 12:17:42 PM8/25/20
to exce...@googlegroups.com
Async + volatile is a bad idea.
Observable should work well and scale well.
Maybe work through the RtdClocks sample in Excel-DNA/Samples repository.

-Govert

Message has been deleted

Jeff FSharp

unread,
Aug 25, 2020, 1:12:05 PM8/25/20
to Excel-DNA
Hi Govert,

Thanks so much for your help however, I am not sure to have expressed my problem clearly.
I will check the RtdClocks example but I am not a C# dev, my language remains F# and so it is not always easy for me to get all the tricks in C# code

I have made a github project with my very basic example (Example1.fs) and an excel file with an example where it is facing my issue
I am indeed using an async and an Async.Sleep (to simulate delay to get data from the outside word - being a database, a JSON message received through a web request etc...) and as you may see the code is not working as expected using observables (it is not a real async)

Your help would be much appreciated and when fixed, I will write an article on my blog and post the full solution here so it could help others.




Best,
Reply all
Reply to author
Forward
0 new messages