0

我有一个名为“DTImageList”的全局数据表和一个名为“uxImageGrid”的 XtraGrid。现在有一个名为“prcFillImagesVideosAndFiles”的方法,其中我们从数据库页面明智地获取图像数据,即一次说 500 行,我们使用 XtraTabControl 在网格顶部创建手动页面,具体取决于根据存在的数据总数搜索。假设我们得到 700 张图片,那么一次只加载 500 张图片,并且 2 个页面将被创建为“第 1 页”、“第 2 页”。但是在“prcFillImagesVideosAndFiles”方法中,我们没有获取实际图像,而只是获取它的名称、ID 等。在此之后,我创建了一个新线程并调用了一个方法运行程序,该运行程序又调用了一个名为“FillImages”的新方法 在其中我查看 DTImageList 并从后端一张一张地获取实际图像并使用该图像更新行,因为 XtraGrid 开始一张一张地显示图像。这个过程可以正常工作几分钟,即加载 20-25 个图像,然后它会给出“跨线程操作无效”错误。

// 我的 prcFillImagesVideosAndFiles 方法的代码是:

`if (DTImageList != null)
                {
                    DTImageList.Rows.Clear(); DTImageList.Columns.Clear(); DTImageList = null;
                }

                string sql = @"select " + top + @" IM.Image_ID,IM.extension,IM.Is_Uploaded,cast(0 as varbinary) 'ActualImage',IM.description 'Description',IM.ContentType,IM.DateTime_Uploaded,IM.FolderName, '' as FilePath 
                           from images as IM where IM.GCRecord is null and IM.Is_Uploaded=1 " + MainCriteria + @" " + Ob + @"";

                string sql1 = LayoutPaging(sql);
                DTImageList = new DataTable();
                DTImageList = FillDataTable(sql1);
                DataTable DTdeliv2 = new DataTable();
                DTdeliv2.Columns.Add("Image");
                DTdeliv2.Columns.Add("UniqueNumber");
                DTdeliv2.Columns["Image"].DataType = typeof(Image);
                DTdeliv2.Columns["UniqueNumber"].DataType = typeof(int);
                DTImageList.Merge(DTdeliv2, true, MissingSchemaAction.Add);

                uxImageGrid.DataSource = null;
                uxImageGrid.DataSource = DTImageList;

                RepositoryItemTextEdit riTextEdit = new RepositoryItemTextEdit();
                riTextEdit.Appearance.TextOptions.HAlignment = HorzAlignment.Center;
                layoutView1.Columns["Description"].AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;

                riTextEdit.Appearance.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(128)))));
                riTextEdit.Appearance.Options.UseBackColor = true;
                riTextEdit.NullText = "";
                uxImageGrid.RepositoryItems.Add(riTextEdit);
                layoutView1.Columns["Description"].ColumnEdit = riTextEdit;
                riTextEdit.Leave += new EventHandler(riTextEdit_Leave);
                riTextEdit.KeyPress += new KeyPressEventHandler(riTextEdit_KeyPress);

                RepositoryItemPictureEdit riPictureEdit = new RepositoryItemPictureEdit();

                riPictureEdit.SizeMode = PictureSizeMode.Zoom;
                riPictureEdit.ShowMenu = false;
                riPictureEdit.NullText = "        Loading Image";
                riPictureEdit.Appearance.Image = Pionero.RetailTherapy.Properties.Resources.mag;


                uxImageGrid.RepositoryItems.Add(riPictureEdit);
                layoutView1.Columns["Image"].ColumnEdit = riPictureEdit;

                riPictureEdit.MouseMove += new MouseEventHandler(riPictureEdit_MouseMove);
                riPictureEdit.MouseDown += new MouseEventHandler(riPictureEdit_MouseDown);

                layoutView1.Columns["Image"].Caption = "";

                int k = DTImageList.Rows.Count;
                if (k > 0)
                {
                    DevExpress.Data.Filtering.CriteriaOperator expr1 = new DevExpress.Data.Filtering.BinaryOperator("Is_Uploaded", true);
                    layoutView1.ActiveFilterCriteria = expr1;
                    if (pthread != null)
                    {
                        StopRunningThread();
                    }
                    if (pthread == null || pthread.IsAlive==false)
                    {
                        pthread = new Thread(new ThreadStart(runner));
                        pthread.IsBackground = true;
                        Is_ThreadNeededtoWork = true;
                        pthread.Start();
                    }
                    else
                    {

                        Is_ThreadNeededtoWork = true;
                        pthread.Start();
                    }

                }`

