tags:

views:

72

answers:

4

I've some pocket pc app and i'm having a serious problem with it described here: http://stackoverflow.com/questions/472598 . While looking for a solution i tried some actually quite stupid code in Main():

[MTAThread]
        static void Main()
        {
            Application.Run(new Tasks());
            Application.Exit();
        }

and set breakpoint on exit. if i just run the application and then close the window the breakpoint is reached. if i run the application and then open another window:

private void questButton_Click(object sender, EventArgs e)
        {
            QuestionnairesWindow questWindow = new QuestionnairesWindow();
            questWindow.Show();
            this.Hide();
}

and then get back from it to initial window:

private void backButton_Click(object sender, EventArgs e)
        {
            Tasks tasksWindow = new Tasks();
            tasksWindow.Show();
            this.Close();
        }

and close the initial one the same way as the first time, the Apllication.exit() code is never reached and i have an impression that the application isn't really closed ( i can't open it again). sorry if the description is complicated

edit: the question is - any ideas why is it behaving differently?

A: 

This line seems to be the problem:

Tasks tasksWindow = new Tasks();

If I understand your code correctly, in your backButton_Click, you're creating a new instance of your Tasks form, isntead of showing the one you originally hid. This means that the application never terminates, because there is still one window open, but hidden.

What you probably want to do is pass the Tasks form reference to the QuestionnairesWindow form.

Vojislav Stojkovic
+2  A: 

new Tasks() in Main() is not the same object with Tasks tasksWindow = new Tasks();

You got 2 objects of Tasks, so closing second, first is still present and never dies. You need to pass to QuestionnairesWindow the reference of current Tasks.

You can do that with additional QuestionnairesWindow constructor:

private Tasks tasks;

public QuestionnairesWindow(Tasks t)
{
  this.tasks = t;
}

using:

new QuestionnairesWindow(this).Show(); // where this = current `Tasks` created in `Main`
abatishchev
i have no idea how i could have missed that
agnieszka
A: 

The issue you have is that in backButton_Click you are creating a new instance of the Tasks window. The original Tasks window is still running, it is just hidden. I suspect that you need to pass a reference to the original Tasks form into the QuestionnairesWindow so it can choose to show it again.

You either need to add a public/internal method on your QuestionnairesWindow where you set which Tasks form caused the QuestionnairesWindow to open. You call this method in questButton_Click with the this reference prior to calling this.Hide(). This reference will be stored in a private variable within the QuestionnairesWindow.

Then in the backButton_Click, you use this private variable, for calling the .Show() method, to show the parent form.

Liam Westley
A: 

i still get OBjectDisposedException while closing app as if something wasn't disposed though i'm certainly showing the same window reference

agnieszka
could you please provide more info about this issue or create a new question to give you some help for easy
abatishchev