WCF中使用控件的委托,线程中的UI委托
生活随笔
收集整理的這篇文章主要介紹了
WCF中使用控件的委托,线程中的UI委托
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
UI界面:
<Window x:Class="InheritDemo.Window1"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="Window1" Height="300" Width="300"><Grid><StackPanel><Label MouseEnter="Label_MouseEnter" MouseLeave="Label_MouseLeave">test1</Label></StackPanel></Grid> </Window>后端代碼:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Threading;namespace InheritDemo {/// <summary>/// Window1.xaml 的交互邏輯/// </summary>public partial class Window1 : Window{private Thread myThread = null;//定義線程private delegate void MyDelegate(Object para);//定義委托public Window1(){InitializeComponent();}/// <summary>/// 鼠標移入啟動線程(繼續掛起線程)/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void Label_MouseEnter(object sender, MouseEventArgs e){if (myThread == null)//啟動線程 {myThread = new Thread(ThreadMethod);myThread.IsBackground = true;//后臺線程 myThread.Start(sender);}else { myThread.Resume(); }//繼續掛起線程 }/// <summary>/// 鼠標移出掛起線程/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void Label_MouseLeave(object sender, MouseEventArgs e){if (myThread != null) { myThread.Suspend(); }//掛起線程 }/// <summary>/// 線程事件調用委托/// </summary>/// <param name="para"></param>private void ThreadMethod(object para){MyDelegate myDelegate = new MyDelegate(DelegateMethod);while (true){this.Dispatcher.BeginInvoke(myDelegate, para);//調用委托Thread.Sleep(1000);//休眠1s }}/// <summary>/// 委托事件獲取當前時間/// </summary>/// <param name="para"></param>private void DelegateMethod(object para){Label lbl = (Label)para;lbl.Content = DateTime.Now.ToString();}} }?
轉載于:https://www.cnblogs.com/qq458978/p/4519449.html
總結
以上是生活随笔為你收集整理的WCF中使用控件的委托,线程中的UI委托的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 最新版富文本编辑器UEditor操作教程
- 下一篇: JAVA学习博客---2015.5