//runner方法是:

 void runner()
    {
        if (Is_ThreadNeededtoWork == false)
        {
            if (dtImages != null)
            {
                dtImages.Rows.Clear(); dtImages.Columns.Clear(); dtImages = null;
            }
            return;
        }
            if (Is_ThreadNeededtoWork == true)
            {
                FillImages();
            }
    }

// FillImages 方法的代码是:

try
        {
            if (DTImageList.Rows.Count <= 0) return;
            StringBuilder sbImagesNotLoaded = new StringBuilder();
            sbImagesNotLoaded.Append("Following images not loaded due to connection: ");
            ArrayList lstImage_IDs = new ArrayList();
            int NoOfAttempts = 0;

            //if (dtImages != null)
            //{
                for (int i = 0; i < DTImageList.Rows.Count; i++)
                {
                    NoOfAttempts = 0;
                V:
                    string Qry = @" Select Image_ID,image from images where Image_ID = " + DTImageList.Rows[i]["Image_ID"].ToString();
                    dtImages = FillDataTable(Qry);

                    if (dtImages != null && dtImages.Rows.Count > 0)
                    {
                        if (DTImageList.Rows[i]["image"] == DBNull.Value)
                        {
                           // Thread.Sleep(100);
                            byte[] barr = (byte[])dtImages.Rows[0]["image"];
                            Image img = Global.byteArrayToImage(barr);
                            DTImageList.Rows[i]["Image"] = img;
                            DTImageList.AcceptChanges();
                            uxImageGrid.RefreshDataSource();
                        }
                    }
                    else
                    {
                       // Thread.Sleep(100);
                        if (Convert.ToInt32(DTImageList.Rows[i]["Image_ID"]) > 0)
                            if (NoOfAttempts < 3)
                            {
                                NoOfAttempts = NoOfAttempts + 1;
                                goto V;
                            }
                            else
                            {
                                if (lstImage_IDs.Count > 0)
                                    sbImagesNotLoaded.Append("," + Convert.ToString(DTImageList.Rows[i]["Description"]));
                                else
                                    sbImagesNotLoaded.Append(Convert.ToString(DTImageList.Rows[i]["Description"]));

                                lstImage_IDs.Add(DTImageList.Rows[i]["Image_ID"]);

                            }
                    }

                }



            //}

            if (lstImage_IDs.Count > 0)
            {
                for (int i = 0; i < lstImage_IDs.Count; i++)
                {
                    DataRow drImage = DTImageList.Select("Image_ID=" + Convert.ToString(lstImage_IDs[i]) + "").FirstOrDefault();
                    if (drImage != null)
                        DTImageList.Rows.Remove(drImage);
                }
                DTImageList.AcceptChanges();

                XtraMessageBox.Show(sbImagesNotLoaded.ToString(), Global.Header, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            EnableDisablePanelControls(true);
            if (pthread != null)
            {
                Is_ThreadNeededtoWork = false;
                StopRunningThread();
            }

        }
        catch (ThreadAbortException abortException)
        {

        }
        catch (Exception emmp)
        {
            EnableDisablePanelControls(true);
            //pthread.Abort();

        }
        finally
        {
            //Global.StopProg();
        }

//StopRunningThread() 的代码是:

 void StopRunningThread()
    {
        if (dtImages != null)
        {
            dtImages.Rows.Clear(); dtImages.Columns.Clear(); dtImages = null;
        }

        Is_ThreadNeededtoWork = false;

    }

谢谢维姬

4

1 回答 1

0

您可以在将图像作为数据源传递给 uxImageGrid 之前尝试插入图像。将图像列添加到 DTImageLis。DTImageList.Columns.Add("图像", typeof(System.Drawing.Image));

根据 Name,Id 等将图像添加到数据表中,并将其绑定到 uxImageGrid uxImageGrid.Datasource=DTImageList;

于 2013-10-01T12:55:21.653 回答