我照书上写了一个启动线程的控制台例子,在VS2005里却通不过,是什么原因啊?是不是VS版本的原因,错误提示就是标题所示:
1.建一控制台应用程序,项目名为“startThread"
2.拖拽一ListBox控件放到表单中,把Form1的text属性设为“启动线程”
3.在名空间添加类Sample,代码如下:
public class Sample
{
private System.Windows.Forms.ListBox listBox1; public Sample(System.Windows.Forms.ListBox a)
{
this.listBox1 = a;
}
public void work()
{
listBox1.Items.Add("Sample的work线程已运行0");
}
}
4.Form1的Load事件添加如下代码:
public void Form1_Load(object sender, System.EventArgs e)
{
Console.ReadLine();
Sample st = new Sample(listBox1);
Thread t = new Thread(new ThreadStart(st.work));
t.Start(); }
5.运行,我运行的时候出现dos框一闪而过,没有看到“Sample的work线程已运行0”字样
1.建一控制台应用程序,项目名为“startThread"
2.拖拽一ListBox控件放到表单中,把Form1的text属性设为“启动线程”
3.在名空间添加类Sample,代码如下:
public class Sample
{
private System.Windows.Forms.ListBox listBox1; public Sample(System.Windows.Forms.ListBox a)
{
this.listBox1 = a;
}
public void work()
{
listBox1.Items.Add("Sample的work线程已运行0");
}
}
4.Form1的Load事件添加如下代码:
public void Form1_Load(object sender, System.EventArgs e)
{
Console.ReadLine();
Sample st = new Sample(listBox1);
Thread t = new Thread(new ThreadStart(st.work));
t.Start(); }
5.运行,我运行的时候出现dos框一闪而过,没有看到“Sample的work线程已运行0”字样
解决方案 »
- SetupDiEnumDeviceInterfaces返回false,错误259 C++范例程序正确,改为C#错误
- 请问老手 vs2008给相关控件添加数据绑定项 出现“未指定的错误”提示怎么解决
- 关于TreeView控件 Check问题
- c#怎样导入导出excel数据?
- 引用的疑惑
- 急求在线等 基于msbuild 的每日构建 和vss 问题
- 简单问题:C#怎么把16进制数"3031454G"转为字符串”EK10“立即给分!
- 求解压 压缩文件案列
- vs2005,给窗体加ico图标,为什么运行后,就不显示图标了?并且exe的图标也没更改?
- 如何使用excel中的宏?
- C# struct类型中如何定义数组
- 母版页导致弹出页面脚本失效
你这是控制台程序吧,是不是连form1窗体都没起来?
连UI都没有吧,求解答
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.Text;
using System.IO;
using System.Threading;
namespace Text
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
FileStream fs;
FileStream wfs;
byte[] filedata;
int len;
private void button1_Click(object sender, EventArgs e)
{
Thread thd, thd2;
fs = new FileStream(@"E:\test.txt", FileMode.Open);
wfs = new FileStream(@"E:\write.txt", FileMode.Create);
len = (int)fs.Length;
filedata = new Byte[len];
fs.Read(filedata, 0, len);
System.Threading.AutoResetEvent controlThda =
new System.Threading.AutoResetEvent(false);
System.Threading.AutoResetEvent controlThdb =
new System.Threading.AutoResetEvent(false);
System.Threading.ThreadPool.QueueUserWorkItem(new WaitCallback(this.MultiWritetwo), controlThda);
System.Threading.ThreadPool.QueueUserWorkItem(new WaitCallback(this.MultiWriteone), controlThdb);
controlThda.WaitOne();
controlThdb.WaitOne(); wfs.Close();
fs.Close();
Console.WriteLine("f"); } private void MultiWriteone(object state)
{
wfs.Seek(0, SeekOrigin.Begin);
wfs.Write(filedata, 0, 5);
Console.WriteLine("1 End");
((System.Threading.AutoResetEvent)state).Set();
}
private void MultiWritetwo(object state)
{
wfs.Seek(5, SeekOrigin.Begin);
wfs.Write(filedata, 5, len - 5);
Console.WriteLine("2 End");
((System.Threading.AutoResetEvent)state).Set();
} }
}