c# - Why can't I use CapturePhotoToStreamAsync from a System.Threading.Timer call back in WPF? -


i have code in winforms application i'm using winrt mediacapture object take picture device's camera. code executed in system.threading.timer call back. i'm trying move code wpf i'm running problems. when try execute mediacapture.capturephototostreamasync timer's callback in wpf, receive following exception:

a first chance exception of type 'system.exception' occurred in myassembly.dll

additional information: request invalid in current state.

started

i can create loop execute method 1000 times , work, however, bombs if method called within timer callback.

so clarify, code work in winforms in timer callback code bomb in wpf in timer callback code work in wpf if it's not executed in timer callback..

i suspect issue has thread it's executing on, however, i've tried use dispatcher no avail.

here method called:

public async task capturephoto(int width, int height)     {         thread.sleep(100);         var jpgproperties = imageencodingproperties.createjpeg();         jpgproperties.width = (uint)width;         jpgproperties.height = (uint)height;          using (var randomaccessstream = new inmemoryrandomaccessstream())         {             if (_mediacapturemanager != null)             {                 await _mediacapturemanager.capturephototostreamasync(jpgproperties, randomaccessstream);                 randomaccessstream.seek(0);                 using (var iostream = randomaccessstream.asstream())                 {                     var bitmapimage = new bitmapimage();                     bitmapimage.begininit();                     bitmapimage.streamsource = iostream;                     bitmapimage.cacheoption = bitmapcacheoption.onload;                     bitmapimage.endinit();                      // copy byte array                     int stride = bitmapimage.pixelwidth * 4;                     byte[] buffer = new byte[stride * bitmapimage.pixelheight];                     bitmapimage.copypixels(buffer, stride, 0);                      // create bitmap                     system.drawing.bitmap bitmap =                         new system.drawing.bitmap(                             bitmapimage.pixelwidth,                             bitmapimage.pixelheight,                             system.drawing.imaging.pixelformat.format32bppargb);                      // lock bitmap data                     system.drawing.imaging.bitmapdata bitmapdata =                         bitmap.lockbits(                             new system.drawing.rectangle(0, 0, bitmap.width, bitmap.height),                             system.drawing.imaging.imagelockmode.writeonly,                             bitmap.pixelformat);                      // copy byte array bitmap data                     system.runtime.interopservices.marshal.copy(                         buffer, 0, bitmapdata.scan0, buffer.length);                      // unlock                     bitmap.unlockbits(bitmapdata);                      imagebrush backgroundbrush = new imagebrush();                     backgroundbrush.imagesource = bitmapimage;                      system.windows.application.current.dispatcher.invoke((action)(() =>                     {                         previewpanel.background = backgroundbrush;                     }));                 }             }         }     } 

i have small wpf project can share via onedrive if help. don't want include url default because i'm not sure if allowed on so.

why can't use capturephototostreamasync system.threading.timer call in wpf?

you'll need make sure capturephototostreamasync executed on ui thread.

you should use dispatchertimer rather threading.timer class.


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -