当前位置: 首页 > news >正文

山东省建设节能协会网站/net的网站建设

山东省建设节能协会网站,net的网站建设,贵州网站建设设计公司,建个网站 网页空间多少当程序在进行一项耗时的工作时,加上一个UI显示当前的进度,这样可以避免让用户等到不耐烦,客户可能以为死机了,可能狂点鼠标,造成更差的效率问题。对于这个问题,解决方法有多种: 1,都…

当程序在进行一项耗时的工作时,加上一个UI显示当前的进度,这样可以避免让用户等到不耐烦,客户可能以为死机了,可能狂点鼠标,造成更差的效率问题。对于这个问题,解决方法有多种:

 

1,都在主线程中进行,将耗时任务分解成小块,主线程处理,同时更新GUI,MSDN中有个很好的例子 "A Single-Threaded Application with a Long-Running Calculation";

 

2,工作线程为非GUI线程,负责处理耗时工作,主线程更新GUI。这种方法只有一个GUI线程,对多线程编程,这个是通常的选择;

 

3,主线程,工作线程都是UI线程,主线程处理耗时工作,工作线程负责处理UI更新提示。某些情况下,不得不这么做。这里详细介绍一个实现。

 

线程包装类
namespace Program
{
public class WaitPromptArgs
{
public bool ShowInCenterOfScreen { get; set; }
public double Left { get; set; }
public double Top { get; set; }

public string Status { get; set; }
}

public class WaitPrompt
{
private bool working = false;

private readonly Window ParentWnd = null;
public WaitPrompt(Window owner)
{
ParentWnd
= owner;
}

private readonly ManualResetEvent BeginComplete = new ManualResetEvent(false);
public void BeginWait()
{
try
{
BeginComplete.Reset();
working
= true;

// calc startup position
WaitPromptArgs args = new WaitPromptArgs();
if (ParentWnd == null)
{
args.ShowInCenterOfScreen
= true;
}
else
{
// change cursor to hourglass
ParentWnd.Cursor = Cursors.Wait;

args.ShowInCenterOfScreen
= false;
args.Left
= ParentWnd.Left + 4;
args.Top
= ParentWnd.Top + 32;
}
args.Status
= string.Empty;

// start a new thread
t = new Thread(new ParameterizedThreadStart(OnThreadStart));
t.SetApartmentState(ApartmentState.STA);
t.IsBackground
= true;
t.Start(args);

BeginComplete.WaitOne();
// wait untill process start complete
}
catch (Exception e)
{
throw new Exception("wait prompt window", e);

}
}

private Thread t = null;
private Dispatcher theDispatcher = null;
private DispatcherFrame theFrame = null;
private WaitingWnd w = null;

private void OnThreadStart(object obj)
{
try
{
WaitPromptArgs args
= obj as WaitPromptArgs;
w
= new WaitingWnd();
if (args.ShowInCenterOfScreen)
{
w.WindowStartupLocation
= WindowStartupLocation.CenterScreen;
}
else
{
w.WindowStartupLocation
= WindowStartupLocation.Manual;
w.Left
= args.Left;
w.Top
= args.Top;
}
w.Show();

theFrame
= new DispatcherFrame();
theDispatcher
= Dispatcher.CurrentDispatcher;

BeginComplete.Set();
// start complete, notify other threads

theDispatcher.BeginInvoke(DispatcherPriority.Background,
new DispatcherOperationCallback(UpdateDescription), args.Status);
Dispatcher.PushFrame(theFrame);

w.Close();
w
= null;
}
catch (ThreadAbortException)
{
Thread.ResetAbort();
}
finally
{
CleanUp();
}
}

// you can NOT directly set this string, because the other thread holds the window
// so, have to delegate by the other thread dispatcher object
public void UpdateHint(string hint)
{
if (working)
{
// w.Description = hint;
theDispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(UpdateDescription), hint);
}
}
private object UpdateDescription(object des)
{
w.Description
= des as string;
return null;
}

public void EndWait()
{
try
{
theDispatcher.BeginInvoke(DispatcherPriority.Normal,
new DispatcherOperationCallback(ExitFrames), theFrame);
}
catch (Exception e)
{
throw new Exception("wait prompt window", e);
}
finally
{
if (ParentWnd != null)
ParentWnd.Cursor
= Cursors.Arrow;

working
= false;
}
}
private object ExitFrames(object f)
{
((DispatcherFrame)f).Continue
= false;
return null;
}
private void CleanUp()
{
try
{
if (w != null)
w.Close();
 
Debug.Assert(t != null && theDispatcher != null && theFrame != null);

t
= null;
theDispatcher
= null;
theFrame
= null;
w
= null;
}
catch (Exception e)
{
throw new Exception("wait prompt window", e);

}
}
}
}

 

