Tuesday, April 14, 2020

C# how get Form tick faster?



I try various things, only those who have try a lot, know the context. This now here is the advanced stuff.
  
Why use the ticks in first place, while(true) is always pretty fast. But InitializeCompinent() newer start or end, now matter which side of it, the while(true)-loop is because there is always certain threading and asynchronization.

So I end-up to do  System.Windows.Forms.Timer tick from that, but it was too slow for the thing I plan to do.

Thread, thread was my mind, so I get idea to spend up the ticks by stop the timer on the threads very own function.

(And I need to make mention: async worker not work here at all, it is for internet stuff, I try that at one point. )

myTimerforTest.Interval = 1; //slow :(
myTimerforTest.Tick += new EventHandler(TheLoop);
myTimerforTest.Start();
... much lower on the code sheet.
private void TheLoop(object sender, EventArgs e)
{
myTimerforTest.Stop();/*here start the error and I though first it speed up the process*/
Thread thread = new Thread(new ThreadStart(WhileTrueInsideThisFunction));
thread.Start();

}

Yes don't do that in above, it make error so big it shut down a whole computer, it some how make that it newer get out that Loop function. But it give me idea for the way that seem to work. Here it is.

//So this go in Form()
Thread thread = new Thread(new ThreadStart(Loop));
thread.Priority = ThreadPriority.Lowest;/* even the slowest this work better than the Tick */
InitializeComponent();
thread.Start();
... then much lower at the code sheet after Form(): 
   private void Loop()
        {
            try
            {
                while (true)
                {
                    //Here the code
                    Thread.Sleep(10); /* when while is in sleep, it go making the InitializeComponent()-function. */
                }

            }
            catch (Exception) { 
            
            }
          

        }

Yes, but this "using thread" is now in the uncharted territory meaning if you close the form thread still looping and other cases might occur that you except more automation, but actually you need yourself write commands down.  

Don't take this too seriously, if you know more official best practice way make fast tick on form() then use it. Soon as I figure it out I use it too.

No comments:

Post a Comment

3D BLog has return

Go  to Check    https://thereal3dblog.com/