C # extension of subthreads using FolderBrowserDialog

Source: Internet
Author: User

A: private void button#click (object sender, EventArgs e)
...{
System. Threading. Thread s = new System. Threading. Thread (new System. Threading. ThreadStart (test ));
S. ApartmentState = System. Threading. ApartmentState. STA;
S. Start ();
}

Public void test ()
...{
System. Windows. Forms. FolderBrowserDialog dlg = new FolderBrowserDialog ();
Dlg. ShowDialog ();
}

The preceding Code demonstrates the use of FolderBrowserDialog in subthreads. Setting the ApartmentState of a thread to System. Threading. ApartmentState. STA is the key statement. It should be used in. net2.0

S. SetApartmentState (System. Threading. ApartmentState. STA );

If you do not have the preceding settings, the following error will be reported:

Before calling OLE, you must set the current thread to the single-threaded unit (STA) mode.


If you understand the com thread model, you should be aware of the underlying problems.
Let's take a look at the real-time features of folderbrowserdialog. this configuration is actually accessible. You can use reflector.exe to check its code and call several windows shell32 APIs.

 

[SuppressUnmanagedCodeSecurity]
Internal class Shell32
...{
// Methods
Public Shell32 ();
[DllImport ("shell32.dll", CharSet = CharSet. Auto)]
Public static extern IntPtr SHBrowseForFolder ([In] UnsafeNativeMethods. BROWSEINFO lpbi );
[DllImport ("shell32.dll")]
Public static extern int SHCreateShellItem (IntPtr pidlParent, IntPtr psfParent, IntPtr pidl, out FileDialogNative. IShellItem ppsi );
Public static int SHGetFolderPathEx (ref Guid rfid, uint dwFlags, IntPtr hToken, [financialas (UnmanagedType. LPWStr)] StringBuilder pszPath, uint cchPath );
[DllImport ("shell32.dll", EntryPoint = "SHGetFolderPathEx")]
Private static extern int SHGetFolderPathExPrivate (ref Guid rfid, uint dwFlags, IntPtr hToken, [financialas (UnmanagedType. LPWStr)] StringBuilder pszPath, uint cchPath );
[DllImport ("shell32.dll")]
Public static extern int SHGetMalloc ([Out, externalas (UnmanagedType. LPArray)] UnsafeNativeMethods. IMalloc [] ppMalloc );
[DllImport ("shell32.dll", CharSet = CharSet. Auto)]
Public static extern bool SHGetPathFromIDList (IntPtr pidl, IntPtr pszPath );
[DllImport ("shell32.dll")]
Public static extern int SHGetSpecialFolderLocation (IntPtr hwnd, int csidl, ref IntPtr ppidl );
[DllImport ("shell32.dll")]
Public static extern int SHILCreateFromPath ([exploralas (UnmanagedType. LPWStr)] string pszPath, out IntPtr ppIdl, ref uint rgflnOut );
}

 

There are three types of thread models provided by COM: Single-Threaded Apartment (STA Single-thread suite) and Multithreaded Apartment (MTA multi-thread suite) and Neutral Apartment/Thread Neutral Apartment/Neutral Threaded Apartment (NA/TNA/NTA Neutral Thread suite, provided by COM + ).

A sta object can only be accessed by one thread, which is equivalent to a windows message loop. The implementation method is message loop. interfaces such as ActiveX controls and OLE document servers use the STA suite. An MTA object can be accessed by multiple threads. That is, the code of this object implements thread protection in its own method, ensuring that its status can be changed correctly.


Therefore, when creating and accessing an activex or ole object, you must set the thread mode to sta.

Some experienced users with multithreading may find that ole objects can be successfully called using the Control. Invoke method. For example, the preceding example is changed

 

Private void Form1_Load (object sender, EventArgs e)
...{
System. Threading. Thread s = new System. Threading. Thread (new System. Threading. ThreadStart (test ));
// S. SetApartmentState (System. Threading. ApartmentState. STA );
S. Start ();
}

Public delegate void dtest ();

Public void test ()
...{
This. Invoke (new dtest (invokeTest ));
}

Public void invokeTest ()
...{
System. Windows. Forms. FolderBrowserDialog dlg = new FolderBrowserDialog ();
Dlg. ShowDialog ();
}

In fact, the reason for this call is not Invoke or thread mode. If you remove [STAThread] from the main function, the error at the beginning of the article will still occur. Invoke only allows the main thread to execute the call function of the subthread. [STAThread] The Master thread is set to the sta mode at the program entrance. If this statement is not provided, it is set to the mta mode. The thread model cannot be changed once determined, so you cannot use code elsewhere to set the thread model of the main thread.

 

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.