Note that there are some explanatory texts on larger screens.

plurals
  1. POCalling a function in another thread C#
    primarykey
    data
    text
    <p>I don't know how to put this but I'll try my best.</p> <p>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 <a href="http://directshownet.sourceforge.net/" rel="nofollow">http://directshownet.sourceforge.net/</a> and have used the DxSnap sample to take the photo.</p> <p>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.</p> <p>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.</p> <p>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.</p> <p>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?</p> <p>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.</p> <p>Here's some code:</p> <pre><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(); } /// &lt;summary&gt; /// Method of retrieving code tag details from reader /// &lt;/summary&gt; 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; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload