Project Development Summary: Solve the inconsistency between multi-threaded forms and the main style

Source: Internet
Author: User

I have done a lot of work this week, and I have written some documents. The previous systems were modified in a lot of unreasonable ways to make them more user-friendly and add some new features. I am very busy, however, it is quite fulfilling, especially the passion for solving some problems. Make a summary after solving the problem. You may encounter similar problems in the future.

This is often the case when a thread is enabled for processing a time-consuming operation. After processing data or logic in the background thread, the user is prompted that the common messagbox is like control. begininvoke is free of discussion here. I understand it all. I am too lazy to add system directly before loading all forms (in the main function. windows. forms. form. checkforillegalcrossthreadcils = false; static attributes are valid no matter how many forms are instantiated. You know, this will enable non-UI threads to access controls created by the UI thread without reporting exceptions.


The login form may have different database connection strings from the actual network environment. It may take a long time to determine the connection or log on. multithreading is required here:

// Enable the background thread to process possible time-consuming operations-login and database interaction and related Logic
This. threadlogin = new thread (New threadstart (loginprocess) {isbackground = true };
This. threadlogin. Start ();
This. marqueeprogressbarcontrol1.visible = true;
This. marqueeprogressbarcontrol1.properties. Stopped = false;

This.txt username. Enabled = false;
This.txt PWD. Enabled = false;
This. btnconfig. Enabled = false;
This. btnlogin. Enabled = false;

 

In the login processing thread:

The MessageBox style of the result is changed, not the above Office 2007 Blue style:

Analysis: The main () function is executing aplication. before run (), the thread form Topic registration and initialization have been completed. These are only valid for the main thread, but not for other threads. Therefore, compare the main function to write a public function. First, register the topic skin and set the topic skin. Then, execute messgebox:

/// <Summary>
/// A message is displayed in multiple threads to solve the inconsistency between the message form and the main form theme.
/// </Summary>
/// <Param name = "owner"> </param>
/// <Param name = "message"> </param>
Public static void showmessage (iwin32window owner, string message)
{
Devexpress. userskins. bonusskins. Register ();
Devexpress. userskins. officeskins. Register ();
Devexpress. Skins. skinmanager. enableformskins ();
Devexpress. lookandfeel. userlookandfeel. Default. skinname = commonlibrary. publicfunction. gettheme (); // mainform. gsplookandfeel. lookandfeel. skinname;
Xtramessagebox. Show (owner, message, "prompt", messageboxbuttons. OK, messageboxicon. information );


}

Then, call it in a non-main thread:

Running result:

OK. The form theme is consistent!

Analysis essence: the thread on which the update or control operation occurs. windows. forms. form. checkforillegalcrossthreadcils = false no control is required. during begininvoke, the update or operation control occurs in a non-UI thread. Therefore, you must register and set the topic in this thread. If you use control. begininvoke. msdn explains that the specified delegate is executed asynchronously on the thread where the basic handle of the control is created. The update or operation of the control occurs in the main UI thread, that is, the thread for creating the control, which is naturally consistent with the original form Topic.

 

 

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.