C# でWindows Formのアプリケーションを作成するにあたり、スレッドを使用するために、
ひとまずプロトとして、プログレスバーの表示を変化させている最中にボタンをクリック
するプログラムを作成しています。
プログレスバーを動的に変化させることはできていますが、ボタンを押下することが
できない状況になっております。
どの部分が変なのか見ていただけないでしょうか。以下が作成したソースになります。
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; 11 12namespace ThreadTest 13{ 14 public partial class Form1 : Form 15 { 16 //private Thread trd; 17 18 public Form1() 19 { 20 InitializeComponent(); 21 } 22 23 private void Form1_Load(object sender, System.EventArgs e) 24 { 25 Thread trd = new Thread(new ThreadStart(anotherThread)); 26 trd.Start(); 27 } 28 29 delegate void delegate1(); 30 31 private void button1_Click(object sender, EventArgs e) 32 { 33 MessageBox.Show("This is the main thread"); 34 } 35 36 private void ThreadTask() 37 { 38 int stp; 39 int newval; 40 Random rnd = new Random(); 41 42 while (true) 43 { 44 stp = this.progressBar1.Step * rnd.Next(-1, 2); 45 newval = this.progressBar1.Value + stp; 46 47 if (newval > this.progressBar1.Maximum) 48 newval = this.progressBar1.Maximum; 49 else if (newval < this.progressBar1.Minimum) 50 newval = this.progressBar1.Minimum; 51 52 this.progressBar1.Value = newval; 53 54 Thread.Sleep(100); 55 } 56 } 57 58 void anotherThread() 59 { 60 Invoke(new delegate1(ThreadTask)); 61 } 62 } 63} 64
以下のサイトを参考に作成してみました。
https://support.microsoft.com/ja-jp/kb/815804
http://urashita.com/archives/4336
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/10/07 03:43