public void OnButtonPressed(IRibbonControl control)
        {
            //MessageBox.Show("Hello from control " + control.Id);

            string str=FirstAddIn.MyGetHostname();

            var UIHandler = new Action<object>((o) =>
            {
                ctrl.ShowDialog();
            });

             //if (waitSet) //avoid double submission
             //{

             //    ThreadPool.QueueUserWorkItem(new WaitCallback(UIHandler));
             //    return;
             //}
             //else
             //{
             //    waitSet = true;
             //}

            //var wait = new ManualResetEvent(false);

            var handler = new EventHandler((o, e) =>
            {
                cr = (DataResponse)o;
                result = cr.data;
                //waitSet = false;
                //wait.Set();

                ExcelAsyncUtil.QueueAsMacro(() =>
                {
                    //ExcelReference cell = (ExcelReference)XlCall.Excel(XlCall.xlfActiveCell);
                    ExcelReference cell = refCell;
                    int testRowSize = cr.row;
                    int testColSize = cr.col;

                    var activeCell = new ExcelReference(cell.RowFirst, testRowSize + cell.RowFirst - 1, cell.ColumnLast, cell.ColumnLast + testColSize - 1);

                    activeCell.SetValue(result);
                    XlCall.Excel(XlCall.xlcSelect, activeCell);

                });

            });

            if (this.ctrl.getRunState() == RunState.IDLE)
            {
                ExcelAsyncUtil.QueueAsMacro(() =>
                {
                    refCell = (ExcelReference)XlCall.Excel(XlCall.xlfActiveCell);
                });
                ctrl.registerCallback(handler);
            }

            ThreadPool.QueueUserWorkItem(new WaitCallback(UIHandler));

            ////For simplicity, we implement the wait here
            //wait.WaitOne();
            //if (result == null)
            //{
            //    return;
            //}

            //ExcelAsyncUtil.QueueAsMacro(() =>
            //{
            //    //ExcelReference cell = (ExcelReference)XlCall.Excel(XlCall.xlfActiveCell);
            //    ExcelReference cell = refCell;
            //    int testRowSize = cr.row;
            //    int testColSize = cr.col;

            //    var activeCell = new ExcelReference(cell.RowFirst,testRowSize+cell.RowFirst-1, cell.ColumnLast ,cell.ColumnLast + testColSize-1);

            //    activeCell.SetValue(result);
            //    XlCall.Excel(XlCall.xlcSelect, activeCell);

            //});
        }
        private void btnGo_Click(object sender, EventArgs e)
        {
            myState = RunState.RUN;
            r = new DataResponse();
            r.row = Int32.Parse( this.txtRow.Text);
            r.col = Int32.Parse(this.txtCol.Text);

            ThreadPool.QueueUserWorkItem(this.runCalculation, r);
            //this.Close();
        }
        public static object mySampleTbl()
        {
            if (RunOnce)
            {
                RunOnce = !RunOnce;
                return XlCall.Excel(XlCall.xlUDF, "Resize", lastRun); ;
            }
            else
            {
                lastRun = null;
                RunOnce = !RunOnce;
            }
            DataResponse cr = new DataResponse();
            object result = null;
            bool waitSet = false;
            //object result = MakeArrayet(5, 5);
            TestWinForm.TestInputForm ctrl = new TestWinForm.TestInputForm();

            var wait = new ManualResetEvent(false);

            var handler = new EventHandler((o, e) =>
            {
                cr = (DataResponse)o;
                result = MakeArrayet(cr.row, cr.col);
                waitSet = true;
                wait.Set();

            });
            ctrl.registerCallback(handler);

            var tHandler = new Action<object> ( (o) => {
                ctrl.ShowDialog();
            });
            ThreadPool.QueueUserWorkItem(new WaitCallback(tHandler));

            //MyAsyncMethod(data, handler); // so it started and will fire handler soon
            if (!waitSet)
            {
                wait.WaitOne();
            }
            lastRun = result;
            // Call Resize via Excel - so if the Resize add-in is not part of this code, it should still work.
            return XlCall.Excel(XlCall.xlUDF, "Resize", lastRun);
        }