none
Task, await with webcam RRS feed

  • Question

  • Hi, i'm trying to write simple app with webcam support. I'm using this library ( i know that was mistake but now i would have change some much :( ) http://easywebcam.codeplex.com/ .

    When I'm connected to camera my UI respond very slow, when i change required fps from camera it's a little better ( i think my UI gets as much as the camera give).

    I tried to write function that gets frames in thread so my ui don't slow down.

      async void webcam_ImageCapturedAsync(object source, WebcamEventArgs e)
            {
                _FrameImage.Source =await Helper.LoadBitmapAsync((System.Drawing.Bitmap)e.WebCamImage);
            }
    
            void webcam_ImageCaptured(object source, WebcamEventArgs e)
            {
                _FrameImage.Source = Helper.LoadBitmap((System.Drawing.Bitmap)e.WebCamImage);
            }
     

    And

     public async static Task<BitmapSource> LoadBitmapAsync(System.Drawing.Bitmap source) 
            {
                return await Task<BitmapSource>.Run(() =>
                    {
                        ip = source.GetHbitmap();
    
                        bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, System.Windows.Int32Rect.Empty,
    
                        System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
    
                        DeleteObject(ip);
                        
                        return bs;
                    });
    
            }
            public static BitmapSource LoadBitmap(System.Drawing.Bitmap source)
            {
                
                    ip = source.GetHbitmap();
    
                    bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, System.Windows.Int32Rect.Empty,
    
                    System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());
    
                    DeleteObject(ip);
    
                    return bs;
    
            }

    Async methods are written my me, and the other ones are from library.

    And when i start program with async methods my imagebox stay grey, in debuger all parameters have error. So the question is why? :> 

    Sunday, April 21, 2013 12:30 PM

All replies

  • You say the UI is very slowly running....

    And what method are you using?

    Please use the 1st defined method instead, this is an Async method, which won't crash your UI;)


    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats

    Monday, April 22, 2013 2:39 AM
  • but within async method i have error somewhere because it never returns result
    Monday, April 22, 2013 10:15 AM
  • but within async method i have error somewhere because it never returns result

    Have u debugged the program in async?

    What's happening?


    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats

    Monday, April 22, 2013 1:01 PM
  • it never returns result, debuger goes to line with

    " return await Task<BitmapSource>.Run(() =>"
    and then jumps to
    "_FrameImage.Source =await Helper.LoadBitmapAsync((System.Drawing.Bitmap)e.WebCamImage);"

    and so on

    i tried

    async void webcam_ImageCaptured(object source, WebcamEventArgs e)
    {
        BitmapSource bs = await Task.Run<BitmapSource>(() =>
            {
                return Helper.LoadBitmap((System.Drawing.Bitmap)e.WebCamImage);
            });
        _FrameImage.Source = bs;
    }
    but this in debuger retun InvalidOperationException: The calling thread cannot access this object because a different thread owns it. and i've nothing :(
     

    Monday, April 22, 2013 1:15 PM