如何修改图片属性使用 CancellationToken 属性

CancellationToken Structure (System.Threading)
CancellationToken Structure
July 28, 2014Propagates notification that operations should be canceled.
Namespace:
mscorlib (in mscorlib.dll)
public struct CancellationToken
The CancellationToken type exposes the following members.
NameDescriptionInitializes the CancellationToken.
NameDescriptionGets whether this token is capable of being in the canceled state.Gets whether cancellation has been requested for this token.Returns an empty CancellationToken value.Gets a
that is signaled when the token is canceled.
NameDescriptionDetermines whether the current CancellationToken instance is equal to the specified token.Determines whether the current CancellationToken instance is equal to the specified . (Overrides .)Allows an object to try to free resources and perform other cleanup operations before the
is reclaimed by garbage collection. (Inherited from .)Serves as a hash function for a CancellationToken. (Overrides .)Gets the
of the current instance. (Inherited from .)Creates a shallow copy of the current . (Inherited from .)Registers a delegate that will be called when this CancellationToken is canceled.Registers a delegate that will be called when this CancellationToken is canceled.Registers a delegate that will be called when this CancellationToken is canceled.Registers a delegate that will be called when this CancellationToken is canceled.Throws a
if this token has had cancellation requested.Returns the fully qualified type name of this instance. (Inherited from .)
NameDescriptionDetermines whether two CancellationToken instances are equal.Determines whether two CancellationToken instances are not equal.For more information and code examples see .Supported in: 8.1, 8.0All public and protected members of CancellationToken are thread-safe and may be used concurrently from multiple threads, with the exception of Dispose, which must only be used when all other operations on the CancellationToken have completed.
Was this page helpful?
Your feedback about this content is important.Let us know what you think.
Additional feedback?
1500 characters remaining
Thank you!
We appreciate your feedback.
Related developer sites
Other Windows sites
Tool and product downloads
Design template downloads
Essentials
Hello from Seattle.帮助企业打造成功软件!
热门搜索:
WPF 4.5探秘之三 Dispatcher的新方法
WPF 4.5探秘之三 Dispatcher的新方法
| 作者:慧都控件网 |
21:59:09| 阅读 0次
概述:这是WPF 4.5的新特性介绍系列的第三部分。介绍了WPF4.5的Dispatcher的新的用法以及一些其它的新功能,并以代码的形式让读者更容易深入理解。
这是WPF 4.5的新特性介绍系列的第三部分。& &&
当你开始做的事情不同步时,Dispatcher有可能是最常用到的。因为这是从另一个线程来更新UI控件唯一的方法。
即使它很容易使用,WPF的团队也为它添加了13种方法。特别是新的await keyword。在本文里,我们将探秘这些新方法。
新的&类&方法
有的过载将作为一个参数Func委托。在之前的版本里,在Dispatcher的可用方法里无法返回一些东西(除了void),但是在新版本里已实现了这一功能。
这种新方法是:
Invoke(Func)
Invoke(Func, DispatcherPriority)
Invoke(Func, DispatcherPriority, CancellationToken)
Invoke(Func, DispatcherPriority, CancellationToken, TimeSpan)
在WPF 4.5之前,返回一些东西,这段代码应该写为:
//The function which returns something (here of type object)
Func&object& myReturningObjectFunction = () =&
{//For example only !
return new object();
object returnedOject =
//A mock action to be abble to return the object
Action mockAction = () =& { returnedOject = myReturningObjectFunction(); };
//Actually invoke the method
Dispatcher.CurrentDispatcher.Invoke(mockAction, null);
//Here I can use the returned object
现在,您可以使用这个编写一个更容易维持的代码:
public void CallingMethod()
object returnedOject = Dispatcher.CurrentDispatcher
.Invoke(MyReturningObjectFunction, null);
//This method can now live alone !
private object MyReturningObjectFunction()
//For example only !
return new object();
Await-ready !
WPF团队已经注意到了一些新生的&await&关键字,并且Dispatcher现在也可以支持这些关键字了。
以下是一些新的方法:
1. InvokeAsync(Action)
2. InvokeAsync(Action, DispatcherPriority)
3. InvokeAsync(Action, DispatcherPriority, CancellationToken)
4. InvokeAsync(Func)
5. InvokeAsync(Func, DispatcherPriority)
6. InvokeAsync(Func, DispatcherPriority, CancellationToken)
这些方法返回DispatcherOperation / DispatcherOperation类型的对象。然后您就可以在它本身或者是&Task&属性里使用await关键字。
下面举个例子:
public async void CallingMethod()
await Dispatcher.CurrentDispatcher.InvokeAsync(MyReturningObjectFunction);
同样,你可以做一些同步,通过使用DispatcherOperationWait方法等待Dispatcher操作完成。这是TaskExtensions类的扩展方法(在System.Windows.Threading上都可以使用)。
DispatcherOperation&object& dispatcherOperation =
Dispatcher.CurrentDispatcher.InvokeAsync(MyReturningObjectFunction);
dispatcherOperation.Task.Wait();
最后将会出现一个内容为&Calling Task.Wait will result in a deadlock if the operation is queued on a calling thread. For more information about using a Task to perform asynchronous operations, see Task Parallelism (Task Parallel Library).&的免责申明/警告。
Cancellation
最后,您可能已经注意到CancellationToken类型的新参数。这是.NET 4 Cancellation Framework的一部分。在后台中,Dispatcher操作将开始创建一个受到这个取消指令的管理Task对象。
如果Task没有启动即你的调度程序操作没有启动,那么它将不再启动。如果它已经开始那么它不会被停止,并且继续执行。
事实上,取消指令的停止与否完全取决于任务本身的运转。然而,我没有为给定的Action找到一种方法跳过Cancellation指令,而不再使用在第一段中关于返回对象提到过的相同的方法。
并没有一定要使用取消指令的场景,但是我认为这是使异步关键字的工作的必要条件。
这种情况在demo中很难复制,但是我们还找到了一个工作例子:
//Create a token source
var cts = new CancellationTokenSource();
//Launch the cancel of the token
Task.Factory.StartNew(() =& cts.Cancel());
//Launch an operation with priority normal
// this will delay the execution of the following Invoke call.
Dispatcher.BeginInvoke(new Action(() =&
int i = 0; while (i & 300)
//Update the UI to be sure to block the following
// action. Rendering will occur and takes precedence
{ i++; _counterTextBlock.Text = i.ToString(); }
}), null);
//Launch a task with the cancellation token
Dispatcher.Invoke(() =& { while (true);},
DispatcherPriority.Background, cts.Token);
catch (OperationCanceledException ex)
//We are here because the token was cancelled
Console.WriteLine(&The operation didn't even start !&);
另外还有两个更容易使用的方法:
1. Invoke(Action)
2. Invoke(Action, DispatcherPriority, CancellationToken, TimeSpan)
本站文章除注明转载外,均为本站原创或翻译
欢迎任何形式的转载,但请务必注明出处,尊重他人劳动成果
转载请注明:文章转载自:慧都控件网 []
本文地址:TaskFactory(TResult) 类 (System.Threading.Tasks)
TaskFactory&TResult& 类
.NET Framework 4
TResult& 对象的支持。
System.Threading.Tasks.TaskFactory&TResult&
命名空间:
mscorlib(在 mscorlib.dll 中)
[HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization = true,
ExternalThreading = true)]
public class TaskFactory&TResult&
TResultTResult 对象获得的结果的类型。TaskFactory&TResult& 类型公开以下成员。
名称说明TResult 实例。TResult 实例。TResult 实例。TResult 实例。TResult 实例。
名称说明。 值。 值。。
名称说明TResult,它将在提供的一组任务完成后马上开始。TResult,它将在提供的一组任务完成后马上开始。TResult,它将在提供的一组任务完成后马上开始。TResult,它将在提供的一组任务完成后马上开始。TResult,它将在提供的一组任务完成后马上开始。TResult,它将在提供的一组任务完成后马上开始。TResult,它将在提供的一组任务完成后马上开始。TResult,它将在提供的一组任务完成后马上开始。TResult,它将在提供的组中的任何任务完成后马上开始。TResult,它将在提供的组中的任何任务完成后马上开始。TResult,它将在提供的组中的任何任务完成后马上开始。TResult,它将在提供的组中的任何任务完成后马上开始。TResult,它将在提供的组中的任何任务完成后马上开始。TResult,它将在提供的组中的任何任务完成后马上开始。TResult,它将在提供的组中的任何任务完成后马上开始。TResult,它将在提供的组中的任何任务完成后马上开始。 是否等于当前的 。 (继承自 。)允许对象在“垃圾回收”回收之前尝试释放资源并执行其他清理操作。 (继承自 。)TResult,它在指定的
完成时执行一个结束方法函数。TResult,它表示符合异步编程模型模式的成对的开始和结束方法。TResult,它在指定的
完成时执行一个结束方法函数。TResult,它表示符合异步编程模型模式的成对的开始和结束方法。TResult,它在指定的
完成时执行一个结束方法函数。TResult,它表示符合异步编程模型模式的成对的开始和结束方法。TResult,它表示符合异步编程模型模式的成对的开始和结束方法。TResult,它表示符合异步编程模型模式的成对的开始和结束方法。TResult,它表示符合异步编程模型模式的成对的开始和结束方法。TResult,它表示符合异步编程模型模式的成对的开始和结束方法。TResult,它表示符合异步编程模型模式的成对的开始和结束方法。用作特定类型的哈希函数。 (继承自 。)。 (继承自 。) 的浅表副本。 (继承自 。)TResult。TResult。TResult。TResult。TResult。TResult。TResult。TResult。返回表示当前对象的字符串。 (继承自 。)TResult
类将其中一些常用
模式编码到获取默认设置的方法中,可以通过其构造函数进行配置。TResult
的默认实例通过
属性获得。注意应用到此类型或成员的
特性具有以下
属性值:Synchronization | ExternalThreading。 不影响桌面应用程序(桌面应用程序一般通过双击图标、键入命令或在浏览器中输入 URL 启动)。有关更多信息,请参见
类或 。受以下版本支持:4受以下版本支持:4Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2
.NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见。
的所有公共且受保护的成员都是线程安全的,可从多个线程同时使用。
本文是否对您有所帮助?
需要更多代码示例
翻译需要改进
(1500 个剩余字符)
感谢您的反馈}

我要回帖

更多关于 如何打开系统属性 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信