Method and thread selection in .NET.

I have two threads in my application - the main UI thread and another thread triggered by the wm_WiiMoteChanged event handler (background thread). In the main thread, I do some video processing. I have a function called processFramebelow. I use this code to measure the processing time of each frame and therefore the frame rate per second.

If I comment on the line wm.WiiMoteChanged ...(see below), the frame rate is about 15-20 frames per second and looks at the video, it seems correct (there is a slight lag).

But when I uncomment the line, that is, add an event handler (which itself generates the stream), fps reaches 40-50, but this is definitely wrong - the video is actually more lag.

Can someone explain to me why this is happening? Thank.

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 also causes a higher lag - processing done before it is installed is quick!

+3
source share
3 answers

Because by adding an event handler, you respond to these events WiimoteChangedand run additional code.

? wm_WiimoteChanged()

UPDATE: System.Diagnostics.Stopwatch, DateTime.Now. , DateTime.Now .

+2

, , ?

FPS , ProcessFrame ? , .

, StopWatch; .

0

processFrame? , WiimoteChanged, processFrame .

In order for your FPS measurement to be accurate, you need to make sure that it processFrameis called exactly once for each frame. You should probably also measure the time between subsequent calls, and not measure the duration performOperationunless processFramecalled immediately when it returns.

0
source

Source: https://habr.com/ru/post/1733833/


All Articles