前提
C#でYoutubeライブのチャットコメントを取得し、Formのtextboxに書き込みたい。
Youtubeライブのチャットコメントは参考記事を使っているが、async(非同期)で書かれている。
チャットコメントは取得できている。
実現したい
C#でsync(非同期)で取得した文字列をFormのtextboxに書き込みたい。
発生している問題・エラーメッセージ
debugモードで変数の中身を見ていると、コメントは取得できているが、textboxに出力されない。
該当のソースコード
参考 https://qiita.com/MCK9595/items/fdbd543ff938febcd136
C#
1using System; 2using System.Collections.Generic; 3using System.ComponentModel; 4using System.Data; 5using System.Drawing; 6using System.Linq; 7using System.Text; 8using System.Threading.Tasks; 9using System.Windows.Forms; 10using System.Threading; 11using System.IO.Ports; 12using Google.Apis.Services; 13using Google.Apis.YouTube.v3; 14 15namespace YoutuveLive 16{ 17 public partial class Form1 : Form 18 { 19 public Form1() 20 { 21 InitializeComponent(); 22 } 23 const string APIKey_ = "XXXXXXXXXXXXXXXXXXXXXXXX"; 24 private static Form1 f1; 25 26 private async void button2_Click(object sender, EventArgs e) 27 { 28 await MainStart(); 29 } 30 private void Form1_Load(object sender, EventArgs e) 31 { 32 33 } 34 35 static async Task MainStart() 36 { 37 f1 = new Form1(); 38 var youtubeService = new YouTubeService(new BaseClientService.Initializer() 39 { 40 ApiKey = APIKey_ 41 }); 42 43 string MOVIE_ID = f1.textBoxMovieID.Text; 44 45 string liveChatId = GetliveChatID(MOVIE_ID, youtubeService); 46 while (liveChatId == null || liveChatId == "") 47 { 48 liveChatId = GetliveChatID(MOVIE_ID, youtubeService); 49 } 50 await GetLiveChatMessage(liveChatId, youtubeService, null); 51 } 52 53 static public string GetliveChatID(string videoId, YouTubeService youtubeService) 54 { 55 //引数で取得したい情報を指定 56 var videosList = youtubeService.Videos.List("LiveStreamingDetails"); 57 videosList.Id = videoId; 58 //動画情報の取得 59 var videoListResponse = videosList.Execute(); 60 //LiveChatIDを返す 61 foreach (var videoID in videoListResponse.Items) 62 { 63 return videoID.LiveStreamingDetails.ActiveLiveChatId; 64 } 65 //動画情報取得できない場合はnullを返す 66 return null; 67 } 68 69 static public async Task GetLiveChatMessage(string liveChatId, YouTubeService youtubeService, string nextPageToken) 70 { 71 f1 = new Form1(); 72 var liveChatRequest = youtubeService.LiveChatMessages.List(liveChatId, "snippet,authorDetails"); 73 liveChatRequest.PageToken = nextPageToken; 74 75 var liveChatResponse = await liveChatRequest.ExecuteAsync(); 76 foreach (var liveChat in liveChatResponse.Items) 77 { 78 try 79 { 80 await Task.Run(() => 81 { 82 f1.textBoxLog.AppendText(liveChat.Snippet.DisplayMessage + liveChat.AuthorDetails.DisplayName + System.Environment.NewLine); 83 84 }); 85 } 86 catch { } 87 88 } 89 await Task.Delay((int)liveChatResponse.PollingIntervalMillis); 90 91 await GetLiveChatMessage(liveChatId, youtubeService, liveChatResponse.NextPageToken); 92 } 93 94 }//class 95}//namespace 96
試したこと
async(非同期)を使っているときに、単純にはFormへアクセスできない、ということはわかり、以下のような方法を試しました。
//Errorは出ないが出力はされない f1 = new Form1(); await Task.Run(() => { f1.textBoxLog.AppendText(liveChat.Snippet.DisplayMessage liveChat.AuthorDetails.DisplayName + System.Environment.NewLine); });
//Invokeにたいして、オブジェクト参照が必要です、のError await Task.Run(() => { Invoke(new Action(() => { //Invokeメソッド内ではUIスレッドに戻してくれる f1.textBoxLog.AppendText(liveChat.Snippet.DisplayMessage + liveChat.AuthorDetails.DisplayName + System.Environment.NewLine); })); });
補足情報(FW/ツールのバージョンなど)
C#、Visualstudio
本質問とは直接関係ありませんが、Form1のインスタンスがどうなっているか理解されたほうがよろしいかと思います。

回答3件
あなたの回答
tips
プレビュー