简体   繁体   English

Windows 窗体应用程序中的闪烁 UI

[英]Flickers UI in windows form application

I am code for auto upload images to server.我是自动上传图片到服务器的代码。 That images are come from folder and its name is show in my windows form UI if uploading complete then UI change in place of image name upload complete show.if new photos are come for upload again image name is show in the place of upload complete.该图像来自文件夹,如果上传完成,则其名称显示在我的 Windows 窗体 UI 中,然后 UI 更改代替图像名称上传完成显示。如果再次上传新照片,则图像名称显示在上传完成的位置。 but when upload complete show is flickers.但是当上传完成时显示是闪烁的。 How to solve it如何解决

public CustomNoteBox()
    {           
        InitializeComponent();
        // ImageNoteBox();
        _task = Task.Factory.StartNew(() => RecallMetho(_cts.Token), _cts.Token);
    }
 private void RecallMetho(CancellationToken token)
    {
        XML TemperoryModelTemp = CP.XMlFileData();
        imagePath = TemperoryModelTemp.UploadImagePath;
        var newImagePath = OldUploading.OldUploadPath;

        if (imagePath != newImagePath)
        {
            ContentType.StopRecursionFuntionImageUpload = false;
            CP.UploadImage();
            ImageNameShow.CurImage = "";
        }
        while (true)
        {
            if (token.IsCancellationRequested)
            {
                break;
            }
            //if (WinOpenShow == true)
            //{
            //    Task.Delay(5000);
            //    WinOpenShow = false;
            //}
            if(ImageNameShow.CurImage != "Complete")
            {
                ImageNoteBox();
            }
            else
            {
                Imagecomplte();
            }

        }

    }
    private void ImageNoteBox()
    {
        XML TemperoryModelTemp = CP.XMlFileData();
        try
        {
            if (WinOpenShow == true)
            {
                Thread.Sleep(2000); 
                WinOpenShow = false;
            }
            if (this.InvokeRequired)
                this.Invoke((MethodInvoker)delegate
            {

                //imageStatus.Text = ContentType.CurrentUploadedImage;
                headStatus.Text = " Uploading Photos";
                imagePath = TemperoryModelTemp.UploadImagePath;
                var newImagePath = OldUploading.OldUploadPath;
                panelComplete.Visible = false;
                pnlSelectPath.Text = imagePath;
                if (imagePath != newImagePath)
                {
                    ImageNameShow.CurImage = "";
                }
                //Changes Date 22Jan2020
                if (TemperoryModelTemp.IsCongratulationScreenOpen == "1")
                {
                    imageStatus.Text = " Status: Searching for Photos to upload from folder:";
                    imagePathlink.Visible = true;
                    imageName.Visible = false;
                    imagePathlink.Text = imagePath;
                }
                else
                {
                    if (ImageNameShow.CurImage == null)
                    {
                        imageStatus.Text = " Status: Searching for Photos to upload from folder:";
                        imagePathlink.Visible = true;
                        imageName.Visible = false;
                        imagePathlink.Text = imagePath;

                    }

                    else if (ImageNameShow.CurImage == "Complete")
                    {
                        panelComplete.Visible = true;
                        pnlSelectPath.Text = imagePath;
                        headStatus.Text = " Upload Complete";
                        UploadingStaus.Uplodingall = true;
                    }
                    else
                    {
                        if (imagePath == newImagePath)
                        {
                            if (ImageNameShow.CurImage != "")
                            {
                                if (pathscreen == false)
                                {
                                    imageStatus.Text = " Status: Searching for Photos to upload from folder:";
                                    imagePathlink.Visible = true;
                                    imageName.Visible = false;
                                    imagePathlink.Text = imagePath;
                                    pathscreen = true;

                                    //Task.Delay(10000).ContinueWith(t => RecallMetho(_cts.Token));

                                    //System.Threading.Timer timer = null;
                                    //timer = new System.Threading.Timer((obj) =>
                                    //{
                                    //    RecallMetho(_cts.Token);
                                    //    timer.Dispose();
                                    //},
                                    //            null, 10000, System.Threading.Timeout.Infinite);

                                    WinOpenShow = true;
                                }
                                else
                                {
                                    imagePathlink.Visible = false;
                                    imageName.Visible = true;
                                    imageStatus.Text = " Status: Uploading photos to frame:" + '"' + CP.XMlFileData().Frame + '"';
                                    imageName.Text = ImageNameShow.CurImage;
                                }

                            }
                            else
                            {
                                imageStatus.Text = " Status: Searching for Photos to upload from folder:";
                                imagePathlink.Visible = true;
                                imageName.Visible = false;
                                imagePathlink.Text = imagePath;

                            }
                        }
                        else
                        {
                            imageStatus.Text = " Status: Searching for Photos to upload from folder:";
                            imagePathlink.Visible = true;
                            imageName.Visible = false;
                            imagePathlink.Text = imagePath;
                            OldUploading.OldUploadPath = imagePath;
                        }
                    }
                }
            });
            else
            {
                newWarningBox1.Dispose();
                //newWarningBox1 = null;
            }
        }
        catch (Exception ex)
        {
        }

    }
    private void Imagecomplte()
    {
        XML TemperoryModelTemp = CP.XMlFileData();
        try
        {
            if (this.InvokeRequired)
                this.Invoke((MethodInvoker)delegate
            {
                //imageStatus.Text = ContentType.CurrentUploadedImage;
                headStatus.Text = " Uploading Photos";
                imagePath = TemperoryModelTemp.UploadImagePath;
                var newImagePath = OldUploading.OldUploadPath;
                panelComplete.Visible = false;
                pnlSelectPath.Text = imagePath;

                if (ImageNameShow.CurImage == "Complete")
                {
                    panelComplete.Visible = true;
                    pnlSelectPath.Text = imagePath;
                    headStatus.Text = " Upload Complete";
                    UploadingStaus.Uplodingall = true;
                }
            });
            else
            {
                newWarningBox1.Hide();
                //newWarningBox1 = null;
            }
        }
        catch (Exception ex)
        {
        }
    }      

You can try to decouple status message sending from UI updating.您可以尝试将状态消息发送与 UI 更新分离。 ConcurrentQueue is the most primitive class that can split pushing status messages and UI update. ConcurrentQueue是最原始的类,可以拆分推送状态消息和 UI 更新。 Worker code can Enqueue status messages to the queue and some UI friendly thread can TryPeek new messages and update the UI on the form.工人代码可以Enqueue状态消息队列和一些用户界面友好的线程可以TryPeek新的消息,并更新表单上的用户界面。

Another possibility is to use ReactiveX observer pattern implementation.另一种可能性是使用ReactiveX观察者模式实现。 The idea is the same: something produces the message and consumer processes them.想法是一样的:某物产生消息,消费者处理它们。

However turning UI control visibility on and off with high rate can cause flickering .但是,以高频率打开和关闭 UI 控件可见性可能会导致flickering So you can try to avoid showing and hiding controls and use some status line (or multiline) label\\readonly-textbox to put messages with ImageNameShow.CurImage content.所以你可以尽量避免显示和隐藏控件,并使用一些状态行(或多行)标签\\只读文本框来放置带有ImageNameShow.CurImage内容的消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM