10秒毎にメッセージを出力するプログラムを完成させたい
C#で時間経過毎にメールを送るプログラムの一部を作っていました。
しかし、Timerクラスを使用したのですが、よく分からないエラーが出現しました。
調べても内容もイマイチわかりません。
なぜ処理が実行されないのか理由を教えてください。
■■な機能を実装中に以下のエラーメッセージが発生しました。
visual studio でメニューバー?(上の方)で出現したよく分からないエラーメッセージ introduce variable code refactoring provider error ブレークポイントで timer.Elapsed += Timer_Elapsed; に焦点を当てた時 error CS0079: The event 'Timer.Elapsed' can only appear on the left hand side of += or -=
該当のソースコード
C#
1using System; 2using System.Timers; 3using System.IO; 4using System.Text; 5using MitFile; 6 7namespace Exercise 8{ 9 class MainHome 10 { 11 public static int Repetition; 12 public static string transFolderPath = "/Users/XXX/Documents/TransFolder"; 13 public static string devicePath = "/Users/XXX/Documents/DeviceFolder"; 14 public static Timer timer; 15 16 static void Main() 17 { 18 19 Repetition = 0; 20 timer = new Timer(); 21 timer.Interval = 10000; 22 timer.Elapsed += Timer_Elapsed;//タイマーイベントの処理、(時間経過後の処理)を登録 23 timer.Enabled = true;//これを呼ばないとタイマーは起動しない? 24 25 Console.WriteLine("データの転送を開始"); 26 if(Repetition >= 5) 27 { 28 timer.Enabled = false; 29 } 30 } 31 32 private static void Timer_Elapsed(object sender, ElapsedEventArgs e) 33 { 34 try 35 { 36 //Device device = new Device();未完成でコメントアウトしているコード 37 //device.DataCapture(transFolderPath,devicePath);上記に同じ 38 string mes = string.Format("{0} : 転送成功: {1}", DateTime.Now, Repetition); 39 Repetition++; 40 Console.WriteLine(mes); 41 42 } 43 catch 44 { 45 Console.WriteLine("Error"); 46 } 47 48 } 49 50 51 52 } 53 54 55} 56
試したこと
ラムダ式を使用しないコードに変更(する前から同じようなエラー)
補足情報(FW/ツールのバージョンなど)
visual studio for mac 2017 使用
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/21 14:08
2018/10/21 14:12
2018/10/21 14:21