So first of all, I have multiple windows which I hide when opening another window, so I have to use
Application.Current.Shutdown();
to completely close the Application when pressing the "x" on the top right.
I wanted to handle the WindowClosing Event in my Home.xaml.cs file. But if I do this:
public Home()
{
InitializeComponent();
Closing += WindowClosing.OnWindowClosing;
}
then I am getting an System.Threading.Tasks.TaskCanceledException when I am closing the window.
Here is the WindowClosing event handler:
public static void OnWindowClosing(object sender, CancelEventArgs e)
{
Application.Current.Shutdown();
}
The weird part is, that I´ve done exactly the same with the login window, and it works there without any trouble.
I´ve stepped through it, and the closing event gets set as it should (in the Login.xaml.cs, as also in the Home.xaml.cs File).
I know this is not much Information for this error (I think thats all, but maybe the error is coming from somewhere else?!), but maybe someone else encountered this issue and can help me.
If you need more information just tell me, I will edit the question then.
Thanks for your help!
EDIT:
The Solution was taking Environment.Exit(0); instead of Application.Current.Shutdown();
In your OnWindowClosing() method, you can try substituting the following line for Application.Current.Shutdown():
Environment.Exit(0);
Related
I know full path of file var temp_file = Path.Combine(_directoryName1, "temp.ini"); which need to be deleted in the end of program working. How could I do this ? As I know it is possible to realize via OncloseEvent(). In addition, I dont know exatly how user will close application via alt+f4 or via buttons.
So far, I have tried to use this code below from almost the same question How to override onclose event on WPF?
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
//do my stuff before closing
base.OnClosing(e);
}
And I have added it in App.xaml.cs but it doesn't work. VS2013 says that he don't know such method base.OnClosing(e);
Is there any mistake or another way out?
Window.Closing is for a specific Window. That's probably not what you want in any case, because the Closing event can be cancelled. Window.Closed is likely a better choice.
To run something when the program is closed -- not tying yourself to a window -- you should subscribe to the Application.Exit event instead.
My C# application starts by opening a form. In the constructor for that form I "showDialog" an openfiledialog. After selecting a file to open, the openfile dialog closes, the file is loaded and the contents displayed in the main form but the main form is buried behind every other open window on my desktop.
I have to find it in the task bar and bring it to focus. I just started the application, I want the form to have focus.
I have written other applications that do not use the openfiledialog and when I start them the main form opens with focus as you would expect.
How do I make the main form get focus after the openfiledialog closes?
I have tried
this.focus(),
this.activate(),
this.bringtofront();
and this.TopMost = true;
None of them make any apparent difference at all.
I have research this problem extensively and this are the things everyone suggests and say work, but they don't work for me. Some have insinuated that I am violating all that is holy by trying to make my form topmost. However, I don't think very many people would like to open an application and have the main form for it show up behind everything else.
Any one have any other ideas about how to make sure my form is "in front", topmost, has focus?
When you do it this way, your application will have a brief moment where no window is available to receive the focus after the dialog closes. Windows is forced to find another window to give the focus to, that will be a window of another app. Your main window eventually appears, now behind that other's app window.
Display the dialog in an event handler of the Shown event instead. Or use the boilerplate File + Open command.
SOLUTION: this.Activate(); works but if called from the form Load event.
This will set the window on top:
[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
private void Form1_Load(object sender, EventArgs e)
{
....
//after your code place the call to the function at the end.
SetForegroundWindow(this.Handle);
}
Althought as Mr. hans said and very well you are better off with another design.
If you can, move the ShowDialog out of the constructor, or try putting this in the constructor:
this.Shown += OnShown;
and move your ShowDialog to here:
private void OnShown(object sender, EventArgs eventArgs)
{
var result = new OpenFileDialog().ShowDialog();
}
My program has multiple forms. The fifth and final form has a button that when clicked closes the application using the method Application.Exit(). However every time I click the button I receive the error 'cannot access a disposed object' surrounding this code on my first form:
frm2 f2 = new frm2();
this.Hide();
f2.ShowDialog();
this.Show();
The compiler indicates that the statement this.show() is the problem. Could someone explain why I am receiving this error and how to fix it?
Ok edited my answer, I reproduced your issue. If you want to use Form.ShowDialog then you should set the DialogResult of the control that is closing the application. So in the buttons properties you should set the dialog result to something, for example Cancel.
Then on the buttons click event you would do something like this:
private void btnClose_Click(object sender, EventArgs e)
{
if (this.DialogResult == DialogResult.Cancel)
{
Application.Exit();
}
}
Otherwise if you don't need to use Form.ShowDialog, you can just show Form2. The above does not produce the error in my testing.
In your code example, did frm2 make a call to Application.Exit? If it did, then why are you trying to call this.Show again?
Anyway, you may have a problem related to how you started the application's message loop. Are you running Application.Run(), or Application.Run(form1)?
If you provided a form to Application.Run() when you started your message loop, then you should not be calling Application.Exit in order to exit the application. Instead, you should simply close your main window, that would cause the message loop to finish, the call to Application.Run to return, and your application will terminate cleanly.
I'm using WebBrowser and when I'm trying to call .Navigate(some_local_html) then nothing is displayed on my browser. If I then use MessageBox.Show(), then while message is shown I can see my html in browser. But when I close MessageBox, html is missing again.
I've tried Try-catch, but there was no errors.
I was trying to set default url on webBrowser control, and there is no result also. I can see nothing.
RESOLVED:
That wasn't a thread itself, but some kind of thread. I added next code:
Stream stream = null;
webBrowser1.DocumentStream = stream;
and forgot to remove it... That's a reason.
Thanks everyone!
Not sure if this will help at all, but it sounds like something is redrawing in the background, as when you put a messagebox up I am sure it sleeps the thread so nothing else can happen until it has been actioned, so whatever is overwriting it would be stopped temporarily.
If you do have something on that thread refreshing or redrawing frequently that could be causing your problems, try adding a button to your form which does a thread.sleep(1000) to see if that correctly displays your browser for a second.
It'd be helpful to know where you are calling your navigate and MessageBox functions. I quickly created a test to see if I could produce a similar result but the code below worked exactly as expected.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.webBrowser1.Navigate("http://www.google.com");
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Some Text");
}
}
I am working on learning Windows Forms with C# and have a bare bones application. I am trying to close it when the user selects File->Exit. I have an event handler attached to it and I have tried calling Application.Exit(), Application.ExitThread() and just closing the form. Nothing. It stays there. I'm not creating any other threads of any sort either.
Ideas? Thanks.
Have you tried to put a breakpoint in the event handler to see if it is being hit?
If so, the application won't exit if the window messages aren't being delivered (i.e. the UI thread is blocked). One way to test this is to call Environment.Exit() which is more brutal about forcing a close. If this succeeds, you can then figure out why Application.Exit() isn't working.
Application.Exit isn't the normal way to close a GUI application. Use form.Close instead.
private static void OnMenuClose_Click(object sender, System.EventArgs e)
{
Form dlg = ((Control) sender).FindForm();
//dlg.DialogResult = DialogResult.OK;
dlg.Close();
}