- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我指的是 this解释 .Net framework 4.5 中 await 和 async 关键字用法的博客
我正在尝试使用这些关键字解决以下实时场景
我有两个设备 Device1 和 Device2。这些设备使用串行端口 (RS 232) 连接到我的计算机。我有一个能够向这些设备发送命令的 Windows 应用程序。
现在,最初我必须通过发送特定的 RS-232 命令来启动这两个设备。现在我可以同时完成这项工作并相应地更新 UI。下面是解决这种情况的代码
public class Device1
{
public async Task<int> Start(int sec)
{
Console.WriteLine("Device1 started @ : " + DateTime.Now.ToString());
Task t = new Task(() => { Thread.Sleep(sec * 1000); });
t.Start();
await t;
Console.WriteLine("Device1 finished @ : " + DateTime.Now.ToString());
return 1;
}
}
public class Device2
{
public async Task<int> Start(int sec)
{
Console.WriteLine("Device2 started @ : " + DateTime.Now.ToString());
Task t = new Task(() => { Thread.Sleep(sec * 1000); });
t.Start();
await t;
Console.WriteLine("Device2 finished @ : " + DateTime.Now.ToString());
return 1;
}
}
private async void button1_Click(object sender, EventArgs e)
{
Device2 d2= new Device2();
Device1 d1= new Device1();
await d2.Start(10);
label1.Text = "d2 Started....";///It takes 10 sec to update this
await d1.Start(5);///this line starts executing after 10 secs? Why?
label1.Text = "d1 Started...";
MessageBox.Show("I am done...");///Get this message after 15 sec!!!
}
现在我的理解是 await d2.Start(10);
和 await d1.Start(5);
将同时运行并相应地更新 UI。但事实并非如此。仔细查看 Console.WriteLine 状态证明其完全顺序调用。
有人可以对此有更多的了解吗?
最佳答案
我推荐我的 async
intro对于 async
新手;它的末尾有指向最佳后续文档的链接。
简而言之,您正在等待
任务。这将暂停该方法,直到这些任务完成。如果你想让它们并发运行,那么你可以将任务保存到变量中(t1
,t2
),然后等待它们(await Task.WhenAll( t1, t2);
).
附言不要在 async
代码中使用 Task
构造函数或 Task.Start
;请改用 Task.Run
。
关于c# - 异步和等待,有点困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21677452/
我对 Powershell 很陌生,正在设置我的 profile.ps1 文件。我有一些托管 DLL,我经常使用它们来维护全天的进程,我希望能够通过快速函数调用加载它们。所以我在我的 ps1 文件中创
实际上我正在处理让我的代码在 IE 中运行的问题。不幸的是这仍然是必要的。 不受支持的方法的 Polyfill 工作正常。但我想知道,如何/是否可以使“const”和“let”起作用。有什么办法可以做
我有一个按钮,它将 window.location 设置为一个 php 文件,该文件生成一个提要,然后下载该提要。但是,由于文件的大小因放入提要的数据而异,有时从单击按钮到弹出文件对话框可能需要一段时
在 iOS 中获取以下内容的最佳方式是什么: 3 个按钮 (?),点击后会更改显示的信息。我有一些想法,但我想听听有经验的人的意见。我问的是如何获得它的外观,最好是一个接一个地使用按钮吗?如果是的话,
int trees = 3; int tree_x, tree_y; for(int r = 0; r 0) { tree_x = random() % m_townsize;
我看过很多关于 C++ 工厂的帖子,但到目前为止我还没有看到解决我的问题的解决方案。 (虽然我可能遗漏了一些东西。) 示例控制台应用程序: #include #include #include
因此,我在 中有 X 幅图像要垂直居中(下面 jquery 中的类名是实际的 )。这工作得很好但是......它只采用第一个值并将其应用于其余图像,导致第一个居中而其他不居中。 我可能会补充一
好吧,正如您可能看出来的那样,我很难想出一个标题。无论如何,我需要从数据库中选择 5 个项目,不包括第一个项目。更具体地说,我在数据库中有项目,每个项目都有一个特定的、自动递增的 ID。我需要选择 I
给定一个项目数组,每个项目都有一个值和成本,什么是最好的算法来确定达到最小值所需的项目最低成本? 例如: Item: Value -> Cost ------------------- A 2
所以,如果你尝试像这样做一个嵌套类: //nestedtest.php class nestedTest{ function test(){ class E extends
是否可以仅深拷贝特定类型的对象,例如列表、字典或元组 示例:[[1, ], ] 我想深度复制第一个列表(当然还有.. 1),但不是SomeObj或 OtherObj .那些应该作为引用。 是否可以使用
跳至上次编辑 我有一个 Person 对象列表,我需要使用 randomize_pairs 函数将它们随机配对,每个 Person 对象都有一个属性目标他们与谁配对。 我的限制是没有人可以与自己配对(
最好用 ruby,但逻辑就足够了...... 考虑到一周是非标准的,我需要一年中的周数。因此,假设您将一周定义为周六 -> 周五。那么,给定一个日期,它是第几周 (1-52)? strftime
这个问题不太可能对任何 future 的访客有帮助;它只与一个较小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于全世界的互联网受众。如需帮助使此问题更广泛适用,visit the
我有这样的模式规则: $(BIN_DIR)/%: $(SOURCES_DIR)/%/*.c $(CC) $? $(CFLAGS) $(LDFLAGS) -o $@ $(LDLIBS) $(LI
CREATE DATABASE 'some database name' ; 使用 mysql 客户端按预期工作。 来自 php/mysqli 的相同查询(不同的数据库名称)- 有点失败。它写入 IN
我有一个 iPhone 应用程序,它本质上是一个列表。顶部有一个 UINavigationBar,然后有一个保存列表的 UITableView。我希望以某种方式提供一个选项,允许用户以不同的方式对列表
我的客户想要的可能实际上是不可能的 - 他们想要一个不裁剪并保留其尺寸的全屏背景图像。他们愿意接受空白,但他们希望图像尽可能地填满屏幕,而不在任何轴上裁剪。我能够找到很好的脚本来实现真正的整页背景,但
我有两个表:文章和文章标签 articles: id, author, date_time, article_text articletags: id, tag (article.id == arti
我正在开发一个模拟器,供应商可以在其中创建由消费者购买的报价。 提供商将报价放在价格(y 轴)x 质量(x 轴)平面上。 消费者只会购买价格低于他们偏好但质量更高的商品 消费者购买具有最大欧氏距离的商
我是一名优秀的程序员,十分优秀!