Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Settings.Default.Save() and Cursor.LoadFromStream lead to System.IO.IOException

Tags:

c#

wpf

Two strange exceptions happened in .NET built-in components.

It's the same IO exception: "The process cannot access the file '......' because it is being used by another process".

In "cursor" case it's about ".tmp" file and exception happens somewhere at the end of the sequence of calls, when WPF grid is remeasured:

System.Windows.Controls.Grid.MeasureCell
...
System.Windows.Controls.GridViewColumnHeader.GetCursor
...
System.Windows.Input.Cursor.LoadFromStream <-- here

In "settings" case it's about ".newcfg" file and happens exactly on "save" method call.

The question is: how is this possible? And how to handle/prevent it?

I guess default implementations close XMLWriters and do everything correctly.

like image 484
Pavel Evdokimov Avatar asked Jul 10 '26 20:07

Pavel Evdokimov


1 Answers

We had a single user with the same problem as your "settings" case (it's about ".newcfg"). It turns out that when they switched off their Norton Antivirus, the problem went away!

Some research I did prior to that may be of use:

  • Check if you have more than one thread capable of calling Settings.Default.Save(). If multiple threads are competing, it might produce this error, although I understand .NET is supposed to make this thread-safe.
  • It might possibly be happening if you have multiple calls to Settings.Default.Save() in quick succession, within the same thread. This can happen if several classes (e.g. user/custom controls) each want to save some settings, but none should have to be aware of the others' need to do this, and closing down the parent form causes each control to call the Save().
  • Check the user has appropriate write permissions in the target folder AND in the folder where the .exe is located. My colleague has seen a bizarre connection between the two!
  • Find all instances in your code where you call Settings.Default.Save(), and set a break point on all of them. When you run the program, you might notice some suspicious behaviour or pattern in the way they get called.

Hope this helps!

like image 147
Morris Avatar answered Jul 13 '26 12:07

Morris