Here In this case OpenFileDialog
will cause the app to hang and crash.
So moved OpenFileDialog
to a new thread. And everything works fine.
سؤال
In my WPF
Application I used OpenFileDialog
to select an image and load it to app, this works fine as expected.
But if I run same app from a flash drive, image loades after that UI
freezes, any clicks on UI
makes app to crash.
I have admin manifest
to app also.
المحلول 3
Here In this case OpenFileDialog
will cause the app to hang and crash.
So moved OpenFileDialog
to a new thread. And everything works fine.
نصائح أخرى
I couldn't find a good explanation but I solved this problem setting the InitialDirectory with a valid local path (e.g., Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
I've seen something similar to this before when running from a network drive. If the application is not being loaded from a completely trusted source, you can get a SecurityException.
In any case, try adding a try/catch block around the code that fails to see if you are getting an exception.
I found that this problem occurs (crash) not only in WPF, but for WinForms. It is hard to say what is the source of the problem, but still it appears that Microsoft dll related to OpenFileDialog has bugs (for me, it was CmnDlg32.dll)
The only way I could call ShowDialog() function was to wrap it in the event and call with the help of
this.BeginInvoke(
new Action<YourObject, EventArgs>(YourObject_FileDialogOpened), new object[]
{ YourObjectInstance, e });
where "this" is a Control (for example, Form).
BeginInvoke(...) grants that you call will be process in a proper way.
Problem will not appear if you use call of the OpenFileDialog under button click event or any other similar scenario.
How Winform crash when using OpenFileDialog
using(var ofd = new OpenFileDialog())
{
ofd.Filter = "Image Files (*.png;*.bmp;*.jpg)|*.png;*.bmp;*.jpg";
if(ofd.ShowDialog() == DialogResult.OK) // <-- reason of crashing
{
PictureBox.Image = Image.FromFile(ofd.FileName);
}
}
How to fix the issue
using(var ofd = new OpenFileDialog())
{
ofd.Filter = "Image Files (*.png;*.bmp;*.jpg)|*.png;*.bmp;*.jpg";
DialogResult Action = a.ShowDialog();
if(Action == DialogResult.OK) // <-- To fix
{
PictureBox.Image = Image.FromFile(ofd.FileName);
}
}
use something like this:
Dispatcher.Invoke(new Action(() =>
{
using (SaveFileDialog fd = new SaveFileDialog())
{
var json = JsonConvert.SerializeObject(arScene, Formatting.Indented);
var bytes = UTF8Encoding.UTF8.GetBytes(json); // or any byte array data
fd.Filter = "JSon files (*.json)|*.json|All files (*.*)|*.*|ARScene (*.ARScene)|*.ARScene";
fd.Title = "Save an ARScene File";
fd.AutoUpgradeEnabled = true;
fd.DefaultExt = "ARScene";
fd.OverwritePrompt = false;
fd.RestoreDirectory = true;
fd.SupportMultiDottedExtensions = true;
fd.CreatePrompt = false;
if (fd.ShowDialog() == DialogResult.OK)
{
if (fd.FileName != "")
{
FileStream fs = (FileStream)fd.OpenFile();
if (fs != null)
{
fs.Write(bytes, 0, bytes.Length);
fs.Close();
}
}
}
fd.Dispose(); // not needed, but save;-)
}
}));