WinForm cross-thread access control (displays fonts on the progress bar)

Source: Internet
Author: User
Tags sqlite database


This article has a good solution to the problem of having to use threads but there is no way to manipulate controls within a thread (personally think. There are better programs to welcome communication. )

Before doing cross-thread access, let's look at what we've done to achieve the desired effect:

This is a batch of XML files into the SQLite database each XML has nearly 3000 nodes each node has 5 properties, if we do not use the thread then in the process of data import, the program is likely to die and basically can not move, the user experience is very poor.

So we used threads where we added data to the database:

Thread thread =NewThread (NewThreadStart (() =                {                    Try{exeinsertdatabase (); //Data Import                    }                    Catch(Exception ex) {MessageBox.Show (ex). Message,"Import Tips"); return;                }                })); Thread. Start ();

Threads are good to create but after all XML files are too much even if each one takes only 1-2 seconds but thousands of XML takes a few minutes, in this process we need to let the user see this progress. Then add a progress bar:

Font font =New Font ("Microsoft Ya-Black", (Float8, Fontstyle.regular);//The font shown on the progress bar PointF PointF =new PointF (this.pbimport.width/4-10, 2-8); //00 1;< Span style= "color: #0000ff;" >this.pbimport.creategraphics (). DrawString ( " importing (write the imported file name or the progress of the import)  ", font, Brushes.black, PointF);             

See here you think of the database insert data in the thread inside the progress bar growth is not also to join in the thread?   is not to write on this.pbimport.value++;? this.pbImport.CreateGraphics ().  DrawString ("Importing the name of the imported file or the progress of the import)", Font, Brushes.black, PointF); Does this also have to be added to the thread in order to refresh the progress bar progress information?

You can try ...

Then the following error will occur:

Access it from a thread that is not creating a control "control name"

Yes, here's the solution to the problem:

PrivateDelegatevoidSetimportm ();PrivatevoidImportmessage () {if (This. invokerequired) {Setimportm s =new Setimportm (importmessage); this. Invoke (s); } Else { string strText = " importing file " + Filepaths[index] + "... "+ (index + 1) + "/"+ Filepaths.count; this. Pbimport.creategraphics (). DrawString (StrText, Font, Brushes.black, PointF); } }

We used a delegate to solve this problem to append the database progress, just need to insert data into the database where to call this method is OK!

It's so much more verbose.

If you do not understand the children's shoes, please look at the lower right corner I provide special services .....

. Net, WPF Technology Exchange Group: 9234450
No matter what problem you encounter, we will never let you face it alone!

WinForm cross-thread access control (displays fonts on the progress bar)

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.