Timer in C# starts when the application starts rather than when I press the button












0















I would appreciate your help. I am a total beginner and just about started to learn programming in C#.



I have a form ("ThisProgram") with 2 buttons.
1st button ("OK") is supposed to close the form and start an external program.
2nd button ("Later") is supposed to hide the form and show it again in 2 minutes.
textBox1 is there just for me to see the timer works.



I have 2 problems:



1) The timer that's supposed to open the window (ThisProgram.exe) again if you click "Later" starts when the program starts even if you don't click "Later"



2) Hide(); ... After putting this line in there, it will open the window in 2 minutes and then after another 2 minutes again and so on forever for some reason.



Here's my code:



/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();

//
// TODO: Add constructor code after the InitializeComponent() call.
//
}

int duration = 0;

void Timer1Tick(object sender, EventArgs e)
{
duration++;
textBox1.Text = duration.ToString();
System.Diagnostics.ProcessStartInfo start = new System.Diagnostics.ProcessStartInfo();
start.FileName = @"C:ThisProgram.exe";
Process.Start(start);
if (duration == 1)
{
Timer.Stop();
}
}

void LaterClick(object sender, EventArgs e)
{
Timer.Enabled = true;
Timer.Start();
Hide();
}

void OKClick(object sender, EventArgs e)
{
System.Diagnostics.ProcessStartInfo start = new System.Diagnostics.ProcessStartInfo();
start.FileName = @"C:OtherExternalProgram.exe";
Process.Start(start);
Application.Exit();
}
}


Thank you in advance for your help. If there's anything else wrong or strange in the code, I'd appreciate it too if you let me know.










share|improve this question




















  • 4





    We need to see the code for the timer from your designer file. Do you have Enabled set to true there?

    – itsme86
    Nov 14 '18 at 16:06








  • 2





    You can check that by opening the form designer and looking at the timer properties

    – None of the Above
    Nov 14 '18 at 16:07











  • Set Timer.Enabled = false; in form constructor so it doesn't run until you press the click.

    – jdweng
    Nov 14 '18 at 16:17











  • Oh, so this was it! Now everything works fine. Thank you for your help.

    – Dunno123
    Nov 14 '18 at 16:49
















0















I would appreciate your help. I am a total beginner and just about started to learn programming in C#.



I have a form ("ThisProgram") with 2 buttons.
1st button ("OK") is supposed to close the form and start an external program.
2nd button ("Later") is supposed to hide the form and show it again in 2 minutes.
textBox1 is there just for me to see the timer works.



I have 2 problems:



1) The timer that's supposed to open the window (ThisProgram.exe) again if you click "Later" starts when the program starts even if you don't click "Later"



2) Hide(); ... After putting this line in there, it will open the window in 2 minutes and then after another 2 minutes again and so on forever for some reason.



Here's my code:



/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();

//
// TODO: Add constructor code after the InitializeComponent() call.
//
}

int duration = 0;

void Timer1Tick(object sender, EventArgs e)
{
duration++;
textBox1.Text = duration.ToString();
System.Diagnostics.ProcessStartInfo start = new System.Diagnostics.ProcessStartInfo();
start.FileName = @"C:ThisProgram.exe";
Process.Start(start);
if (duration == 1)
{
Timer.Stop();
}
}

void LaterClick(object sender, EventArgs e)
{
Timer.Enabled = true;
Timer.Start();
Hide();
}

void OKClick(object sender, EventArgs e)
{
System.Diagnostics.ProcessStartInfo start = new System.Diagnostics.ProcessStartInfo();
start.FileName = @"C:OtherExternalProgram.exe";
Process.Start(start);
Application.Exit();
}
}


Thank you in advance for your help. If there's anything else wrong or strange in the code, I'd appreciate it too if you let me know.










share|improve this question




















  • 4





    We need to see the code for the timer from your designer file. Do you have Enabled set to true there?

    – itsme86
    Nov 14 '18 at 16:06








  • 2





    You can check that by opening the form designer and looking at the timer properties

    – None of the Above
    Nov 14 '18 at 16:07











  • Set Timer.Enabled = false; in form constructor so it doesn't run until you press the click.

    – jdweng
    Nov 14 '18 at 16:17











  • Oh, so this was it! Now everything works fine. Thank you for your help.

    – Dunno123
    Nov 14 '18 at 16:49














0












0








0








I would appreciate your help. I am a total beginner and just about started to learn programming in C#.



I have a form ("ThisProgram") with 2 buttons.
1st button ("OK") is supposed to close the form and start an external program.
2nd button ("Later") is supposed to hide the form and show it again in 2 minutes.
textBox1 is there just for me to see the timer works.



I have 2 problems:



1) The timer that's supposed to open the window (ThisProgram.exe) again if you click "Later" starts when the program starts even if you don't click "Later"



2) Hide(); ... After putting this line in there, it will open the window in 2 minutes and then after another 2 minutes again and so on forever for some reason.



Here's my code:



/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();

//
// TODO: Add constructor code after the InitializeComponent() call.
//
}

