Getting a string from ISampleGrabber and updating a textbox in first form

3

I would like to update my UI through another class, I have tried doing this by creating a form1 object and using a method to update the textbox. This leads to an error which informs me that my device is not properly running.

So basically how do I update a textbox on my Form1 using my samplegrabber.cs class? This class is called constantly, however I only need to use the string .

The ISampleGrabber class calls the SampleCB method which consists of:

public int SampleCB(double sampletime, IMediaSample sample)
    {
        if (sample == null)
        {
            return -1;
        }
        try
        {
            int length = sample.GetActualDataLength();
            IntPtr buffer;          
            if (sample.GetPointer(out buffer) == 0 && length > 0)
            {
                Bitmap bitmapOfFrame = new Bitmap(width, height, stride, PixelFormat.Format24bppRgb, buffer);
            }                
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        Marshal.ReleaseComObject(sample);
        return 0;
    }

The form1 object is created within the if statement, the erroroccurs even if I create the object (Even without the f1.updateTextBox(id);) line.

The `updateTextBox1' is created in Form1:

    public void updateTextBox1(string id)
    {
        textBox1.Text = id;
    }

The error I receive is as follows:

COMException(0x8007001F)A device attached to the system is not functioning properly.

c#
winforms
directshow
invoke
directshow.net
asked on Stack Overflow Nov 24, 2013 by legohead • edited May 13, 2014 by Max

1 Answer

2

SampleCB is called on a side thread. You should not do any UI related operations in this callback, instead you might want to store the values in member variables and indicate that you need to continue on the UI thread, e.g. by posting yourself a message and then handling it on the correct thread.

answered on Stack Overflow Nov 26, 2013 by Roman R. • edited May 23, 2017 by Community

User contributions licensed under CC BY-SA 3.0