Develop real estate information collectors with WPF + MongoDB (2) -- background thread

Source: Internet
Author: User

As we all know, when performing time-consuming operations (for example, crawling and analyzing website data ),Background threadThe interface will be suspended.

To solve this problem,. NET will provide us with a lot of solutions.Background threadThe backgroundworker tool is one of them. It encapsulates all the thread processing and is convenient for applications.

Simple to use. Define a backgroundworker

 
Private backgroundworker BW = new backgroundworker ();

Then define its dowork and runworkercompleted events. To report the progress, you can also handle the processchanged event.

 
Bw. dowork + =NewDoworkeventhandler (bw_dowork );
Bw. runworkercompleted + =NewRunworkercompletedeventhandler (bw_runworkercompleted );

We put time-consuming operations (crawling and analyzing website data) in bw_dowork. When capturing data, we should note that the control cannot be accessed across threads due to multithreading, therefore, if you want to operate interface controls, you need to use dispatcher to call the delegate:

Private VoidBw_dowork (ObjectSender, doworkeventargs E)
{
E. Result = getdata ();
}

Then display the data in the completed event:

 
Private VoidBw_runworkercompleted (ObjectSender, runworkercompletedeventargs E)
{
List AsList Rgv. itemssource =Null;
Rgv. itemssource = listhi;
}

OK. The preceding Code uses a thread to obtain data.

However, in many cases, we want to allow crawlers to crawl every other time, instead of only once, or when users trigger them manually. Therefore, we need an alert clock (timer) to notify crawlers of the job.

Therefore, we define another Timer:

 
Private dispatchertimer timer = new dispatchertimer ();

Set the time interval of a timer and the tick event:

 
Timer. Tick + = new eventhandler (timer_tick); timer. interval = new timespan (0, 5, 0); // five minutes

Then, the crawler is notified in the tick event:

 
Private VoidTimer_tick (ObjectSender, eventargs E)
{
If(! Bw. isbusy)//If the worker is still running when isbusy = true, the backgroundworker reports an exception.
{
Bw. runworkerasync ();
}
}

The final attachment is executable.ProgramFor everyone to make bricks ~ Real Estate Information Collector

Next, I will add the MongoDB database and add the downloaded data to the database ~~~~~

Develop real estate information collectors with WPF + MongoDB (1)

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.