问个问题(多线同步),因为奥运会要开幕了,进来的都交好运!!

编程有钱人了 2008-08-04 04:21:39


public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Thread thread;//构造线程
OpenFileDialog op;//构造对话框
private void addfile_Click(object sender, EventArgs e)
{
op = new OpenFileDialog();
op.InitialDirectory = @"d:\";//对话框打开时初始化目录
op.Filter = "All files (*.*)|*.*|txt files (*.txt)|*.txt";//文件过滤
op.RestoreDirectory = true;//保存当前目录
if (op.ShowDialog() == DialogResult.OK)
{
this.listBox1.Items.Add(op.FileName.ToString());//把选择的文件添加到listbox中
}
}

private void start_Click(object sender, EventArgs e)//用线程开始删除指定目录下的文件
{
if (this.listBox1.SelectedItem.ToString().LastIndexOf(".") > 0)//是否是文件
{
File.Delete(this.listBox1.SelectedItem.ToString());
}
else//是否是文件夹
{
thread = new Thread(new ParameterizedThreadStart(DeleteFiles));//委托线程调用带参数的方法
thread.IsBackground = true;
thread.Start(this.listBox1.SelectedItem.ToString()); //执行删除方法
}
 
}

public void DeleteFiles(object state)//定义一个删除文件的方法
{
string strDir = state as string;
while (true)
{
Thread.Sleep(3000);
if (Directory.Exists(strDir))//判断指定的目录是否存在
{

string[] strFiles = Directory.GetFiles(strDir);//获取该目录下的所有文件
string filenameslog= "";//用来保存删除记录
foreach (string strFile in strFiles)
{
File.Delete(strFile);
filenameslog += strFile;
this.textBox1.Text = filenameslog + "\n";//在这出错了
//错误信息:线程间操作无效: 从不是创建控件“textBox1”的线程访问它。
//原因是线程不同步
}

}
}

}

private void listBox1_DragDrop(object sender, DragEventArgs e)
{
if(e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] MyFiles;
int i;
// 将文件赋给一个数组。
MyFiles = (string[])(e.Data.GetData(DataFormats.FileDrop));
// 循环处理数组并将文件添加到列表中。
for(i = 0;i <= MyFiles.Length - 1;i++)
{
this.listBox1.Items.Add(MyFiles[i]);
}
}
}

private void Form1_Load(object sender, EventArgs e)
{
this.listBox1.AllowDrop = true;
}

private void listBox1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.All;
}
}

private void stop_Click(object sender, EventArgs e)
{
if (thread != null)
{
thread.Abort();

}
}

private void removefile_Click(object sender, EventArgs e)
{
if(this.listBox1.SelectedIndex>-1)//是否选中
{
for (int i = 0; i < this.listBox1.Items.Count;i++ )
{
this.listBox1.Items.Remove(this.listBox1.Items[i]);
}
}

}
}

//错误信息:线程间操作无效: 从不是创建控件“textBox1”的线程访问它。
//原因是线程不同步
//我想把删除的结果保存成记录
放到文本框中,但是不行
如上
谁能帮忙解决一下,谢谢啊!
...全文
216 31 打赏 收藏 转发到动态 举报
写回复
用AI写文章
31 条回复
切换为时间正序
请发表友善的回复…
发表回复
nextsea 2008-08-05
  • 打赏
  • 举报
回复
问题不难,转点好运.
mawering 2008-08-05
  • 打赏
  • 举报
回复
关注,学习一下!
IceCoffee503 2008-08-05
  • 打赏
  • 举报
回复
线程中是没法获取控件的值的,我遇到过这种情况,后来用的是变量
编程有钱人了 2008-08-05
  • 打赏
  • 举报
回复
[Quote=引用 21 楼 hopewoo 的回复:]
lz把你项目发我 sonic260@163.com
[/Quote]

好的 发给你
try530 2008-08-05
  • 打赏
  • 举报
回复
凡是用到线程更改主线程或其他的线程上创建的控件属性是,都必须有用委托。

随便写一个委托更改textbox的text的方法:
delegate void OutDelegate(TextBox tb,string str);
/// <summary>
/// 向System.Window.Form.Label输出文本信息
/// </summary>
/// <param name="TextBox">System.Window.Form.TextBox</param>
/// <param name="text">文本信息</param>
public void outext(TextBox tb, string text)
{

if (tb.InvokeRequired)
{



OutDelegate Outstr = new OutDelegate(outext);



label.BeginInvoke(Outstr, new object[] { tb, text });



return;



}
tb.Text = text;
}

线程中直接调用outext(this.textbox, filenameslog );
fhrl 2008-08-05
  • 打赏
  • 举报
回复
用backgroundworker吧,msdn上有例子讲解
hopewoo 2008-08-05
  • 打赏
  • 举报
回复
lz把你项目发我 sonic260@163.com
美丽大道 2008-08-05
  • 打赏
  • 举报
回复
求好运!!
编程有钱人了 2008-08-05
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 wodegege10 的回复:]
\r\n
这样可以吗?
[/Quote]

还是不行
编程有钱人了 2008-08-05
  • 打赏
  • 举报
回复
[Quote=引用 30 楼 hopewoo 的回复:]
那我不知道了.我这里是没问题的.
[/Quote]

把你修改完的发给我吧
谢谢了

该结贴了
hopewoo 2008-08-05
  • 打赏
  • 举报
回复
那我不知道了.我这里是没问题的.
编程有钱人了 2008-08-05
  • 打赏
  • 举报
回复
[Quote=引用 28 楼 hopewoo 的回复:]
我这里没问题啊.照常换行的.
要不你试试 Environment.NewLine 替换\n吧.
[/Quote]

还是不能换行
hopewoo 2008-08-05
  • 打赏
  • 举报
回复
我这里没问题啊.照常换行的.
要不你试试 Environment.NewLine 替换\n吧.
aimeast 2008-08-04
  • 打赏
  • 举报
回复
楼上的invoke都是正确的。不过还有一种不推荐的方法:
Control.CheckForIllegalCrossThreadCalls = false;
virusswb 2008-08-04
  • 打赏
  • 举报
回复
用委托啊,或者在msdn中输入,线程安全,会有一个很好的例子,
wenbin 2008-08-04
  • 打赏
  • 举报
回复
\r\n
这样可以吗?
编程有钱人了 2008-08-04
  • 打赏
  • 举报
回复
没人回答吗 ?

我的"\n"不起作用了
编程有钱人了 2008-08-04
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 hopewoo 的回复:]
[C# code]
this.textBox1.BeginInvoke(txt, new object[1] { filenameslog });
[/Quote]
谢谢
问题解决了
但是现在为什么回车不管用了??
hopewoo 2008-08-04
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 wangjun8868 的回复:]
还有就是
线程怎么不能停止了
用了上面的方法
[/Quote]
....我理解不出了..
你试试

this.textBox1.BeginInvoke(txt, new object[1] { filenameslog });
加载更多回复(11)

110,529

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