using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using System.Text;
class Program : Form
{
private System.Windows.Forms.ProgressBar _ProgressBar;
[STAThread]
static void Main()
{
Application.Run(new Program());
}
public Program()
{
InitializeComponent();
Task.Factory.StartNew(Increment);
}
void UpdateProgressBar()
{
if (_ProgressBar.InvokeRequired)
{
MethodInvoker updateProgressBar =
UpdateProgressBar;
_ProgressBar.BeginInvoke(updateProgressBar);
}
else
{
_ProgressBar.Increment(1);
}
}
private void Increment()
{
for (int i = 0; i < 100; i++)
{
UpdateProgressBar();
Thread.Sleep(100);
}
if (InvokeRequired)
{
Invoke(new MethodInvoker(Close));
}
else
{
Close();
}
}
private void InitializeComponent()
{
_ProgressBar = new ProgressBar();
SuspendLayout();
_ProgressBar.Location = new Point(13, 17);
_ProgressBar.Size = new Size(267, 19);
ClientSize = new Size(292, 53);
Controls.Add(this._ProgressBar );
Text = “Multithreading inWindows Forms”;
ResumeLayout(false);
}
}
提示错误为:
错误 1 当前上下文中不存在名称“Task” C:\Documents and Settings\Administrator\My Documents\Visual Studio 2010\Projects\ConsoleApplication3\ConsoleApplication3\Program.cs 19 9 ConsoleApplication3
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using System.Text;
class Program : Form
{
private System.Windows.Forms.ProgressBar _ProgressBar;
[STAThread]
static void Main()
{
Application.Run(new Program());
}
public Program()
{
InitializeComponent();
Task.Factory.StartNew(Increment);
}
void UpdateProgressBar()
{
if (_ProgressBar.InvokeRequired)
{
MethodInvoker updateProgressBar =
UpdateProgressBar;
_ProgressBar.BeginInvoke(updateProgressBar);
}
else
{
_ProgressBar.Increment(1);
}
}
private void Increment()
{
for (int i = 0; i < 100; i++)
{
UpdateProgressBar();
Thread.Sleep(100);
}
if (InvokeRequired)
{
Invoke(new MethodInvoker(Close));
}
else
{
Close();
}
}
private void InitializeComponent()
{
_ProgressBar = new ProgressBar();
SuspendLayout();
_ProgressBar.Location = new Point(13, 17);
_ProgressBar.Size = new Size(267, 19);
ClientSize = new Size(292, 53);
Controls.Add(this._ProgressBar );
Text = “Multithreading inWindows Forms”;
ResumeLayout(false);
}
}
提示错误为:
错误 1 当前上下文中不存在名称“Task” C:\Documents and Settings\Administrator\My Documents\Visual Studio 2010\Projects\ConsoleApplication3\ConsoleApplication3\Program.cs 19 9 ConsoleApplication3
解决方案
10
本人设断点调试下多调试几次找到错误的原因。
10
添加一个using System.Threading.Tasks;