The combination of multithreading and Timer In the WinForm Program

Source: Internet
Author: User

In a user interface, you are required to provide the address of an Excel file. The user interface is as follows. After selecting a file, click Next. The system checks the availability of the provided Excel file, calls the COM component to analyze the layout of the Excel file, and switches to another Form if the file passes, if not, the system prompts the user to reselect. If this process takes a long time, the user interface will be suspended, without any movement. Therefore, a GIF image waiting for is usually used in Form to prompt that the user is running. To achieve the following results. However, although this image is added, because it is in a thread, you may first execute all the logic in the Button Click method before rendering the interface. In this case, the image is still invisible, but an incomplete WinForm is suspended. Now I think of multithreading, and use another thread to check the Excel availability. Use a variable to mark whether the second thread is complete, and then continue the operation. [Csharp] bool isOK = false; bool isThreadOVer = false; private void button_Click () {// code for displaying images this. pictureBox. visible = true; SecondThread. start (); // run the check logic and Mark isThreadOver as true. in addition, if the check result Excel is available, set global isOK to true; otherwise, false while (true) // This is a waiting process, waiting for the second thread to complete, then proceed to the logic of the main thread {if (isThreadOver) {if (isOK) {}// destroy this, And the next interface else {}// returns this user interface, prompt to re-provide the Excel path} imagine that the CPU executes the main thread for a period of time, then forwards it to the sub-thread for a period of time, and then forwards it back. However, during execution, it is found that the program is stuck here when it is executed into an endless loop and will not be converted to the sub-thread. Therefore, it is now a phenomenon of false death. The other method is to use the Timer control instead of the endless loop. If you set a Timer and the specified time interval is very small, such as 100, it will constantly test whether the variable isThreadOver is true. If it is true, it indicates that the second thread has completed the execution, the current interface is ended and the next interface is displayed. [Csharp] private mytimer_Tick () {if (isThreadOver) {if (isOK) {<span style = "white-space: pre"> </span> // display the next interface, destroy the current interface} so that when the program checks the Excel availability, it will not cause the user visual interface blocking in the main thread so that the program will become stuck.

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.