[英]Flickers UI in windows form application
我是自動上傳圖片到服務器的代碼。 該圖像來自文件夾,如果上傳完成,則其名稱顯示在我的 Windows 窗體 UI 中,然后 UI 更改代替圖像名稱上傳完成顯示。如果再次上傳新照片,則圖像名稱顯示在上傳完成的位置。 但是當上傳完成時顯示是閃爍的。 如何解決
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)
{
}
}
您可以嘗試將狀態消息發送與 UI 更新分離。 ConcurrentQueue是最原始的類,可以拆分推送狀態消息和 UI 更新。 工人代碼可以Enqueue
狀態消息隊列和一些用戶界面友好的線程可以TryPeek
新的消息,並更新表單上的用戶界面。
另一種可能性是使用ReactiveX觀察者模式實現。 想法是一樣的:某物產生消息,消費者處理它們。
但是,以高頻率打開和關閉 UI 控件可見性可能會導致flickering
。 所以你可以盡量避免顯示和隱藏控件,並使用一些狀態行(或多行)標簽\\只讀文本框來放置帶有ImageNameShow.CurImage
內容的消息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.