int duration = 0;

void Timer1Tick(object sender, EventArgs e)
{
duration++;
textBox1.Text = duration.ToString();
System.Diagnostics.ProcessStartInfo start = new System.Diagnostics.ProcessStartInfo();
start.FileName = @"C:ThisProgram.exe";
Process.Start(start);
if (duration == 1)
{
Timer.Stop();
}
}

void LaterClick(object sender, EventArgs e)
{
Timer.Enabled = true;
Timer.Start();
Hide();
}

void OKClick(object sender, EventArgs e)
{
System.Diagnostics.ProcessStartInfo start = new System.Diagnostics.ProcessStartInfo();
start.FileName = @"C:OtherExternalProgram.exe";
Process.Start(start);
Application.Exit();
}
}


Thank you in advance for your help. If there's anything else wrong or strange in the code, I'd appreciate it too if you let me know.










share|improve this question
















I would appreciate your help. I am a total beginner and just about started to learn programming in C#.



I have a form ("ThisProgram") with 2 buttons.
1st button ("OK") is supposed to close the form and start an external program.
2nd button ("Later") is supposed to hide the form and show it again in 2 minutes.
textBox1 is there just for me to see the timer works.



I have 2 problems:



1) The timer that's supposed to open the window (ThisProgram.exe) again if you click "Later" starts when the program starts even if you don't click "Later"



2) Hide(); ... After putting this line in there, it will open the window in 2 minutes and then after another 2 minutes again and so on forever for some reason.



Here's my code:



/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();

//
// TODO: Add constructor code after the InitializeComponent() call.
//
}

int duration = 0;

void Timer1Tick(object sender, EventArgs e)
{
duration++;
textBox1.Text = duration.ToString();
System.Diagnostics.ProcessStartInfo start = new System.Diagnostics.ProcessStartInfo();
start.FileName = @"C:ThisProgram.exe";
Process.Start(start);
if (duration == 1)
{
Timer.Stop();
}
}

void LaterClick(object sender, EventArgs e)
{
Timer.Enabled = true;
Timer.Start();
Hide();
}

void OKClick(object sender, EventArgs e)
{
System.Diagnostics.ProcessStartInfo start = new System.Diagnostics.ProcessStartInfo();
start.FileName = @"C:OtherExternalProgram.exe";
Process.Start(start);
Application.Exit();
}
}


Thank you in advance for your help. If there's anything else wrong or strange in the code, I'd appreciate it too if you let me know.







c#






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 21:27









Maximilian Ast

1,90852332




1,90852332










asked Nov 14 '18 at 16:00









Dunno123Dunno123

141




141








  • 4





    We need to see the code for the timer from your designer file. Do you have Enabled set to true there?

    – itsme86
    Nov 14 '18 at 16:06








  • 2





    You can check that by opening the form designer and looking at the timer properties

    – None of the Above
    Nov 14 '18 at 16:07











  • Set Timer.Enabled = false; in form constructor so it doesn't run until you press the click.

    – jdweng
    Nov 14 '18 at 16:17











  • Oh, so this was it! Now everything works fine. Thank you for your help.

    – Dunno123
    Nov 14 '18 at 16:49














  • 4





    We need to see the code for the timer from your designer file. Do you have Enabled set to true there?

    – itsme86
    Nov 14 '18 at 16:06








  • 2





    You can check that by opening the form designer and looking at the timer properties

    – None of the Above
    Nov 14 '18 at 16:07











  • Set Timer.Enabled = false; in form constructor so it doesn't run until you press the click.

    – jdweng
    Nov 14 '18 at 16:17











  • Oh, so this was it! Now everything works fine. Thank you for your help.

    – Dunno123
    Nov 14 '18 at 16:49








4




4





We need to see the code for the timer from your designer file. Do you have Enabled set to true there?

– itsme86
Nov 14 '18 at 16:06







We need to see the code for the timer from your designer file. Do you have Enabled set to true there?

– itsme86
Nov 14 '18 at 16:06






2




2





You can check that by opening the form designer and looking at the timer properties

– None of the Above
Nov 14 '18 at 16:07





You can check that by opening the form designer and looking at the timer properties

– None of the Above
Nov 14 '18 at 16:07













Set Timer.Enabled = false; in form constructor so it doesn't run until you press the click.

– jdweng
Nov 14 '18 at 16:17





Set Timer.Enabled = false; in form constructor so it doesn't run until you press the click.

– jdweng
Nov 14 '18 at 16:17













Oh, so this was it! Now everything works fine. Thank you for your help.

– Dunno123
Nov 14 '18 at 16:49





Oh, so this was it! Now everything works fine. Thank you for your help.

– Dunno123
Nov 14 '18 at 16:49












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53304235%2ftimer-in-c-sharp-starts-when-the-application-starts-rather-than-when-i-press-the%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53304235%2ftimer-in-c-sharp-starts-when-the-application-starts-rather-than-when-i-press-the%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Bressuire

Vorschmack

Quarantine