1
private void Student_btn(object sender, EventArgs e)
{
    SignUp signup = new SignUp();
    signup.Show();
    this.Hide();
}

First-time works successfully, but when I run the second time it does not work

I get this error:

Unable to copy file "obj\Debug\FinalProject.exe" to "bin\Debug\FinalProject.exe". The process cannot access the file 'bin\Debug\FinalProject.exe' because it is being used by another process.

enter image description here

Alamin
  • 1,878
  • 1
  • 14
  • 34
  • Stop the program and try again. – Tomsen Dec 03 '20 at 13:44
  • after run this program, then i need to stop from task manager, but why i don't know. – Alamin Dec 03 '20 at 13:46
  • What is `this` and do you ever show it again? That is most likely what is still running. – crashmstr Dec 03 '20 at 13:47
  • how can I stop it !! is there any way to hide the window? when I will go to the next window – Alamin Dec 03 '20 at 13:50
  • 1
    This leads me to believe you are having another running instance of your program, probalby accidently opened it in two environments. If you are not sure where is it oppened, rebooting your computer should probably solve your problem – 66Gramms Dec 03 '20 at 13:50
  • I have stopped all window from my task manager, but I have face the same problem – Alamin Dec 03 '20 at 13:54
  • use `signup.ShowDialog()` instead. And remove `this.Hide()` – Tomsen Dec 03 '20 at 13:54
  • but i need to hide the first window before i will go to the next window. If i do not use this.Hide() or this.Close(). Then it does not work. – Alamin Dec 03 '20 at 13:58
  • 1
    Well, then do it like this: `this.Hide()` then `signup.ShowDialog()` then `this.Show()` – Tomsen Dec 03 '20 at 14:04
  • Please provide more detail about your requirements and what the expected result is. Currently, your code is hiding the first form, but it's not shown where you activate it again or close it. – Tu deschizi eu inchid Dec 03 '20 at 14:05
  • 2
    The relevent code is where you quit the application. You aren't actually ending the program, probably just hiding the window so it stays running – Garr Godfrey Dec 03 '20 at 14:07
  • working, but it's the right solution! – Alamin Dec 03 '20 at 14:10
  • Garr Godfrey- if i do not use 'this.Hide()' then it's work, but when i use this.HIde() then show this bug. – Alamin Dec 03 '20 at 14:14

1 Answers1

2

The reason why you have this issue is because your previous version is still running in the background. And because it's still running Visual Studio can't remove it's .exe in order to replace it with the new version.

Why is you previous version still running?

You've provided this code:

private void Student_btn(object sender, EventArgs e)
{
    SignUp signup = new SignUp();
    signup.Show();
    this.Hide();
}

Let's assume that this method is part of your MainWindow. When the user clicks the button, the MainWindow opens a new window signup.Show()and hides itself this.Hide().

But that means, that your MainWindow is still running, you just can't see it and it will still be running after you close the new window.

To resolve the problem you have to close the MainWindow properly. And you can do it like that:

private void Student_btn(object sender, EventArgs e)
{
    SignUp signup = new SignUp();
    this.Hide(); //Your MainWindow hides itself and gets invisible
    signup.ShowDialog(); //You code will "stop" here until the new window is closed
    this.Close() //You MainWindow closes itself and your program will stop
}

If you want to resume to your MainWindow when the new window closes. Use this.Show()instead of this.Close()

Tomsen
  • 321
  • 3
  • 12
  • Now working Thank you! – Alamin Dec 03 '20 at 14:52
  • You could also wire up the FormClosed() event of your secondary form and close the primary form there. This would allow you to use Show() instead of ShowDialog(): `signup.FormClosed += (s2, e2) => { this.Close(); };` with `signup.Show();` – Idle_Mind Dec 03 '20 at 16:08