线程中的窗口,负责显示状态
<Window
x:Class="Program.WaitingWnd"
xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x
="http://schemas.microsoft.com/winfx/2006/xaml"
Title
="Please wait ..." Height="100" Width="380" ResizeMode="NoResize"
Background
="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"
WindowStyle
="ToolWindow" WindowStartupLocation="Manual"
>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="32"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBlock x:Name="_description" HorizontalAlignment="Left" VerticalAlignment="Center" Text="TextBlock" TextWrapping="Wrap" Margin="10,0,0,0"/>
<ProgressBar HorizontalAlignment="Stretch" VerticalAlignment="Center" Width="Auto" Height="15" Grid.Row="1" Margin="10,0,10,0" IsIndeterminate="True"/>
</Grid>
</Window>

 

线程窗口Code Behind
namespace Program
{
/// <summary>
/// Interaction logic for WaitingWnd.xaml
/// </summary>
public partial class WaitingWnd : Window
{
public string Description { set { _description.Text = value; } }

public WaitingWnd()
{
InitializeComponent();
}
}
}

 

如何使用
public partial class Window1 : Window
{
readonly WaitPrompt wp = null;
public Window1()
{
InitializeComponent();

wp
= new WaitPrompt(this);
}


private void Begin_Click(object sender, RoutedEventArgs e)
{
wp.BeginWait();
wp.UpdateHint(
"hello world!");
}

private void Send_Click(object sender, RoutedEventArgs e)
{
wp.UpdateHint(theWords.Text);
}

private void End_Click(object sender, RoutedEventArgs e)
{
wp.EndWait();
}
}

 

转载于:https://www.cnblogs.com/yapzhang/archive/2010/12/23/1914600.html

http://www.lbrq.cn/news/1598311.html

相关文章:

  • 日本r影片网站做我的奴隶/晚上看b站
  • 折扣券网站怎么做/山东最新消息今天
  • 怎样做营销型网站推广/流量平台有哪些
  • 做网站如何防止被坑/哈尔滨seo优化软件
  • dreamweaver设计网页步骤/自己做seo网站推广
  • 北京网站建设 降龙网/2022最新小学生新闻
  • 北京网站开发建设/软文营销文章
  • 自己做网站的图片/中国十大搜索引擎排名最新
  • 中国容桂品牌网站建设/百度app内打开
  • 牡丹江做网站的公司/电商网站建设公司哪家好
  • 建筑工程网登/网站seo优化技巧
  • 服务器不是自己的做违法网站/青岛的seo服务公司
  • discuz修改网站底部/百度网站名称和网址
  • 广州宝盈网络科技有限公司网站/郑州网站优化排名
  • wordpress php 5.2/seo搜索优化是什么
  • 设计一个网站的步骤/沈阳优化网站公司
  • 给赌博网站做设计/18岁以上站长统计
  • 电子商务网站建设的基本要素/aso优化分析
  • 阿里云做网站要几天/销售网络平台推广
  • 买下云服务器怎么做网站/如何推广自己产品
  • 深圳市住房城乡建设局网站/做一个公司网站需要多少钱
  • 浙江网站建设推广公司哪家权威/网站免费高清素材软件
  • 企业做网站需要哪些人员/免费下载百度
  • 四川省住房与城乡建设 厅网站/谁有恶意点击软件
  • 宝塔怎么做两个网站/网站推广平台有哪些
  • 网站搭建吧/盘多多搜索引擎入口
  • 北京市住房和建设委员会网站/重庆seo技术博客
  • 专门做资产负债表结构分析的网站/交换友链平台
  • 建湖哪家专业做网站/模板建站
  • 莱阳网站定制/网站检测
  • Modbus tcp 批量写线圈状态
  • 智慧城市SaaS平台|市容环卫管理系统
  • HTML 媒体元素概述
  • 行为模式-模板方法模式
  • 【C++】模板深入进阶
  • History 模式 vs Hash 模式:Vue Router 技术决策因素详解