Solve the problem of "previous function evaluate timeout, function evaluate is disabled. You must continue the execution to re-enable function evaluation"

Source: Internet
Author: User

Today, I downloaded the cartoon. As a result, only vol1 and vol2 were published on verycd, and the rest of them were complete. There is no way to go online. You have to wait for the webpage to load up and watch the advertisement. So I made a cartoon downloader myself.

In the multi-threaded download class, I defined a downloadfinished event and returned all the download information. However, after the main form receives this event, nothing is executed. After the breakpoint is reached, it is found that all the parameters I passed in have changed to "previous function evaluate timeout, function evaluate is disabled. You must continue to execute the function to re-enable function evaluation ".

After verification by multiple parties, the problem was finally found. Let's take a look at my original incorrectCode:

Code
Private   Void Downloadimage ( Object Dlinfo)
{
Downloadinfo dinfo = Dlinfo As Downloadinfo;
Uri UI =   New Uri (dinfo. url );
Wclient. downloadfile (ui, dinfo. savepathwithfilename );
Delegate [] d = Downloadfinished. getinvocationlist ();
If (D. Length > 0 )
{
Downloadfinished ( This , UI );
}
}

 

The imported object is actually a downloadinfo class containing the download information. I extract the URI and download the corresponding file. When the downloadfinished event is triggered after the download is completed, the second parameter of the event is the downloaded link. However, after a thread is transferred to the main window (that is, the main thread), the imported UI cannot be accessed. Why?

Because I got the result in the auxiliary thread, that is, the accessible domain of the UI variable is only the sub-main thread. When the downloadfinished time is released, it indicates that the thread has been executed and destroyed. Therefore, all the variables allocated on this thread are listed as recycle objects by the garbage collector. The main thread naturally cannot access them. How can this problem be solved? Take a look at the following code:

Code
Class Filedownloader: idisposable
{
Thread t;
Downloadinfo _ dinfo;
Public   Delegate   Void Downloadfinish ( Object Sender, Object Params );
Public   Event Downloadfinish downloadfinished;
WebClient wclient =   New WebClient ();

Public   Void Startdownload ( Object Dlinfo)
{
T. Start (dlinfo );
}
Private   Void Downloadimage ( Object Dlinfo)
{
Downloadinfo dinfo = Dlinfo As Downloadinfo;
This . _ Dinfo = Dinfo;
Uri UI =   New Uri (dinfo. url );
Wclient. downloadfile (ui, dinfo. savepathwithfilename );
Delegate [] d = Downloadfinished. getinvocationlist ();
If (D. Length > 0 )
{
Downloadfinished ( This , _ Dinfo );
}
Getathread ();
}

 

Filedownloader is a class. To use startdownload to start thread download, you must first instantiate a filedownloader variable, which exists in the main thread, this implies that _ dinfo is also included in the main thread. Let's take a look at the new downloadimage method. The parameter passed in after the event is no longer the local variable UI in the thread, but the _ dinfo variable of the downloadinfo type instantiated in the main thread. In this way, the UI is destroyed when thread t is destroyed, but the _ dinfo in the main function will not be destroyed. In this way, "the previous function evaluate times out, and the function evaluate is disabled. You must continue the execution to re-enable function evaluate.

I wanted to release the cartoon download tool for everyone to download, but this is quite bad for that website. After all, people also need to make money. And once it is released, people will certainly get more BT encryption.AlgorithmI will re-compile the algorithm to download it next time. So you can leave a message if you need it.Source codeI will not post it here.

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.