Real Time Data


Excel 2013+

In Excel 2013, programmers can run multiple user-defined functions asynchronously at the same time.
This frees the Excel calculation thread to run other calculations while the user-defined functions wait for external resources.
The RTD approach for Excel 2013 (and later) is to use the System.Threading.Tasks.Task class which runs the code using the Task Parallel Library (TPL).
Using the Task class is much more efficient that using the Thread class.
The TPL was added in .NET 4.0 and brings many new features:
The corresponding ExcelDNA method is ExcelAsyncUtil.Observe


Excel 2007+

In Excel 2007, programmers could run multiple user-defined functions at the same time by using threads.
Increasing the number of threads used in multiple-thread recalculations increased the performance.
This method has drawbacks primarily because the number of threads is a setting scoped to an application.
The number of threads cannot be controlled at the level of a single function or an add-in.
Some user-defined functions must wait for external resources and while they wait, the Excel calculation thread is blocked.
The RTD approach for Excel 2007 was to use the System.Threading.Thread class and runs the code using a ThreadPool thread.
The corresponding ExcelDNA method is ExcelAsyncUtil.Run


        public static string udf_CLOCK(string sArgument1) 
        {
            return System.Convert.ToString(ExcelDna.Integration.ExcelAsyncUtil.Observe(
                "udf_SOMETEXT2",
                sArgument1,
                () => new ExcelObservableClock() )
                );
        }

        public class ExcelObservableClock : ExcelDna.Integration.IExcelObservable
        {
            System.Threading.Timer _timer;

            List<ExcelDna.Integration.IExcelObserver> _observers;

            public ExcelObservableClock()
            {
                _timer = new System.Threading.Timer(method_TimerTick, null, 0, 1000);

                _observers = new List<ExcelDna.Integration.IExcelObserver>();

            }
            public System.IDisposable Subscribe(ExcelDna.Integration.IExcelObserver observer)
            {
                System.Diagnostics.Debug.WriteLine("Subscribe");

                _observers.Add(observer);

                observer.OnNext(DateTime.Now.ToString("HH:mm:ss.fff") + " (Subscriber)");

                return new ActionDisposable(() => _observers.Remove(observer));
            }

            void method_TimerTick(object oo)
            {
                System.Diagnostics.Debug.WriteLine("method_TimerTick");

                string sNow = DateTime.Now.ToString("HH:mm:ss.fff");

                foreach (var obj in _observers)
                {
                    obj.OnNext(sNow);
                }
            }

        }


        public class ActionDisposable : System.IDisposable
        {
            System.Action _disposeAction;

            public ActionDisposable(System.Action disposeAction)
            {
                _disposeAction = disposeAction;
            }
            public void Dispose()
            {
                System.Diagnostics.Debug.WriteLine("Dispose");

                _disposeAction();
            }
        }

Links

link - stackoverflow.com/questions/5397607/how-do-i-create-a-real-time-excel-automation-add-in-in-c-sharp-using-rtdserver/5697823#5697823 


© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext