Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi, I am trying to get the Average Time the User has the Program open. But I have a Problem I can't solve.

C#
TimeDate timePerOpen = new DateTime(Settings.Default.userTimeCount_ProgramOpen.Ticks / Settings.Default.userActionCound_ProgramStart));


Both Settings.Settings work totally fine, I checked that. But I think that they have problems to divide. timePerOpen is totally unrealistic like 2 min total open time = 56 years average open time.

userActionCound is never null or 0

Thanks for your help ;)
Posted
Comments
BillWoodruff 21-Dec-14 23:46pm    
Do note that if you divide an integer by an integer you will get back an integer: don't you need a Double result ?
[no name] 22-Dec-14 6:55am    
Oh that's Clever

I'll try that

EDIT: Settings.Default.verificationCount_ProgramTimespan.Ticks is 630822816029950569 and doesnt fit into an Integer. It's a long so i made the int a long too.

Still doesn't work

DateTime averageTime = new DateTime(Settings.Default.verificationCount_ProgramTimespan.Ticks / (long)Settings.Default.verificationCound_ProgramOpenCount));

I opened it 4 times
It was open for 3 secs
Expected: 750 miliseconds average time
What I got: -1499 Years, -6 hours

averageTime = 01.10.0500 18:00:00

This Problem seems to be a hard nut to crack, or i am just too stupid

1 solution

Try using TimeSpan instead of DateTime when doing the computation. As someone else pointed out, one needs to cast the division so that everything works out. Here is the fixed solution with the cast.

Using your inputs:

C#
Int32 count = 4;
TimeSpan totalOpened = new TimeSpan(0, 0, 3);
TimeSpan t = new TimeSpan((long)(totalOpened.Ticks / count));
Console.WriteLine("Avg. time open = {0:d.hh:mm:ss.ff}", t.ToString());


Times opened = 4
Total time = 3 seconds

The output will be:

Avg. time open = 00:00:00.7500000


Average time = 750 milliseconds
 
Share this answer
 
v3
Comments
[no name] 22-Dec-14 6:21am    
I tested it, didn't worked

Here the Code I used:

public void getAverageOpenTime()
{
TimeSpan timePerOpen = new TimeSpan(Settings.Default.userTimeCount_ProgramOpen.Ticks / Settings.Default.userActionCound_ProgramStart);
DateTime dt = new DateTime(Settings.Default.userTimeCount_ProgramOpen.Ticks / Settings.Default.userActionCound_ProgramStart);
//breakpoint
}


Here the Values I got:
Settings.Default.userActionCound_ProgramStart . . 11
Settings.Default.userTimeCount_ProgramOpen . . . {01.01.2000 00:00:31}
TimePerOpenTimeSpan . . . . . . . . . . . . . . . . . . . . . . .{66374.10:54:35.5928260}
TimePerOpenDateTime . . . . . . . . . . . . . . . . . . . . . . .{23.09.0182 10:54:35}
Hung, Han 22-Dec-14 17:23pm    
When storing the userTimeCount_ProgramOpen, it should also be a TimeSpan data type. According to your output, it looks like it was stored as a DateTime data type since it has the year component of 2000. So that will factor into any calculations you do with that setting. By using TimeSpan, it will eliminate the date component if there no days to consider.

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