Click here to Skip to main content
15,895,011 members
Home / Discussions / C#
   

C#

 
GeneralRe: How to create a class instance inside a webservice class Pin
Guffa13-Feb-09 2:01
Guffa13-Feb-09 2:01 
GeneralRe: How to create a class instance inside a webservice class Pin
Nikki0513-Feb-09 17:29
Nikki0513-Feb-09 17:29 
QuestionDataGridview Problem Pin
Nitin K12-Feb-09 23:13
Nitin K12-Feb-09 23:13 
AnswerRe: DataGridview Problem Pin
Henry Minute13-Feb-09 5:53
Henry Minute13-Feb-09 5:53 
QuestionHow to Create share in Vista Pin
Joe Rozario12-Feb-09 22:25
Joe Rozario12-Feb-09 22:25 
QuestionPDF sharp Pin
abbd12-Feb-09 22:21
abbd12-Feb-09 22:21 
AnswerRe: PDF sharp Pin
DaveyM6912-Feb-09 23:36
professionalDaveyM6912-Feb-09 23:36 
QuestionGame Logic Implementation Pin
The Dust Puppy12-Feb-09 22:10
The Dust Puppy12-Feb-09 22:10 
Background:

Iam on writing a little game to get familiar with Managed DirectX.
The logic part (eg. Updating GameObjects World Position) has to be hardware independent,
which means that it will run in a separate thread with an own timer logic.

Implementation
//This will be runned in a separate thread
bool gameRunning = true;

while (gameRunning)
{
    using (new ExecutionTimer(100)) //Update the game every 100 ms
    {
       //Implement Game Logic here...
       //eg. Update World Position for Game Objects
    }
}


ExecutionTimer Code:

/// <summary>
/// This class will stop the current thread,
/// if the code within a using scope was executed faster than the
/// calculated end time provided in the constructor.
/// </summary>
public class ExecutionTimer : IDisposable
{
    /// <summary<
    /// The Calculated EndTime (DateTime.Now + ExecutionTime)
    /// </summary>
    private DateTime endTime;

    /// <summary>
    /// Returns a new instance of ExecutionTimer. This class should be used within a using scope.
    /// </summary>
    /// <param name="miliseconds">Time in Miliseconds</param>
    public ExecutionTimer(double milliseconds)
    {
        //Calculate the endtime
        this.endTime = DateTime.Now.AddMilliseconds(milliseconds);
    }

    #region IDisposable Member

    /// <summary>
    /// Dispose will be called at the end of the using scope.
    /// This Method will stop the thread until the calculated end time is reached.
    /// </summary>
    public void Dispose()
    {
        //Get the remaining time
        var remainingTime = endTime - DateTime.Now;

        //If there is time left -> Thread.Sleep
        if(remainingTime.TotalMilliseconds > 0)
            System.Threading.Thread.Sleep((int)remainingTime.TotalMilliseconds);
    }

    #endregion
}


What do you think about this implementation?
I know that it will create a new instance of ExecutionTimer in every step,
but on the other hand the syntax looks very smooth in my opinion.

Is there any alternative or best practice?

Greetings DustPuppy
AnswerRe: Game Logic Implementation Pin
Rob Philpott12-Feb-09 22:31
Rob Philpott12-Feb-09 22:31 
AnswerRe: Game Logic Implementation Pin
musefan12-Feb-09 23:31
musefan12-Feb-09 23:31 
GeneralRe: Game Logic Implementation Pin
The Dust Puppy13-Feb-09 1:06
The Dust Puppy13-Feb-09 1:06 
GeneralRe: Game Logic Implementation Pin
musefan13-Feb-09 1:22
musefan13-Feb-09 1:22 
GeneralRe: Game Logic Implementation Pin
The Dust Puppy13-Feb-09 1:31
The Dust Puppy13-Feb-09 1:31 
GeneralRe: Game Logic Implementation Pin
musefan13-Feb-09 1:59
musefan13-Feb-09 1:59 
QuestionZedGraph - PieChart Pin
Member 370147212-Feb-09 22:08
Member 370147212-Feb-09 22:08 
AnswerRe: ZedGraph - PieChart Pin
Ashfield13-Feb-09 1:10
Ashfield13-Feb-09 1:10 
GeneralRe: ZedGraph - PieChart Pin
Member 370147213-Feb-09 5:24
Member 370147213-Feb-09 5:24 
GeneralRe: ZedGraph - PieChart Pin
Ashfield14-Feb-09 23:57
Ashfield14-Feb-09 23:57 
GeneralRe: ZedGraph - PieChart Pin
Member 370147215-Feb-09 2:21
Member 370147215-Feb-09 2:21 
QuestionTab control- reason behind this ?? Pin
Hum Dum12-Feb-09 21:52
Hum Dum12-Feb-09 21:52 
AnswerRe: Tab control- reason behind this ?? Pin
Christian Graus12-Feb-09 22:07
protectorChristian Graus12-Feb-09 22:07 
GeneralRe: Tab control- reason behind this ?? Pin
Hum Dum12-Feb-09 23:22
Hum Dum12-Feb-09 23:22 
GeneralRe: Tab control- reason behind this ?? Pin
EliottA13-Feb-09 2:48
EliottA13-Feb-09 2:48 
Questionparse object array as string Pin
vijay.victory12-Feb-09 21:31
vijay.victory12-Feb-09 21:31 
AnswerRe: parse object array as string Pin
J4amieC12-Feb-09 21:49
J4amieC12-Feb-09 21:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.