質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Q&A

解決済

1回答

384閲覧

public ○○ の中にawaitを書く方法

junyasu0124

総合スコア38

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

0グッド

0クリップ

投稿2022/09/26 16:55

前提・実現したいこと・試したこと

クラスを作っているのですが、上のコードのように public NotificationScheduler(略) とするとその中にawaitを入れられず、下のコードのように public async void Scheduler(略) とするとほかの場所から NotificationScheduler notificationScheduler = new NotificationScheduler(略) のようにして使う(?)ことができないです。awaitを使う方法または、classの中のpublic async voidをほかの場所から使う方法を教えてください。

該当のソースコード

C#

1namespace Example.Helpers 2{ 3 public class NotificationScheduler 4 { 5 public NotificationScheduler(string title, string folder, DateTime notificationDateTime, string dateTimeText, int iD) //ここ 6 { 7 string snoozeText = Windows.ApplicationModel.Resources.ResourceLoader.GetForCurrentView().GetString("NotificationScheduler_Snooze"); 8 var content = new ToastContentBuilder() 9 .AddArgument(title, iD) 10 .AddText(title) 11 .AddText(dateTimeText) 12 .AddText(folder) 13 .AddButton(new ToastButton() 14 { 15 ActivationType = ToastActivationType.Background, 16 ActivationOptions = new ToastActivationOptions() 17 { 18 AfterActivationBehavior = ToastAfterActivationBehavior.PendingUpdate 19 } 20 } 21 .AddArgument(iD.ToString(), "Snooze") 22 .SetContent(snoozeText) 23 ) 24 .SetToastScenario(ToastScenario.Reminder) 25 .AddCustomTimeStamp(notificationDateTime) 26 .GetToastContent(); 27 28 ScheduledToastNotification scheduledToast = new ScheduledToastNotification(content.GetXml(), DateTimeOffset.Now.AddSeconds(10)); 29 30 var notifier = await ToastNotificationManager.GetDefault().GetToastNotifierForToastCollectionIdAsync("MyToastCollection"); //ここ 31 32 notifier.AddToSchedule(scheduledToast); 33 } 34 } 35} 36

C#

1namespace Example.Helpers 2{ 3 public class NotificationScheduler 4 { 5 public async void Scheduler(string title, string folder, DateTime notificationDateTime, string dateTimeText, int iD) //ここ 6 { 7 string snoozeText = Windows.ApplicationModel.Resources.ResourceLoader.GetForCurrentView().GetString("NotificationScheduler_Snooze"); 8 var content = new ToastContentBuilder() 9 .AddArgument(title, iD) 10 .AddText(title) 11 .AddText(dateTimeText) 12 .AddText(folder) 13 .AddButton(new ToastButton() 14 { 15 ActivationType = ToastActivationType.Background, 16 ActivationOptions = new ToastActivationOptions() 17 { 18 AfterActivationBehavior = ToastAfterActivationBehavior.PendingUpdate 19 } 20 } 21 .AddArgument(iD.ToString(), "Snooze") 22 .SetContent(snoozeText) 23 ) 24 .SetToastScenario(ToastScenario.Reminder) 25 .AddCustomTimeStamp(notificationDateTime) 26 .GetToastContent(); 27 28 ScheduledToastNotification scheduledToast = new ScheduledToastNotification(content.GetXml(), DateTimeOffset.Now.AddSeconds(10)); 29 30 var notifier = await ToastNotificationManager.GetDefault().GetToastNotifierForToastCollectionIdAsync("MyToastCollection"); //ここ 31 32 notifier.AddToSchedule(scheduledToast); 33 } 34 } 35} 36

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

NotificationScheduler notificationScheduler = new NotificationScheduler();
notificationScheduler.Scheduler(略);
とするとawaitでもできる。

投稿2022/09/26 17:01

junyasu0124

総合スコア38

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2022/09/26 22:23

> NotificationScheduler notificationScheduler = new NotificationScheduler(); > notificationScheduler.Scheduler(略); そのコードを見る限りでは await してませんが? 以下の記事を参考にしてはいかがですか? Async OOP 2: Constructors https://blog.stephencleary.com/2013/01/async-oop-2-constructors.html
YAmaGNZ

2022/09/26 22:42

多分コンパイルエラーが出なくなったというだけで想像している処理になっていないというのが分かっていないのではないでしょうか。 Schedulerをasync voidで定義していることからもとりあえずのコンパイルエラーを消したというだけではないかと思います。 NotificationScheduler notificationScheduler = new NotificationScheduler(); Console.WriteLine("Start"); notificationScheduler.Scheduler(略); Console.WriteLine("End"); といった感じでどのような順番で実行されるのかを確認されたほうがいいかと思います。
退会済みユーザー

退会済みユーザー

2022/09/26 22:51

質問してから 6 分で自己解決 (?) して回答まで書いてることから想像して、自分が見つけた案を自慢したかっただけのように思いましたが、違いますか?
Zuishin

2022/09/27 03:44

えっと、何が言いたかったのか推測するに、コンストラクターを await したかったということだと思います。 しかしそれができないので、コンストラクターの中で行っていた処理を外に出し、new した後にそのメソッドを await することにしたのではないかと。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問