Reputation: 896
EDITED CODE/QUESTION. BEGININVOKE IS NOT LONGER ON THE TABLE. I'm having trouble getting a SaveFileDialog to show its dialog. When I call ShowDialog normally I get no response at all. The code falls through and acts like the SFD was never called in the first place. I also tried running this inside another thread. Same problem. When I run it inside the main_load() routine everything works fine. I'm using .NET 4.0. I get the feeling this is happening because I'm calling the SFD code as part of an process exited event. MessageBox.Show() works fine!!! My sample code is shown below. Does anyone know what could be causing this error? Thanks!
procConvert.Exited += new EventHandler(ConversionExited);//inside another routine
private void ConversionExited(object sender, System.EventArgs e)
{
try{
//works fine
MessageBox.Show("OverWrite old Gcode file " + NewGcodeName, "Warning File Exist",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Question,MessageBoxDefaultButton.Button1);//works fine
SaveFileDialog SFD = new SaveFileDialog();
SFD.Filter = "text files (*.gcode)|*.gcode|All files (*.*)|*.*"; //set up filter for gcode and all
DialogResult result = SFD.ShowDialog(); //show dialog
if (result != DialogResult.OK) return;
NewGcodeName = SFD.FileName;
if (File.Exists(NewGcodeName + "1")) File.Delete(NewGcodeName + "1");//erase the destination
System.IO.File.Move(NewGcodeName, NewGcodeName + "1");//copy to backup location, smart thing to do would be to make like 5 backups then toss
}
catch(Exception ex)
{
//handle exception
}
}
Upvotes: 1
Views: 3138
Reputation: 2258
This is a somewhat lousy answer, but I had this problem on a W10 machine that would just spin and spin. It may have had something to do with the initial directory that the dialog was pointed to. Restarted the PC, and things are much improved. Not the answer I'd want, but maybe the answer you need.
Upvotes: 0
Reputation: 896
The issue was the thread. Somehow C# knew that I was calling the SFD from an exiting routine even when I started another thread. I ran everything from a timer task routine with a flag to turn the SFD on/off and everything ran perfectly. Thanks for all the help everyone.
Upvotes: 1
Reputation: 8551
You're using the SaveFileDialog
on a different thread from the one it was created on.
This is not allowed.
For a start, you could put the first two lines and the NewGcodeName = SFD.FileName;
inside the delegate definition. But then you'll still have the problem that BeginInvoke
is intentionally asynchronous, so your delegate won't have finished running by the time you try to use the result
variable. To solve this problem, try using Invoke
instead of BeginInvoke
.
Edit: this answer assumes that the code shown is running on a different thread from the GUI. If it's not, this answer makes no sense, but then I'd ask why you're using BeginInvoke
at all...
Edit 2: after reading your question and the comments more carefully, I realize that these problems (though they need to be addressed) aren't actually what's causing the error message you posted. That error message is telling you that the Form
or other Control
in whose class this code is running hasn't been shown yet (doesn't have a window handle). You'll need to fix that, too, to have a working solution.
Upvotes: 2
Reputation: 2042
Please format your code better. It's hart to read it.
And why would you use BeginInvoke
here?
The problem is that you call BeginInvoke
before the form/control is actually created (I guess you call it inside the constructor). Try to move your code to a location which is called after the form/control is created or remove the BeginInvoke
.
Upvotes: 0
Reputation:
SaveFileDialog SFD = new SaveFileDialog(); SFD.Filter = "text files (.gcode)|.gcode|All files (.)|.";
To open dialog use
SFD.ShowDialog();
Upvotes: -1