Click here to Skip to main content
15,942,292 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good Day

I have written an application that enables users to capture a photo of themselves from a webcam stream. I used the Task class from "System.Threading.Tasks" to perform image editing functions while the stream is active, to prevent the application from hanging.

The application works well after I have tested it on my machine. I have also tested it on another machine and it worked as well,but , any other machine I test it on results in the following exception being thrown : "A framework exception was generated: Specified cast is not valid".

In my application I do not perform any casts or conversions, to make sure I ran the code on the main thread. As expected the application ran much slower but then I could see it working on my machine as well as all the other machines I have tested. It is safe to assume that there is something wrong with the way I handle the Task class.

here is a snippet of my code as reference :

C#
  private  Task m_objTask;

  private  bool m_bisStreamEnabled

  private void RunStream()
     {
         while (this.m_bisStreamEnabled)
         {
             try
             {
                 var hImage = this.m_objCapture.Click();

                 // create bitmap object
                 var objBitmapCapturedImage = new Bitmap(this.m_objCapture.Width,
                                                         this.m_objCapture.Height,
                                                         this.m_objCapture.Stride,
                                                         PixelFormat.Format32bppRgb,
                                                         hImage);
                 // flip clone
                 objBitmapCapturedImage.RotateFlip(RotateFlipType.RotateNoneFlipY);
                 objBitmapCapturedImage.RotateFlip(RotateFlipType.RotateNoneFlipX);

                 // edit captured image
                 this.HandleFrameUpdated(objBitmapCapturedImage);

                 // manage memory
                 objBitmapCapturedImage.Dispose();
                 Marshal.FreeCoTaskMem(hImage);
             }

             catch (Exception ex)
             {
                 SCS_Exception.ToSoftconException(ex, Environment.StackTrace);
             }
         }
     }

void button_click(object sender, EventArgs e)
{
    this.m_bisStreamEnabled = true;

             this.m_objTask = new Task(RunStream);

             this.m_objTask.Start();
}
Posted
Updated 17-Sep-13 22:52pm
v2
Comments
Thomas Barbare 18-Sep-13 4:35am    
Have you the same framework installed on all computers ? Maybe function from framework that you use is doing a cast.
Khani Ndhambi 18-Sep-13 4:42am    
I have dot net framework 4.5 installed on my machine while the other machines have dot net framework 4.0 installed. Could this be the cause of the problem ?
V.Lorz 18-Sep-13 4:59am    
In some multi-threading related classes when compiling for .Net 3.5 and running on .Net 3.0..... yes, that happened. Try to upgrade the framework and let us know (please).
Khani Ndhambi 18-Sep-13 7:00am    
The application is targeted for dotnet framework 4.0 and is therefore compiled in a framework 4.0 environment. I uninstalled dot net framework 4.5 and then compiled it again. It runs on my machine but still does not work properly on other machines.
V.Lorz 18-Sep-13 7:08am    
You could try to gather stack trace information to have an idea where the exception is thrown.

For debugging and verifying applications that are running as part of control and automation systems in some industrial machines I use the VisualStudio Remote Debugger. It is a little bit tricky to get it working but it is certainly a very usefull tool for remote-debugging such applications.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900