English 中文(简体)
Timing a method and threads in .NET
原标题:

I have two threads in my app - the main UI thread and another thread initiated by the wm_WiiMoteChanged event handler (a background thread). In the main thread I do some video processing. I have a function called processFrame shown below. I use that code to measure the time to process each frame and thus the frames per second rate.

If I comment out the wm.WiiMoteChanged ... line (see below), the frame rates are about 15-20 fps and looking at the video, this seems right (there is a small lag).

But when I uncomment the line, i.e. add the event handler (which will spawn a thread on its own), the fps goes up to 40-50, but this is definitely wrong - the video is in fact more laggy.

Can someone explain to me why this happens? Thanks.

private void Main_Load(object sender, EventArgs e)
{
    try
    {
        wm.Connect();
        //wm.WiimoteChanged += wm_WiimoteChanged; 

        wm.SetReportType(InputReport.IRAccel, true);
        wm.SetLEDs(false, false, false, true);
    }
    catch (Exception x)
    {
        MessageBox.Show("Exception: " + x.Message);
        this.Close();
    }
}

More code:

private void processFrame(object sender, EventArgs e)
{
    DateTime curr = DateTime.Now;
    performOperation();
    TimeSpan currTime = DateTime.Now - curr;
    lblFPS.Text = (1000 / currTime.Milliseconds).ToString() + " fps";
}

EDIT

An interesting find, only when this line is present in wm_WiimoteChanged, does this happen.

ibxOutput.Image = new Image<Bgr, Byte>(_irViewAreaBitmap);

Sidenote: this line is the cause of the higher lag too - the processing done before setting this is actually fast!

问题回答

Because by adding a event handler, you are responding to those WiimoteChanged events and running extra code.

Does the handler contain locking? Suggest you post the code for wm_WiimoteChanged()

UPDATE: Suggest you use System.Diagnostics.Stopwatch rather than DateTime.Now It is likely that DateTime.Now is not accurate enough.

Okay, I m being thick here, but I don t understand the frames per second calculation?

Should your FPS not actually be the number of times that ProcessFrame is called in a second? If you measure that you might get a more accurate result.

Also, for measuring time, you re better off using StopWatch; it s built for this purpose.

Does exactly one frame get rendered to the screen for each invocation of the processFrame method? I suspect the rendering is happening elsewhere and is blocked by the code in the WiimoteChanged handler, giving your processFrame method more processor time.

For your FPS measurement to be accurate you need to ensure that processFrame is called exactly once per frame. You should probably measure the time between subsequent invocations too rather than measuring the duration of performOperation, unless processFrame will be called immediately again when it returns.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签