C
C#2y ago
The Joker

❔ Help to close a forms from another forms C#

I created 2 scripts with a timer with intervals of 1000 ms, both one is to open a new form when a certain process is started, this side is working fine, and the other is to close the same forms when the given process is closed, I've tried several times forms and I can't close the forms that were opened when the process is finished, could someone help me?
private void timer2_Tick(object sender, EventArgs e)
{
Process[] processes = Process.GetProcessesByName("teste");

if (processes.Length == 1)
{
frmTimer tm = new frmTimer();
tm.Show();

timer2.Stop();
}
}

///


private void timer1_Tick(object sender, EventArgs e)
{
Process[] processes = Process.GetProcessesByName("teste");

if (processes.Length == 0)
{
frmTimer tm = new frmTimer();
tm.Close();

timer1.Stop();
}
}
private void timer2_Tick(object sender, EventArgs e)
{
Process[] processes = Process.GetProcessesByName("teste");

if (processes.Length == 1)
{
frmTimer tm = new frmTimer();
tm.Show();

timer2.Stop();
}
}

///


private void timer1_Tick(object sender, EventArgs e)
{
Process[] processes = Process.GetProcessesByName("teste");

if (processes.Length == 0)
{
frmTimer tm = new frmTimer();
tm.Close();

timer1.Stop();
}
}
5 Replies
Relevant
Relevant2y ago
In this example, these are 2 separate instances of frmTimer You're closing a different one
The Joker
The JokerOP2y ago
yes one instance to open and another to close?
Relevant
Relevant2y ago
No, instance. Like you can open that form 2 times. And they're 2 separate entities. You can close one and keep the other one open What you probably want to do is declare the frmTimer outside of the methods so you have access to the same instance of the form inside of both methods
frmTimer tm = new frmTimer();
private void timer2_Tick(object sender, EventArgs e)
{
Process[] processes = Process.GetProcessesByName("teste");

if (processes.Length == 1)
{
tm.Show();

timer2.Stop();
}
}

///


private void timer1_Tick(object sender, EventArgs e)
{
Process[] processes = Process.GetProcessesByName("teste");

if (processes.Length == 0)
{
tm.Close();

timer1.Stop();
}
}
frmTimer tm = new frmTimer();
private void timer2_Tick(object sender, EventArgs e)
{
Process[] processes = Process.GetProcessesByName("teste");

if (processes.Length == 1)
{
tm.Show();

timer2.Stop();
}
}

///


private void timer1_Tick(object sender, EventArgs e)
{
Process[] processes = Process.GetProcessesByName("teste");

if (processes.Length == 0)
{
tm.Close();

timer1.Stop();
}
}
There might be better ways to do that, but at least that should function the way you're intending it to
The Joker
The JokerOP2y ago
correct I understand now Thanks bro. Worked fine Thanks....
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server