System. Windows. Forms. Timer timer "self-destruction"

Source: Internet
Author: User

Sometimes we complete some initialization work when the program is just starting to run. I also liked to write the code in the load event of the form, but I found this is not good, because of the heavy workload, or when using a remote database, you will find that the program will be stuck at startup, and the user experience is poor.

The interface design principle is that the interface thread doesn't do heav work.

Later, I handed over the initialization work to a separate thread for execution, and found that the effect was good. However, the CLR has implemented a thread security restriction. The interface control cannot be changed across threads. You can also disable this control and use:

Add this sentence to the constructor of the form. checkforillegalcrossthreadcils = false; The child thread can directly access the controls in the form, but the thread is not safe. the default value is control. checkforillegalcrossthreadcils = true; (capture thread error call) in this case, you can use invoke

 

But this is not the case for security purposes.

Use delegation. The code is exhausting to update so many interface controls!

I thought of system. windows. forms. timer, this update interface is secure, but the problem also arises. It is implemented through the windows message mechanism. Similar to the timer control in VB or Delphi, it is implemented using the API settimer internally. Its main disadvantage is that the timing is not accurate and there must be a message loop, which cannot be used by the console application.

It doesn't matter if it is not accurate. It doesn't matter to the console application, but it is difficult to loop. I just want it to be executed once!

This is also simple. After the execution, timer will destroy itself. Usage:

Private void timerjavastick (Object sender, eventargs e) {// initialize the work code timer1.enabled = false;
This method has a small problem. Please refer to the following code}

According to the unkown opinion on the 6th floor (I have just tested that a problem will occur if the destruction is put behind it)
That is to say, do you use timer to update controls in the main process during program loading? In the Code triggered by the timer, the first one should be destroy yourself, and then update the control !!
To:

Thank you.

Private void timerjavastick (Object sender, eventargs e) {timer1.enabled = false; // initialize the work code}

  

Isn't it easy? It's just a little trick that you think. I also hope that you can share your own technologies and experiences.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.