开发者

WPF Threading Grab Bag

开发者 https://www.devze.com 2023-03-11 04:41 出处:网络
I have developed an interesting WPF control that is currently slowing down my entire application :)On my custom control, I have an image control that I need to update everytime a backend event occurs.

I have developed an interesting WPF control that is currently slowing down my entire application :) On my custom control, I have an image control that I need to update everytime a backend event occurs. This backend event is firing twice a second (very fast). When the event fires I need to pull a Bitmap object out of a 3rd party control, convert to a BitmapSource object and then bind it to my Image control. Each time my event is fired, I am queuing a new work item in the ThreadPool. The item will then fetch the Bitmap and do the conversion in a background worker object. This is done everytime the event fires. I am using the dispatcher to update my image control source with BeginInvoke but I still get an unresponsive app. Please let me know what I can do to make this process better performing and help to make my app more responsive:

Here is the code in my event:

void inSight_ResultsChanged(object sender, EventArgs e)
        {

            ThreadPool.QueueUserWorkItem(new WaitCallback(ProcessEvent), ((InSightViewer)this.DataContext).CvsDisplay);
        }

Here is the code from delegate:

void ProcessEvent(object display)
        {
            BackgroundWorker bw = new BackgroundWorker();
            bw.DoWork += new DoWorkEventHandler(bw_DoWork);
            bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
            bw.RunWorkerAsync(display);

        }

Here is the code in my background worker DoWork event:

void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            3rdPartyControl displayControl = new 3rdPartyControl();

            displayControl.ImageHost = (ImgHost)e.Argument;


            Bitmap b = displayControl.GetBitmap();

            var mBitmap = b.GetHbitmap();
            BitmapSource bs;

            try
            {
                bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                           mBitmap,
                           IntPtr.Zero,
                           Int32Rect.Empty,
                           BitmapSizeOptions.FromEmptyOptions());

                bs.Freeze();
            }
            catch (System.Exception ex) { 开发者_运维问答throw ex; }
            finally
            {
                DeleteObject(mBitmap);
            }

            e.Result = bs;
        }

Here is the code in RunWorkerCompleted event:

void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.ApplicationIdle, (ThreadStart)delegate()
        {
            this.imgSource.Source = (BitmapSource)e.Result;
        });
}


void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
        Application.Current.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.ApplicationIdle, (ThreadStart)delegate()
        {
            this.imgSource.Source = (BitmapSource)e.Result;
        });
}

System.Windows.Threading.DispatcherPriority.ApplicationIdle means Operations are processed when the application is idle.

What if the application is always busy and never idle? The requests will be queued and the app slows down.

However I haven't tested this because I don't have the source code of your app.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号