A very difficult problem for UI

I wrote a component for hard long time calculation,and begin a new thread for this calculation.
and i also need a Message to show the realtime procedure of this calculation.such as “Now is 50%”,and “Finish!”
and when use this component,i found it show the procedure can not update self automatically,only if you click the blank area of canvas,or zoom in/out,the message can update itself.
so can you have some idea of making the message automatically update itself?thank you very much!

image

    protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
    { }

    protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
    { }
    
    Thread CalcuThread;
    protected override void SolveInstance(IGH_DataAccess DA)
    {
        double A = 1;
        CalcuThread = new Thread(() => LongTimeCalculate(A));
        CalcuThread.Start();
    }
    private void LongTimeCalculate(double a)
    {
        for(int i=0;i<100;i++)
        {
            //do some hard calculation....
            for(long j=0;j<1000000;j++)
            {
                double c = Math.Sin(a * i) * Math.Cos(a * i);
            }
            //do some hard calculation....
            Message = $"Now is {i}% ";
        }
        Message = "Finish!";
    }

You must invalidate the canvas to trigger a redraw. Easiest way to do this is to schedule a redraw on the GH_Canvas during the attribute Render method. Say one second in the future if your process is still running.

Thank you David,is this : Instances.RedrawCanvas Method?

No, Instances.RedrawCanvas immediately invalidates and paints the canvas. You’ll eat up far too many processing cycles if you start doing redraws in a tight loop like that. Something along these lines:

if (redrawRequired)
  Grasshopper.Instances.ActiveCanvas?.ScheduleRegen(1000);
2 Likes

Thankyou,David!