private void DisplayPicture()
        {
            TimerCallback timerDelegate = new TimerCallback(PaintBegin);
            System.Threading.Timer timer = new System.Threading.Timer(timerDelegate, null, 1000, 10000);
         
            
        }
public void PaintBegin(Object param)
        {
            if (count >= 4)
                count = 0;
            szFileName = picture[count];            //sync methods
            //this.Invoke(new InvailateDelegate(InvalidateRect), null);               //async methods
            this.BeginInvoke(new InvailateDelegate(InvalidateRect), null);
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.PaintPicture);
            count++;
        }        public delegate void InvailateDelegate();
        private void InvalidateRect()
        {
            Rectangle dstRect = new Rectangle(810, 0, 224, 300);
            this.Invalidate(dstRect);
        }private void PaintPicture(object sender, PaintEventArgs e)
        {
            // Load the image with alpha data through Imaging.            IImagingFactory factory = (IImagingFactory)Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid("327ABDA8-072B-11D3-9D7B-0000F81EF32E")));
            IImage imagingImage;
            if (szFileName == null)
                return;
            uint result = factory.CreateImageFromFile(szFileName, out imagingImage);            Rectangle dstRect = new Rectangle(0, 0, 224, 300);
            backBuffer = new Bitmap(224, 300);
            Graphics gxBuffer = Graphics.FromImage(backBuffer);
            IntPtr hdcDest = gxBuffer.GetHdc();            this.BeginInvoke(new AsyncPaintCallback(AsyncPaint), new Object[] { hdcDest, imagingImage });
            e.Graphics.DrawImage(backBuffer, 800, 0);
            backBuffer.Dispose();        }        delegate void AsyncPaintCallback(IntPtr hdcDest,IImage imagingImage);
        private void AsyncPaint(IntPtr hdcDest,  IImage imagingImage)
        {
            Rectangle dstRect = new Rectangle(0, 0, 224, 300);
            imagingImage.Draw(hdcDest, ref dstRect, IntPtr.Zero);
        }IImage 为COM接口,当我用异步方式BeginInvoke时出错。
请问这是为什么?

解决方案 »

  1.   


    delegate void AsyncPaintCallback(IntPtr hdcDest,IImage imagingImage);
    private void AsyncPaint(IntPtr hdcDest,  IImage imagingImage)
    {
            Rectangle dstRect = new Rectangle(0, 0, 224, 300);        //因为这里比较慢,所以用异步的方式实现
            imagingImage.Draw(hdcDest, ref dstRect, IntPtr.Zero);
    }
      

  2.   

    这种与是否COM没有关系吧确认下接口是否支持多线程
      

  3.   

    如何在C#里指定COM多线程
    比方我在C++里可以这样做:CoInitializeEx(NULL, COINIT_MULTITHREADED);现在我在C#中导入:[ComImport, Guid("327ABDA9-072B-11D3-9D7B-0000F81EF32E"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    [ComVisible(true)]我应该如何去指定它用多线程?