Timer in C# starts when the application starts rather than when I press the button
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#
add a comment |
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#
4
We need to see the code for the timer from your designer file. Do you haveEnabled
set totrue
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
add a comment |
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#
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#
c#
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 haveEnabled
set totrue
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
add a comment |
4
We need to see the code for the timer from your designer file. Do you haveEnabled
set totrue
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
4
We need to see the code for the timer from your designer file. Do you have
Enabled
set totrue
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