Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

multithreading - Calling a function in another thread C#

I don't know how to put this but I'll try my best.

I have a Windows form application which uses a webcam to take a photo of a user which works fine, I'm using the directshownet library found here http://directshownet.sourceforge.net/ and have used the DxSnap sample to take the photo.

The next part of the application uses an RFID tag reader that once a tag is scanned, it calls the take photo method. And this is where the problem is coming in, because the RFID listen method is run in a separate thread to the GUI thread, because it is an infinite while loop.

The class that is used to take the picture takes an instance of a picturebox control as a parameter for it's constructor, which is created in the main thread, and i think this is where the problem is.

Each part of the application works fine separately but when i try to call the method to take the photo from the listener thread all hell breaks loose and the app crashes.

Does anybody know how I could call a method of the object that is initialized in the main thread (that takes the photo) from the listener thread?

I've tried delegate and invoke, but can't figure it out as i don't want to manipulate the control directly, but rather let the object do it in the main thread.

Here's some code:

    private Capture cam;
    private int portIndex = -1;
    private ArrayList AlreadyOpenPortList = new ArrayList();
    private byte readerAddr = 0;
    private Thread listenThread;
    IntPtr m_ip = IntPtr.Zero;

    public podiumForm()
    {
        InitializeComponent();

        // scanner and camera startup
        startCam();
        openComs();
        openRF();
        startListening();
    }


 private void startListening()
    {
        listenThread = new Thread(new ThreadStart(this.Listen));
        listenThread.Start();
    }

    /// <summary>
    /// Method of retrieving code tag details from reader
    /// </summary>
    private void Listen()        
    {
        int fCmdRet = 0x30;
        byte state = 1;
        byte AFI = 00;
        byte[] DSFIDAndUID = new byte[9];
        byte cardNumber = 0;
        string strDSFIDAndUID = "";
        byte outputSet;

        if (!GetCurrentUsePort())
        {
            MessageBox.Show("Open ComPort, Please");
            return;
        }


        while (true)
        {
            fCmdRet = StaticClassReaderA.Inventory(ref readerAddr, ref state, ref AFI, DSFIDAndUID, ref cardNumber, portIndex);
            if (fCmdRet == 0)
            {
                outputSet = 0;
                fCmdRet = StaticClassReaderA.SetGeneralOutput(ref readerAddr, ref outputSet, portIndex);
                strDSFIDAndUID = ByteArrayToHexString(DSFIDAndUID).Replace(" ", "");
                outputSet = 3;
                fCmdRet = StaticClassReaderA.SetGeneralOutput(ref readerAddr, ref outputSet, portIndex);
                SavePic(strDSFIDAndUID.Substring(2, 16));
                //MessageBox.Show(strDSFIDAndUID.Substring(2, 16));

                //ShutDown();
            }
        }
    }

private void SavePic(string text)
    {
        Cursor.Current = Cursors.WaitCursor;

        // Release any previous buffer
        if (m_ip != IntPtr.Zero)
        {
            Marshal.FreeCoTaskMem(m_ip);
            m_ip = IntPtr.Zero;
        }

        // here's where it crashes
        // capture image
        m_ip = cam.Click();
        Bitmap b = new Bitmap(cam.Width, cam.Height, cam.Stride, PixelFormat.Format24bppRgb, m_ip);
        cam.Dispose();

        // If the image is upsidedown
        b.RotateFlip(RotateFlipType.RotateNoneFlipY);
        pbPic.Image = b;

        Cursor.Current = Cursors.Default;
        MessageBox.Show("Here " + text);
    }


private void startCam()
    {
        const int VIDEODEVICE = 0; // zero based index of video capture device to use
        const int VIDEOWIDTH = 640; // Depends on video device caps
        const int VIDEOHEIGHT = 480; // Depends on video device caps
        const int VIDEOBITSPERPIXEL = 24; // BitsPerPixel values determined by device

        cam = new Capture(VIDEODEVICE, VIDEOWIDTH, VIDEOHEIGHT, VIDEOBITSPERPIXEL, pbPic);
    }


// method in capture class
 public IntPtr Click()
    {
        int hr;

        // get ready to wait for new image
        m_PictureReady.Reset();
        m_ipBuffer = Marshal.AllocCoTaskMem(Math.Abs(m_stride) * m_videoHeight);

        try
        {
            m_WantOne = true;

            // If we are using a still pin, ask for a picture
            if (m_VidControl != null)
            {
                // CRASHES HERE with : System.InvalidCastException was unhandled
                // Tell the camera to send an image
                hr = m_VidControl.SetMode(m_pinStill, VideoControlFlags.Trigger);
                DsError.ThrowExceptionForHR(hr);
            }

            // Start waiting
            if (!m_PictureReady.WaitOne(9000, false))
            {
                throw new Exception("Timeout waiting to get picture");
            }
        }
        catch
        {
            Marshal.FreeCoTaskMem(m_ipBuffer);
            m_ipBuffer = IntPtr.Zero;
            throw;
        }

        // Got one
        return m_ipBuffer;
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Take a look at the SynchronizationContext class; it allows you to dispatch work back on the main (UI) thread in a manner that is agnostic of how that mechanism actually works (and therefore works with WinForms and WPF). Therefore you don't need to have a reference to a Control to call Invoke on.

It is what BackgroundWorker uses behind the scenes (you may also be able to use BackgroundWorker, depending on what you're trying to do, which is slightly easier to work with).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...