Common C#/C# Threading
기본 Threading Study #1
penguindori
2009. 3. 13. 16:28
[form1 sorces]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace ButtonExam
{
public partial class Form1 : Form
{
ButtonExam.Pro form = new Pro();
public ProgressBar okpro = null;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("This is Main Thread");
form.Show();
form.Text = "안녕";
}
private void ThreadTask()
{
int stp;
int newval;
Random rnd = new Random();
okpro = form.getProg();
while (true)
{
stp = okpro.Step * rnd.Next(-1, 2);
newval = okpro.Value + stp;
if (newval > okpro.Maximum)
newval = okpro.Maximum;
else if (newval < okpro.Minimum)
newval = okpro.Minimum;
okpro.Value = newval;
Thread.Sleep(100);
}
}
private void Form1_Load(object sender, EventArgs e)
{
Thread trd = new Thread(new ThreadStart(this.ThreadTask));
trd.IsBackground = true;
trd.Start();
}
}
} |

[form2 sorces]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ButtonExam
{
public partial class Pro : Form
{
public ProgressBar getProg()
{
return this.progressBar1;
}
public Pro()
{
InitializeComponent();
}
}
} |