c#
来源:互联网 发布:php 关闭call stack 编辑:程序博客网 时间:2024/06/06 14:04
Console:
using System;using System.Threading;using System.Threading.Tasks;namespace ConsoleApplication37{ class Program { public static Random R = new Random(); public static string T { get; set; } static void Main(string[] args) { AsyncCall2(); while (true) { string input = Console.ReadLine(); if (input == "exit") break; Console.WriteLine(input.Length + " characters."); } Console.WriteLine("Press <Anykey> to exit application."); Console.ReadLine(); } /// <summary> /// 无返回值的异步操作 /// </summary> public static async void AsyncCall() { var t = Task.Factory.StartNew(() => { while (true) { // 随机睡0.1~0.3秒 int time = R.Next(1, 3) * 100; Thread.Sleep(time); Console.WriteLine("AsyncCall()=>" + Guid.NewGuid().ToString() + " cost " + time + " ms."); } }); await t; } /// <summary> /// 调用有返回值异步操作 /// </summary> public static async void AsyncCall2() { while (true) { T = await AsyncCallWithReturn(); Console.WriteLine(T); } } /// <summary> /// 有返回值异步操作 /// </summary> /// <returns></returns> public static async Task<string> AsyncCallWithReturn() { // 等待结束 return await Task.Factory.StartNew<string>(() => { // 随机睡1~5秒 int time = R.Next(1, 5) * 1000; Thread.Sleep(time); return "AsyncCallWithReturn()=>" + Guid.NewGuid().ToString() + " cost " + time + " ms."; }); } }}
Winform:
using System;using System.Threading;using System.Threading.Tasks;using System.Windows.Forms;namespace WindowsFormsApplication1{ public partial class Form1 : Form { private System.ComponentModel.IContainer components = null; private System.Windows.Forms.Button button1; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Label label1; public Form1() { InitializeComponent(); } private async void button1_Click(object sender, EventArgs e) { this.label1.Text = this.textBox1.Text.Length + " characters."; var text = await AsyncCall(); this.label1.Text = text; } private async Task<string> AsyncCall() { return await Task.Factory.StartNew(() => { Thread.Sleep(5000); return Guid.NewGuid().ToString(); }); } #region AutoGenerated Code protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.textBox1 = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(46, 38); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 0; this.button1.Text = "Set"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(46, 12); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(356, 20); this.textBox1.TabIndex = 1; // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(46, 68); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(0, 13); this.label1.TabIndex = 2; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(529, 103); this.Controls.Add(this.label1); this.Controls.Add(this.textBox1); this.Controls.Add(this.button1); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); this.PerformLayout(); } #endregion }}
阅读全文
0 0
- C#
- C#
- C#
- C#
- C#
- C#
- C#
- C#
- C#
- C#
- C#
- c#
- c#
- C#
- C#
- C#
- c#
- C#
- 钱多的没处花?程序员老司机教你如何5年攒百万
- jquery实现双击编辑表格
- The method getNotCompleteOrder(ResultBean) of type HomeServiceImpl must override a superclass method
- 基于Rtmp协议的流媒体直播实现
- 基本函数
- c#
- 多线程编程(NSThread、NSOperation、GCD)
- iOS开发-应用角标展示逻辑处理
- Linux下的守护进程
- Arduino
- 微信传播如何使大家智商降低
- oracle查询语句大全 oracle 基本命令大全
- ERROR 1366 (HY000): Incorrect string value: '\xB4\xE7\xB1\xCA\xBC\xC7...' for co lumn 'goods_name' a
- AJAX——核心XMLHttpRequest对象