Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello
My problem is i want to take the amount of seconds from user, and my program will run for this amount of seconds. Now i am very new in coding i Know how to get a value from text box but i want to convert it in the seconds?. for example if user entered 3 i want it to be 3 seconds.English is not my first language so pardon me
C#
counter=Convert.Toint32(textBox1.Text.Trim());


Thanks anyways
Posted
Comments
Joezer BH 8-Sep-13 7:44am    
Try to explain more on the nature of your problem,
your line is just fine if you only want to convert the value (see solution posted below).

Your line is just fine for converting your textbox's text to an int.
C#
//You could also use 
int.Parse(textBox1.Text);


Using the counter variable in your code as seconds will be when you use it in the code's context.
e.g.

C#
main()
{
  counter = Convert.Toint32(textBox1.Text.Trim());

  // Call Foo with a Datetime value of now plus the amount of seconds from the textbox.
  Foo(Datetime.Now.AddSeconds(counter));
}


Foo(Datetime dt)
{
  // ... do stuff ...
}


Cheers,
Edo
 
Share this answer
 
If you want to delay the execution of a code block for a given period, and want the period to be given as input at runtime from the user, you code to take in intervals in seconds is ok. Once you have this time, convert it into milli-seconds (x 1000) and have a call to Thread.Sleep(period) and then the call to the delayed functionality.
C#
int seconds = int.Parse(txtTime.Text);
int msec = seconds * 1000;
Thread.Sleep(msec);
DelayedFunctionCall();


Just remember that this delay if too long, will actually hang out your system so it will be better to have the delay call function in separate application which you can call from here (Use System.Diagnostic.Process 's Start method). This way your process will be callable after a delay without making your main function unresponsive.

Or else you can have a BackgroundWorker created, which gets triggered after the interval, does it's job and returns you the output without making your app hanging. Just be sure not to close your main app before getting the output, else your background thread will get terminated automatically also.
 
Share this answer
 
Comments
BillWoodruff 9-Sep-13 5:50am    
In this case the OP wants to control how long his whatever (thread ? code ? external application ?) runs, not how long to delay running it.
Nitin Singh India 9-Sep-13 8:49am    
Does it means that after a given period, the code's context will be destroyed?
usually its a wait for a given period and have a timeout post that.
I was about to post this a few hours ago, and then my computer crashed, and some business came up that had to be handled, but I'll post it anyway.

I assume that when the OP says "take the amount of seconds from user, and my program will run for this amount of seconds," they mean they want to execute something (code ? process ? thread ? launch an external application ?), and, if whatever they run is still running after that number of seconds, they want to stop it from running: so it's a time-limit scenario.

I'd guess that something is in code they have "at hand," and they'll most likely use a thread, or the BackGroundWorker "front-end" to using a thread. Without knowing exactly what the OP is going to run, we can't do anything but fantasize about: how the time-limit is "enforced."

So, that assumption lead me to think the OP wants to convert a string to an integer, which will then used to set the duration of a TimeSpan.
private void someTrigger_Click(object sender, EventArgs e)
{
    int seconds;

    if (Int32.TryParse(textBox1.Text, out seconds))
    {
        // valid integer

        // call your code to execute
        // goDoMyStuff(new TimeSpan(0,0,0,seconds), Type param1, Type param2)
    }
    else
    {
        // handle invalid entry by user
    }
}
But, calculating a "time to quit" in advance of code execution as Canny Stark/Edo suggests here is equally valid, and may well be more efficient depending on factors we can only guess at now.

As you can see, my assumption was that the "something executed" would take in the duration, and handle enforcing the time-limit internally (with or without a callback would, of course, depend on what the OP is doing ... which we don't know).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900