Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
Hello! I would need some help converting milliseconds to minutes.. and I dont mean like..

"Hey here you go 1 second is 1000 milliseconds, 60 seconds is a minute.. Do the math.."

I am simply asking if there was a way I could programatically convert the Thread.Sleep(); so it counts in minutes instead of milliseconds.
Lets say I want the application to sleep for 35 minutes thats 35 x 60 = 2 100 000

I want to be able to tell the application to read in minutes instead of milliseconds because 35 is alot easier to type and remember than 2 100 000, I dont know about you but for me it is atleast.

So what I need to do is..

I have the application which I already showed a picture of(at the bottom), and when typing into the /min textbox I am currently typing in the value in milliseconds, I would like to be able to write it in minutes.. But since the Sleep.Thread(); only reads in milliseconds (as far as I know) I need help to change it aka (Also known as) convert it (The milliseconds to minutes)

http://i.imgur.com/XAXYv4d.png[^]



C#
private void startlbl_Click(object sender, EventArgs e)
{
    Process.Start(textBoxFirst.Text);

    var firstProcess = Process.Start(textBoxFirst.Text);
    int timeOne = Convert.ToInt32(MinTextBoxFirst.Text);
    Thread.Sleep(timeOne); // I want to convert timeOne to minutes and not milliseconds
    firstProcess.Kill();

}


What I have tried:

Ive tried looking into overloads
Posted
Updated 16-Jun-16 4:05am
v2
Comments
Garth J Lancaster 16-Jun-16 9:50am    
I dont get the issue - somewhere in your code you read a ? textbox, convert it to int (+ validate ?), and pass that result to Thread.Sleep() .. so, assuming Int i is in minutes, why not Thread.Sleep(i * 60 * 1000) ?

Thread.Sleep also accepts TimeSpan as a parameter.
See Thread.Sleep Method (TimeSpan) (System.Threading)[^]

Then you can set the time to almost what you want.

For example, you write the time as 35:00 in the text box and then use one of the
TimeSpan.Parse methods to convert the time interval.
You have to follow certain rules, of course, so maybe a MaskedTextBox can be an alternative.
 
Share this answer
 
v2
How about


C#
Thread.Sleep(1000 * 60 * Minutes);


That's what I usually do, its readable :)
 
Share this answer
 
Comments
BladeLogan 16-Jun-16 9:47am    
I want the users input to be converted, will update my code above!
EveryNameIsTakenEvenThisOne 16-Jun-16 9:49am    
What's the problem? Just assign "Minutes" to whatever's in your TextBox.
BladeLogan 16-Jun-16 9:50am    
Really? It gets so confusing when you've nver worked with this type of stuff before :p , so just timeOne * 60 * minutes?
EveryNameIsTakenEvenThisOne 16-Jun-16 9:53am    
No. Either Sleep(1000ms * 60s * Minutes) or Sleep(TimeSpan.FromMinutes(Minutes))

BladeLogan 16-Jun-16 9:56am    
I see what you mean but how would it connect to the users input in Thread.Sleep(timeOne);
C#
public void ThreadSleepInMinutes(int minutes)
{
    Thread.Sleep(new TimeSpan(0, minutes, 0));
}
See: [^].

The documentation is your friend.
 
Share this answer
 
Comments
BladeLogan 16-Jun-16 9:56am    
I see what you mean but how would it connect to the users input in Thread.Sleep(timeOne);
Richard Deeming 16-Jun-16 11:30am    
The TimeSpan.FromMinutes[^] method might be slightly clearer than trying to remember which positional argument of the constructor relates to which element of the TimeSpan. :)
As soon as you stop hard-coding numbers passed to methods (that is, using immediate constants), it will stop being a problem. Such matter-of-convenience problems are usually step from one or another kind of misuse. In real development, they don't even appear. Take the Thread.Sleep. It is rarely needed and often is a result of trying to synchronize things, which is simply false synchronization, this: Race condition — Wikipedia, the free encyclopedia.

In legitimate cases, if can be just one value, but then it should rather be an explicitly defined constant. Please see the code sample below. Again, Thread.Sleep rarely makes sense, but it often makes sense in some software controlling external hardware. The the delay values should not be hard-coded even in constants, they should come from some data describing each device separately. And so on.

But the question is asked, I have to answer. One solution is Solution 1. Again, it's a matter of style. One approach could be:
C#
const int min = 60000;

//...

Thread.Sleep(2*min);

It won't allow you to enter fractions of a minute. Another obvious approach is defining your own Sleep method(s) accepting the parameters you want, anything you want. In its implementation, it should calculate milliseconds or an instance of System.TimeSpan and call Thread.Sleep.

—SA
 
Share this answer
 
Comments
BladeLogan 16-Jun-16 18:28pm    
What worked was this

Thread.Sleep(TimeSpan.FromMinutes(timeOne));

I converted the users input into minutes instead of milliseconds
Sergey Alexandrovich Kryukov 16-Jun-16 19:35pm    
Of course, but I explained two alternatives to you. Did you understand them?
—SA
BladeLogan 16-Jun-16 19:39pm    
Kinda.. One of them was to create a int and hold it and use it later

const int min = 60000; more than that I didnt really understand thats why I went with the other method.

Also with the method im using right now, it wont let me kill 2 processes at the same time, they interfear somehow, but if you would like to explain your methods a bit more I would love to read it and maybe try them out and see what works for the better because this is the general thought of the application.



Imagine you have an application with 4 text boxes and 3 buttons
Like this

[Button1][Textbox1] [Textbox2]
[Button2][Textbox3] [Textbox4]
[Button3]

When user presses button1 it opens a filedialog and lets the user select a program (the user is me)
when the user presses okay or select it closes the filedialog and enters the file url into textbox1

the user can input a value on textbox 2 to let the application know how long the file from the file url should be open.

when the user presses the button 3 it starts the file in the file url and waits the ammount of time that the user put in to textbox 2 and then closes the software

same logic applies to [Button2][Textbox3] [Textbox4]


What would be a better approach because mine is kinda working now, its messing up with the [Button2][Textbox3] [Textbox4] but yeah, if you have any better idea I would love to hear it
Sergey Alexandrovich Kryukov 16-Jun-16 20:16pm    
How can you mix up unrelated things, simple numerical issues with killing processes and the like?
The problem is extremely simple.

What does it mean:
"const int min = 60000; more than that I didnt really understand thats why I went with the other method"?
What needs understanding here?

How can you mix up UI with the same simple numeric issues? If the user enters text, you can interpret it the way you want. Look, I started my answer from the idea that you are trying to create a problem from a non-existing problem. That's the essence, the rest is just not so essential detail.

—SA

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