关于c#–我可以停止System.Threading.Timer和c#停止运行代码的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于.net–Windows.System.Threading
关于c# – 我可以停止System.Threading.Timer和c#停止运行代码的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于.net – Windows.System.Threading.ThreadPool和System.Threading.ThreadPool有什么区别?、.net – 使用异步,等待与System.Threading.Thread.Sleep、.net – 在mscorlib.dll中发生类型“System.Threading.ThreadAbortException”的第一次机会异常、C# System.Threading.AutoResetEvent等相关知识的信息别忘了在本站进行查找喔。
本文目录一览:- c# – 我可以停止System.Threading.Timer(c#停止运行代码)
- .net – Windows.System.Threading.ThreadPool和System.Threading.ThreadPool有什么区别?
- .net – 使用异步,等待与System.Threading.Thread.Sleep
- .net – 在mscorlib.dll中发生类型“System.Threading.ThreadAbortException”的第一次机会异常
- C# System.Threading.AutoResetEvent
c# – 我可以停止System.Threading.Timer(c#停止运行代码)
是否有任何方法可以使用Thread.Abort()以编程方式中止线程,或者我是否必须在代码中添加一个标志以指示线程已完成并在启动的代码中的有害位置检查通过计时器?
解决方法
注意:通常使用Abort是一种不好的做法.这是一种相当可靠的方法,最终难以检测到死锁和/或资源泄漏.使用像CancellationToken这样的被动机制要好得多
.net – Windows.System.Threading.ThreadPool和System.Threading.ThreadPool有什么区别?
解决方法
如果您正在为WP8或Windows应用商店开发XAML应用程序,请使用Windows.System版本.
.net – 使用异步,等待与System.Threading.Thread.Sleep
await Task.Delay(3000);
现在我的长期计算真的很像:
System.Threading.Thread.Sleep(3000)
例如:
for(i=0;i<1000000000;i++) i=i*2;
如何使这个工作与异步等待?现在看来,我可以’只’使用诸如webrequest,WCF等的方法与这个伟大的新方法.
我在哪里错过了点?
解决方法
await Task.Run(() => Thread.Sleep(10000))
会工作,但它没有意义
.net – 在mscorlib.dll中发生类型“System.Threading.ThreadAbortException”的第一次机会异常
但是当我尝试调试应用程序时,会给我任何错误 –
mscorlib.dll中出现了“System.Threading.ThreadAbortException”类型的第一个机会异常
它不会在任何特定的代码行错误。它给出任何代码行(不确定)..然后网页显示 – “服务器应用程序不可用”错误的错误。
即使我试图在即时窗口/快速观察中解析/执行一些声明 – 它给我上面的错误..
例如,我在XElement中加载了一个xml文档(超过10000行),当我尝试通过xpath检查一些属性值为XElementObj.XPathSelectElement(“/ asdf / asd / wqer / xyz”)..它给出了上面的错误(不是所有的时间,而是随机)。
任何人有任何想法这个..请帮助。
If you want to pinpoint where the
exceptions are occurring,you can
select the Debug->Exceptions menu
item,and in the dialog that appears,
check the first checkBox for “Common
Language Runtime Exceptions”. This
will make the debugger break as soon
as an exception occurs instead of only
breaking on unhandled exceptions.This is also one reason why it is generally a bad idea to catch generic exceptions unless you are clearly logging the information caught.
C# System.Threading.AutoResetEvent
表示线程同步事件在一个等待线程释放后收到信号时自动重置.
1 using System;
2 using System.Threading;
3
4 // Visual Studio: Replace the default class in a Console project with
5 // the following class.
6 class Example
7 {
8 private static AutoResetEvent event_1 = new AutoResetEvent(true);
9 private static AutoResetEvent event_2 = new AutoResetEvent(false);
10
11 static void Main()
12 {
13 Console.WriteLine("Press Enter to create three threads and start them.\r\n" +
14 "The threads wait on AutoResetEvent #1, which was created\r\n" +
15 "in the signaled state, so the first thread is released.\r\n" +
16 "This puts AutoResetEvent #1 into the unsignaled state.");
17 Console.ReadLine();
18
19 for (int i = 1; i < 4; i++)
20 {
21 Thread t = new Thread(ThreadProc);
22 t.Name = "Thread_" + i;
23 t.Start();
24 }
25 Thread.Sleep(250);
26
27 for (int i = 0; i < 2; i++)
28 {
29 Console.WriteLine("Press Enter to release another thread.");
30 Console.ReadLine();
31 event_1.Set();
32 Thread.Sleep(250);
33 }
34
35 Console.WriteLine("\r\nAll threads are now waiting on AutoResetEvent #2.");
36 for (int i = 0; i < 3; i++)
37 {
38 Console.WriteLine("Press Enter to release a thread.");
39 Console.ReadLine();
40 event_2.Set();
41 Thread.Sleep(250);
42 }
43
44 // Visual Studio: Uncomment the following line.
45 //Console.Readline();
46 }
47
48 static void ThreadProc()
49 {
50 string name = Thread.CurrentThread.Name;
51
52 Console.WriteLine("{0} waits on AutoResetEvent #1.", name);
53 event_1.WaitOne();
54 Console.WriteLine("{0} is released from AutoResetEvent #1.", name);
55
56 Console.WriteLine("{0} waits on AutoResetEvent #2.", name);
57 event_2.WaitOne();
58 Console.WriteLine("{0} is released from AutoResetEvent #2.", name);
59
60 Console.WriteLine("{0} ends.", name);
61 }
62 }
63
64 /* This example produces output similar to the following:
65
66 Press Enter to create three threads and start them.
67 The threads wait on AutoResetEvent #1, which was created
68 in the signaled state, so the first thread is released.
69 This puts AutoResetEvent #1 into the unsignaled state.
70
71 Thread_1 waits on AutoResetEvent #1.
72 Thread_1 is released from AutoResetEvent #1.
73 Thread_1 waits on AutoResetEvent #2.
74 Thread_3 waits on AutoResetEvent #1.
75 Thread_2 waits on AutoResetEvent #1.
76 Press Enter to release another thread.
77
78 Thread_3 is released from AutoResetEvent #1.
79 Thread_3 waits on AutoResetEvent #2.
80 Press Enter to release another thread.
81
82 Thread_2 is released from AutoResetEvent #1.
83 Thread_2 waits on AutoResetEvent #2.
84
85 All threads are now waiting on AutoResetEvent #2.
86 Press Enter to release a thread.
87
88 Thread_2 is released from AutoResetEvent #2.
89 Thread_2 ends.
90 Press Enter to release a thread.
91
92 Thread_1 is released from AutoResetEvent #2.
93 Thread_1 ends.
94 Press Enter to release a thread.
95
96 Thread_3 is released from AutoResetEvent #2.
97 Thread_3 ends.
98 */
构造函数
AutoResetEvent(Boolean) | 用一个指示是否将初始状态设置为终止的布尔值初始化 AutoResetEvent 类的新实例。 |
Close() | 释放由当前 WaitHandle 占用的所有资源。 (Inherited from WaitHandle) |
CreateObjRef(Type) | 创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。 (Inherited from MarshalByRefObject) |
Dispose() | 释放 WaitHandle 类的当前实例所使用的所有资源。 (Inherited from WaitHandle) |
Dispose(Boolean) | 当在派生类中重写时,释放 WaitHandle 使用的非托管资源,并且可选择释放托管资源。 (Inherited from WaitHandle) |
Equals(Object) | 确定指定的对象是否等于当前对象。 (Inherited from Object) |
GetHashCode() | 作为默认哈希函数。 (Inherited from Object) |
GetLifetimeService() | 检索控制此实例的生存期策略的当前生存期服务对象。 (Inherited from MarshalByRefObject) |
GetType() | 获取当前实例的 Type。 (Inherited from Object) |
InitializeLifetimeService() | 获取生存期服务对象来控制此实例的生存期策略。 (Inherited from MarshalByRefObject) |
MemberwiseClone() | 创建当前 Object 的浅表副本。 (Inherited from Object) |
MemberwiseClone(Boolean) | 创建当前 MarshalByRefObject 对象的浅表副本。 (Inherited from MarshalByRefObject) |
ToString() | 返回表示当前对象的字符串。 (Inherited from Object) |
WaitOne() | 阻止当前线程,直到当前 WaitHandle 收到信号。 (Inherited from WaitHandle) |
WaitOne(Int32) | 阻止当前线程,直到当前 WaitHandle 收到信号,同时使用 32 位带符号整数指定时间间隔(以毫秒为单位)。 (Inherited from WaitHandle) |
WaitOne(Int32, Boolean) | 阻止当前线程,直到当前的 WaitHandle 收到信号为止,同时使用 32 位带符号整数指定时间间隔,并指定是否在等待之前退出同步域。 (Inherited from WaitHandle) |
WaitOne(TimeSpan) | 阻止当前线程,直到当前实例收到信号,同时使用 TimeSpan 指定时间间隔。 (Inherited from WaitHandle) |
WaitOne(TimeSpan, Boolean) | 阻止当前线程,直到当前实例收到信号为止,同时使用 TimeSpan 指定时间间隔,并指定是否在等待之前退出同步域。 (Inherited from WaitHandle) |
Handle | 获取或设置本机操作系统句柄。 (Inherited from WaitHandle) |
SafeWaitHandle | 获取或设置本机操作系统句柄。 (Inherited from WaitHandle) |
IDisposable.Dispose() | 释放由 WaitHandle 使用的所有资源。 (Inherited from WaitHandle) |
WaitTimeout | 指示在任何等待句柄终止之前 WaitAny(WaitHandle[], Int32, Boolean) 操作已超时。此字段为常数。 (Inherited from WaitHandle) |
今天关于c# – 我可以停止System.Threading.Timer和c#停止运行代码的讲解已经结束,谢谢您的阅读,如果想了解更多关于.net – Windows.System.Threading.ThreadPool和System.Threading.ThreadPool有什么区别?、.net – 使用异步,等待与System.Threading.Thread.Sleep、.net – 在mscorlib.dll中发生类型“System.Threading.ThreadAbortException”的第一次机会异常、C# System.Threading.AutoResetEvent的相关知识,请在本站搜索。
本文标签: