大家好,本人是,想请大家帮看下代码

.Net技术 码拜 8年前 (2016-05-15) 648次浏览
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
解决方案

10

本人设断点调试下多调试几次找到错误的原因。

10

添加一个using System.Threading.Tasks;

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明大家好,本人是,想请大家帮看下代码
喜欢 (0)
[1034331897@qq.com]
分享 (0)