我有代码,它异步运行一些块。如何在不创建 LongTask() 和 LongOperationAsync() 函数的情况下使 LongOperation() 以“内联”方式(直接从 Main 方法)异步运行?
class Program
{
static void Main(string[] args)
{
LongOperationAsync();
Console.WriteLine("Main thread finished.");
Console.ReadKey();
}
static void LongOperation()
{
Thread.Sleep(3000);
Console.WriteLine("Work completed");
}
static Task LongTask()
{
return Task.Run(() => LongOperation());
}
static async void LongOperationAsync()
{
await LongTask();
Console.WriteLine("Callback triggered");
}
}
Run Code Online (Sandbox Code Playgroud)
UPD
我希望我做对了。我的意思是,我想让任何现有函数异步并在执行后添加一些操作而不添加任何新函数。似乎下面的解决方案正在起作用。
class Program
{
static void Main(string[] args)
{
((Action)(async () =>
{
await Task.Run(() => LongOperation());
Console.WriteLine("Then callback triggered");
}))();
Console.WriteLine("Main thread finished first.");
Console.ReadKey();
}
static …Run Code Online (Sandbox Code Playgroud) 当我收到来自Telegram的消息时,我会在Activity.ChannelData字段中获得有关帐户的详细信息(例如用户名,conversation_id等).
{
"update_id": ,
"callback_query": {
"id": "",
"from": {
"id": ,
"is_bot": false,
"first_name": "",
"last_name": "",
"username": "",
"language_code": ""
},
"message": {
"message_id": ,
"from": {
"id": ,
"is_bot": true,
"first_name": "",
"username": ""
},
"chat": {
"id": ,
"first_name": "",
"last_name": "",
"username": "",
"type": "private"
},
"date": ,
"text": "Example text"
},
"chat_instance": "",
"data": ""
}
}
Run Code Online (Sandbox Code Playgroud)
但是当涉及到Skype时,我所看到的只是消息文本,而不是其他内容.
{ "text": "Example text"}
Run Code Online (Sandbox Code Playgroud)
怎么知道,谁给我发了消息呢?
Upd:这不应该是用户的名字,任何其他数据,如用户唯一ID都适合.