简体   繁体   中英

resources.GetObject(“$this.Icon”) crashes application on Windows XP

I've made this program which works fine in Windows 7, but it doesn't seem to work in Windows XP, as it crashed right away with a 'Program has closed bla bla send error report' message from Windows. After some googling I found a solution to get myself an exceptionlog in the Event Log. This is the result:

Edit: new exception log (with unhandled exception filter)

Exception: Het doel van een aanroep heeft een uitzondering veroorzaakt. bij System.RuntimeMethodHandle._SerializationInvoke(Object target, SignatureStruct& declaringTypeSig, SerializationInfo info, StreamingContext context) bij System.RuntimeMethodHandle.SerializationInvoke(Object target, SignatureStruct declaringTypeSig, SerializationInfo info, StreamingContext context) bij System.Reflection.RuntimeConstructorInfo.SerializationInvoke(Object target, SerializationInfo info, StreamingContext context) bij System.Runtime.Serialization.ObjectManager.CompleteISerializableObject(Object obj, SerializationInfo info, StreamingContext context) bij System.Runtime.Serialization.ObjectManager.FixupSpecialObject(ObjectHolder holder) bij System.Runtime.Serialization.ObjectManager.DoFixups()
bij System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage) bij System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage) bij System.Resources.ResourceReader.DeserializeObject(Int32 typeIndex)
bij System.Resources.ResourceReader.LoadObjectV2(Int32 pos, ResourceTypeCode& typeCode) bij System.Resources.ResourceReader.LoadObject(Int32 pos, ResourceTypeCode& typeCode) bij System.Resources.RuntimeResourceSet.GetObject(String key, Boolean ignoreCase, Boolean isString) bij System.Resources.RuntimeResourceSet.GetObject(String key, Boolean ignoreCase) bij System.Resources.ResourceManager.GetObject(String name, CultureInfo culture, Boolean wrapUnmanagedMemStream) bij System.Resources.ResourceManager.GetObject(String name) bij STREDIT.frmMain.InitializeComponent() bij STREDIT.frmMain..ctor()
bij STREDIT.Program.Main()

I've found the place where it crashed:

this.Controls.Add(this.statusStrip1);
this.Controls.Add(this.menuStrip1);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); // Here
this.MainMenuStrip = this.menuStrip1;
this.MinimumSize = new System.Drawing.Size(726, 220);

Does anyone have an idea why this happens?

Thanks in advance

Windows XP does not support PNG icons. Create a non-PNG ico for the application, and the application will run fine :).

Icon type correct or not, there is a solution to handle it correctly:

  • put the icons into Assembly's resource file (if you haven't)
  • access the icon like this:

     this.Icon = global::AEM.UI.Properties.Resources.your_icon_name; 

I had exactly the same symptoms on Win7 (with a ICO image) and I found a workaround : add a small delay before the InitializeComponent() for let time to the app to load resources.

public MyForm()
{
    Thread.Sleep(100); // delay for loading ressources

    InitializeComponent();
}

In my case is was not caused by the image format because it worked fine with that image during months of development. It was really caused by the resources loader.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM