Do something at a given (odd) BPM [duplicate] - c#

This question already has answers here:
High resolution timer in C#
(5 answers)
Closed 5 years ago.
No matter where I look, I can't find a good answer to this question. I'd like to have something happen at a given BPM (in my example, I'm using BPM), but the basic C# Timer class isn't working for me. Since it only measures in milliseconds, any actions performed within the timer get noticeably unsynced from the music. I've attempted to use this MicroTimer Library but with no luck! Though it can be quite fine grained, it's resource heavy and it doesn't have the resolution necessary. I understand I can have a function with a counter, but is there a good way to do this with Visual Studio's libraries (like the basic timer)? I hear those aren't as processor hungry.

I doubt you'll get the kind of time resolution you're looking for in a managed language like C#.
Hell, even if you were writing in C the OS could decide another process is more important and just like that you're out of sync.
Maybe consider using the timer, but resyncing every second or half second? I'd default to another user if they have experience in this area, but I'd at least give that a shot. Or go by the system clock ticks?

Related

Restart Application If using too much RAM [duplicate]

This question already has answers here:
How to get memory available or used in C#
(6 answers)
Closed 9 years ago.
I'm creating a server (Console App), but after doing some long-term testing I found out it grows eating RAM. For the local test suite, I am not working with much RAM
(8GB-DDR3 #2400MHz)Is there a way (In Program.cs, I assume) to restart the program if it is using over 'x' amount of RAM? Also, one way could be a timed loop/checkup?
You can use GC.GetTotalMemory. It returns an approximate value (long) of how much memory your program has allocated.
You can create a Timer object and make this comparison under the Tick event handler.
For more information, you can look here: http://msdn.microsoft.com/en-us/library/system.gc.gettotalmemory.aspx
I agree with what others have said about fixing your memory leak.
If you want to restart your program, create a second application that monitors the first process. Then, when memory gets too high in your original app, safely shut it down and allow the second application to launch it again.

C# lock vs Java syncronized - Is there any difference in runtime? [duplicate]

This question already has answers here:
Are there any differences between Java's "synchronized" and C#'s "lock"?
(3 answers)
Closed 9 years ago.
I'm wondering if there is any difference in runtime at lock vs syncronized.
I have learnd that syncronized is a slow operation and outdated at Java.
Today I saw the lock at C# and I'm wondering if they are the same and lock is something I "want" to avoid same as in Java or maybe he is much faster and I want to use it...
Thanks!
1 synchronized is not outdated, java.util.concurrent.locks package simply provides extended functions which are not always needed.
2 Locking is done at CPU level and there is no difference between Java and C# in this regard
see http://www.cs.umd.edu/~pugh/java/memoryModel/jsr-133-faq.html
... special instructions, called memory barriers, are required to flush or invalidate the local processor cache in order to see writes made by other processors or make writes by this processor visible to others. These memory barriers are usually performed when lock and unlock actions are taken; they are invisible to programmers in a high level language.

Run System.Timers.Timer event on specific day and time each week c# [duplicate]

This question already has answers here:
How to call a method daily, at specific time, in C#?
(19 answers)
Closed 9 years ago.
I'm in the process of writing a windows service using System.Timers.Timer to keep track of my interval. What I'd like to do is make it so my service will launch on a specific day and time based on variables in my app.config file.
I don't think you're really writing a Windows service, I think you're writing a job. A cleaner, and easier approach, to your problem would be to write a Console application and then setup a Windows Scheduled Task to run that Console application at the intervals you want.
Never mind the fact that you'll have to set the time in milliseconds with the Timer approach, the Windows service would not be capable of handling Daylight Savings Time and more.
Although I think your best bet is to make this a scheduled task, you can easily create a waitable timer that will signal at the same time every day. Windows has a Waitable Timer object, but there's no .NET support for it.
I published an article a while back called Waitable Timers in C#, in which I showed how to use this object from a C# program. Unfortunately, the site that published the article is no longer. However, you can download the code examples for the article from my site at http://www.mischel.com/pubs/waitabletimer.zip. You're free to use the code in any way you see fit.

Run C# code at specific time [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
C# Execute function at specific time
I want to run certain function at certain time.I tried Timer control.it's not working. My question is:How can I run a function every day at the 19:00 in C#?
Is there any way to check the time and have a Timer object?
Timer code:
int Interval(TimeSpan gorevZamani)
{
if ((gorevZamani - DateTime.Now.TimeOfDay).TotalMilliseconds > 0.0)
return (int)(gorevZamani - DateTime.Now.TimeOfDay).TotalMilliseconds;
else
return (int)((gorevZamani - DateTime.Now.TimeOfDay).Add(TimeSpan.FromDays(1)).TotalMilliseconds);
}
in the question set as possible duplicate: C# Execute function at specific time people suggest to use either Quartz.NET or windows Task Scheduler.
Both options could eventually serve the purpose but I believe, as I suggested already few times in similar previous questions, Windows Task Scheduler is better because you no code anything for it and let Windows do the scheduling for you and you focus only on the real business case of your application, which is what Windows cannot do for you, then rely on existing technologies to glue things together and don't have to debug or reinvent what has been done and is available for you anyway.
Use a scheduled task. A good way to do this is the at command, documented on MSDN here.

How to implement Excel Solver functionality in C#? [duplicate]

This question already has answers here:
Recommended library for linear programming in .Net? [closed]
(3 answers)
Closed 5 years ago.
I have an application in C#, I need to do some optimization calculations, like Excel Solver Add-in does, one option is certainly to write my own solver implementation, but I'm kind of short of time, so I'm looking into libraries that already exist that can help me with this.
I've been trying the Microsoft Solver Foundation, which seems pretty neat and cool, the problem is that it doesn't seem to work with the kind of calculations that I need to do.
At the end of this question I'm adding the information about the calculations I need to perform and optimize.
So basically my question is if any of you know of any other library that I can use for this purpose, or any tutorial that can help to do my own solver, or any idea that gives me a lead to solve this issue.
Thanks.
Additional Info:
This is the data I need to calculate:
I have 7 variables, lets call them var1, var2,...,var7
The constraints for these variables are:
All of them need to be 0 <= varn <= 0.5 (where n is the number of the variable)
The sum of all the variables should be equal to 1
The objective is to maximize the target formula, which in Excel looks like this:
(MMULT(TRANSPOSE(L26:L32),M14:M20)) / (SQRT(MMULT(MMULT(TRANSPOSE(L26:L32),M4:S10),L26:L32)))
The range that you see in this formula, L26:L32, is actually the range with the variables from above, var1, var2,..., varn.
M14:M20 and M4:S10 are ranges with data that I get from different sources, there are more likely decimal values.
As I said before, I was using Microsoft Solver Foundation, I modeled pretty much everything with it, I created functions that handle the operations of the target formula, but when I tried to solve the model it always fail, I think it is because of the complexity of the operations.
In any case, I just wanted to show these data so you can have an idea about the kind of calculations that I need to implement.
Here are some commercial .NET libraries containing different kind of multivariate optimization functions which can be a replacement for Excel's solver:
Extreme Optimization Numerical Libraries
Centerspace NMath.Net library
Visual Numerics library
If you find a good non-commercial / open source library for this purpose, let me know.

Categories