Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / July 2007

Tip: Looking for answers? Try searching our database.

Reading file associated icons from a secondary thread not returning correct icon - IconTest.zip (0/1)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
nagar@community.nospam - 28 Jun 2007 07:33 GMT
I need to read the icons from a secondary thread so that the file list
is returned prompty in a listview.

I'm using a background worker object to retrieve icons asynchronously.
What happens is that the icons for certain files (I verified this
happens for xml and sln files on my system) are not correctly
retrieved (I get the standard blank icon).

I'm attaching a sample application that retrieves the icons
synchronously and asynchronously.
Thanks
Andrea
Linda Liu [MSFT] - 28 Jun 2007 11:29 GMT
Hi Andrea,

I run your sample application and do see the problem on my side.

When I click the 'Read Async' button and navigate to a folder that contains
a .sln and a .xml files, the associated icons to the two files are the
standard blank icon.

However, when I click the 'Read Sync' button and navigate to the same
folder, the associated icons to the two files are correct.

I notice that when the application gets the associated icon asynchronous
using the SHGetFileInfo function, the returned iIcon memeber of the
structure specified by the psfi is of value 0, which is not correct.

public static Icon GetAssociatedIcon(string strPath, bool smallIcon)
{
    ....
    int result = SHGetFileInfo(strPath, 0, out info, (uint)cbFileInfo,
flags);

           if (info.hIcon != IntPtr.Zero)
           {
               retIcon = Icon.FromHandle(info.hIcon);
               // when the SHGetFileInfo function is called by a thread
other than the UI thread, the info.iIcon memeber returns 0, which is not
correct
               int index = info.iIcon;              
           }              
           
           return retIcon;
}

I will go on the research and get the result back to you ASAP.

I appreciate your patience!

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
nagar@community.nospam - 28 Jun 2007 16:42 GMT
Thanks Linda. Hope there's a way to get the icons from a secondary
thread so that I can display the items in the list first and then load
the icons.
Thanks so much for your help.
Andrea

>Hi Andrea,
>
[quoted text clipped - 56 lines]
>
>This posting is provided "AS IS" with no warranties, and confers no rights.
nagar@community.nospam - 02 Jul 2007 17:11 GMT
Have you got any news for me, Linda?
Thanks
Andrea

>Hi Andrea,
>
[quoted text clipped - 56 lines]
>
>This posting is provided "AS IS" with no warranties, and confers no rights.
Linda Liu [MSFT] - 03 Jul 2007 11:02 GMT
Hi Andrea,

Sorry for my delayed response. I was OOF yesterday.

I discussed this problem with my workmate. We found that the problem exists
only when the ApartmentState is set to MTA. If you comment out the
STAThreadAttribute on the static Main method, you should see that you can't
get the correct icons for .sln and .xml files even when you click the 'Read
Sync' button.

So the workaround to this problem is to set the ApartmentState of the
thread that the BackgroundWorker uses to STA. Unfortunately, I haven't
worked out how to do it until now.

Alternatively, you may create a Thread and set the thread's ApartmentState
to STA. The following is a sample.

private void AddIcons()
{
         ....
           DisplayIcon();
}

delegate void ShowIconDelegate();
private void DisplayIcon()
{
           if (this.InvokeRequired)
           {
               this.Invoke(new ShowIconDelegate(DisplayIcon));
           }
           else
           {
               int cont = 0;
               for (; cont < this.iconlistAsync.Count; cont++)
               {
                   this.imageListAsync.Images.Add(iconlistAsync[cont]);
                   listViewAsync.Items[cont].ImageIndex = cont;
                   DestroyIcon(iconlistAsync[cont].Handle);
               }
           }
}

private void btnReadAsync_Click(object sender, EventArgs e)
{
   ....
   //backgroundWorker1.RunWorkerAsync();

   Thread thd = new Thread(new ThreadStart(AddIcons));
   thd.TrySetApartmentState(ApartmentState.STA);
   thd.Start();
}

Hope this helps.

Sincerely,
Linda Liu
Microsoft Online Community Support
nagar@community.nospam - 03 Jul 2007 22:02 GMT
Thanks so much Linda. You are really an asset for this newsgroup. I
tried your solution and it worked like a charm.

I wonder if I could use a thread pool to simultaneously load more
icons to speed things up a little bit more. However, it's already a
big improvement.

Thanks.
Andrea

>Hi Andrea,
>
[quoted text clipped - 53 lines]
>Linda Liu
>Microsoft Online Community Support
Linda Liu [MSFT] - 05 Jul 2007 09:48 GMT
Hi Andrea,

Thank you for your feedback.

Thread pooling is not compatible with the single-threaded apartment (STA)
model. However, in your scenario, the thread must to be set to the STA
mode. So it's impossible to use thread pooling in your application.

Hope this helps.
If you have any other concerns, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support
Linda Liu [MSFT] - 09 Jul 2007 11:17 GMT
Hi Andrea,

How about the problem now?

If you have any question, please feel free to let me know.

Thank you for using our MSDN Managed Newsgroup Support Service!

Sincerely,
Linda Liu
Microsoft Online Community Support

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.