Re: COM-Interface usage in MultiThreaded-Environment fails
- From: "Willy Denoyette [MVP]" <willy.denoyette@xxxxxxxxxx>
- Date: Mon, 10 Oct 2005 23:09:23 +0200
Do you realy need the threads to be apartment (STA) threaded? I asume
IShellFolder supports both STA and MTA apartments, so I assume MSFT made
them thread safe.
Willy.
"taj" <taj@xxxxxxxxxxx> wrote in message news:di6kvp$t9b$1@xxxxxxxxxxxx
> Hi,
>
> i got really frustated by this; in the following code, i try to use
> SHGetDesktopFolder-function to retrieve the IShellFolder-interface for -
> well - the desktop (root) folder.
> works well for the first (main) thread doing so; however, it does not for
> the second also doing this if the interface isn't freed by the first
> thread before the second trys to use it.
> if using IntPtr as out-parameters for SHGetDesktopFolder-function, i
> recognized that both threads (although both STA-Apartmented) got the same
> interface-pointer by the function call.
> i asume the marshaller recognizes this and doesn't allow to work the
> second thread on the interface if the first thread not quitted doing so by
> releasing the interface.
> however, calling the function in the second thread works somehow, only
> using it (casting it to another interface in this case) fails for the
> second thread.
> any ideas?
>
> regards,
> taj
>
> //////////////////////////////////////////////////////////////////
>
> using System;
> using System.Threading;
> using System.Runtime.InteropServices;
>
> namespace ConsoleApplication1 {
> class Class1 {
> [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
> Guid("000214E5-0000-0000-C000-000000000046")]
> public interface IShellIcon {
> }
>
> [ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
> Guid("000214E6-0000-0000-C000-000000000046")]
> public interface IShellFolder {
> }
>
> [DllImport("shell32.dll")]
> public static extern Int32 SHGetDesktopFolder(out IShellFolder ppshf);
>
> [STAThread]
> static void Main(string[] args) {
> IShellFolder shellFolder = null;
> SHGetDesktopFolder(out shellFolder);
>
> Thread mythread = new Thread(new ThreadStart(MyThreadWorker));
> mythread.ApartmentState = ApartmentState.STA;
> mythread.Start();
>
> //Marshal.ReleaseComObject(shellFolder); //<-- Uncomment to make it work
> mythread.Join();
> }
>
> public static void MyThreadWorker() {
> try {
> IShellIcon shellIcon = null;
> IShellFolder shellFolder = null;
>
> SHGetDesktopFolder(out shellFolder);
> shellIcon = shellFolder as IShellIcon;
>
> if(shellIcon != null)
> System.Windows.Forms.MessageBox.Show("Got it!");
> } catch(Exception ex) {
> string test = ex.Message;
> }
> }
> }
> }
.
- References:
- Prev by Date: Re: using PIA'S at the time of deployment
- Next by Date: side by side assemblies under same IIS process load wrong unmanaged dll
- Previous by thread: COM-Interface usage in MultiThreaded-Environment fails
- Next by thread: Re: Question about default parameters in COM type lib and use in C# after using tlbimp.exe
- Index(es):
Relevant Pages
|