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();//休眠1s
}
}
/// <summary>
/// 委托事件获取当前时间
/// </summary>
/// <param name="para"></param>
private void DelegateMethod(object para)
{
Label lbl = (Label)para;
lbl.Content = DateTime.Now.ToString();
} }
}
WCF中使用控件的委托,线程中的UI委托的更多相关文章
- android中ListView控件&&onItemClick事件中获取listView传递的数据
http://blog.csdn.net/aben_2005/article/details/6592205 本文转载自:android中ListView控件&&onItemClick ...
- VC线程中操作控件,引起程序卡死的问题。
[问题还原] 线程中操作控件,具体为控制一个按键的使能,使能后结束线程. 主程序中有一个死循环,等待线程结束. 然后,就没有然后了-- [解决方案] 在主程序死循环中,如果检测到界面消息,优先处理掉.
- 在WinForm中使用委托来在其他线程中改变控件的显示
假设winform中有两个控件: 1.ListView用来显示进度的文本提示,ID:listView_progressInfo 2.ProgressBar用来显示进度,ID:progressBar1 ...
- WPF中窗口控件的跨线程调用
在WinForm中,我们要跨线程访问窗口控件,只需要设置属性CheckForIllegalCrossThreadCalls = false;即可. 在WPF中要麻烦一下,同样的不允许跨线程访问,因为没 ...
- [转]MFC子线程中更新控件内容的两种办法
一.概述 每个系统中都有线程(至少都有一个主线程),而线程最重要的作用就是并行处理,提高软件的并发率.针对界面来说,还能提高界面的响应能力.一般的,为了应用的稳定性,在数据处理等耗时操作会单独在一个线 ...
- C# 跨线程调用控件的4中方法
原文:C# 跨线程调用控件 在C# 的应用程序开发中, 我们经常要把UI线程和工作线程分开,防止界面停止响应. 同时我们又需要在工作线程中更新UI界面上的控件, 下面介绍几种常用的方法 阅读目录 线 ...
- Android线程中设置控件
在Android中经常出现多线程中设置控件的值报错的情况,今天教大家封装一个简单的类避免这样的问题,同样也调用实现也非常的方便. 自定义类: /** * Created by wade on 2016 ...
- 用委托在listbox中异步显示信息,解决线程间操作无效,从不是创建控件的线程访问它
//创建一个委托,是为访问listbox控件服务的. public delegate void UpdateTxt(string msg); //定义一个委托变量 public UpdateTxt u ...
- Android 中常见控件的介绍和使用
1 TextView文本框 1.1 TextView类的结构 TextView 是用于显示字符串的组件,对于用户来说就是屏幕中一块用于显示文本的区域.TextView类的层次关系如下: java.la ...
随机推荐
- (转)C# DES
本文原地址:http://blog.csdn.net/zhoufoxcn/article/details/1497095 作者:周公 , inputByteArray.Length); ...
- 分享一个基于EF5.0封装的BaseDAL
public class BaseDAL<T> where T : class,new() { protected DbContext dbContext = DbContextFacto ...
- cookie机制和session机制的原理和区别[转]
一.cookie机制和session机制的区别 具体来说cookie机制采用的是在客户端保持状态的方案,而session机制采用的是在服务器端保持状态的方案. 同时我们也看到,由于在服务器端保持状态的 ...
- Orchard官方文档翻译(十一) 使用Tags组织文本
原文地址:http://docs.orchardproject.net/Documentation/Organizing-content-with-tags 想要查看文档目录请用力点击这里 最近想要学 ...
- mysql中data_format用法
date_format(date,format)可以把日期转换为制定的格式: mysql> select date_format('2008-08-08 22:23:00', '%W %M %Y ...
- Compilation failed: this version of PCRE is not compiled with PCRE_UTF8 support at offset 0
在安装pcre-8.13.tar.gz时候出了错,说是缺少libpcre.so.0 下面是解决方法.真不容易哦,一个问题来没解决,新问题就出来了.一环扣一环,会搞死去.. errorgrep: err ...
- 分层服务提供者(LSP)
分层服务提供者(LSP)(1) 开发过滤数据包的LSP程序可以定义过滤规则,恩,先看看LSP本身是DLL,可以将它安装至Winsock目录,创建套接字的应用程序不必知道此LSP的任何信息就能调用它 1 ...
- HTML转义字符集合
readme:这次可以不readme了,因为这个是我copy过来的~ ISO Latin-1字符集: — 制表符Horizontal tab — 换行Line feed — 回车Carriage ...
- Exceeded maximum number of retries. Exceeded max scheduling attempts 3 for instance
Exceeded maximum number of retries. Exceeded max scheduling attempts 3 for instance
- 【MySQL】MySQL事务回滚脚本
MySQL自己的 mysqlbinlog | mysql 回滚不好用,自己写个简单脚本试试: 想法是用mysqlbinlog把需要回滚的事务区域从mysql-bin.file中找到,然后通过脚本再插入 ...