解决方案 »
- C#串口通信中如何接收串口的数据
- FTP通信测试问题
- 关于vs2005 第三控件的问题
- C#中的SqlDataAdapter控件的使用
- .NET 如何设制DataGridView中每列的宽度
- 如何用C#做PDF阅读器?
- 如何动态统计DATAGRID里模版列(textbox)的数据?
- 使用ShowDialog
- 项目文件太大 F5起来非常慢 咋办
- 封杀3721的程序下载,防止3721自动下载!
- 【WinForm】如何持久化一个可视化控件到 XXX.Designer.cs文件 [.NET技术 组件/控件开发]
- 看到之前的帖子说取目录里面的随即文件,我想顺便问个问题,如果是固定的范围应该怎么高效的取?
http://www.baidu.com/link?url=R1yEXT1WF-gmOgldlD1c7erxXAo5eF_zpkmiS5E5hiQ48FvK9GJa2LsMSwGAXTpI_eNWcd8dIV3k-DO1pJ8G0_&wd=多线程使用&tn=baidu&ie=utf-8&inputT=3790
using System.IO;
using System.Threading;
using System.Windows.Forms;namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private static string Path = "..\\.."; private void Form1_Load(object sender, EventArgs e)
{
Find(new DirectoryInfo(Path), "b");
} private void Find(DirectoryInfo dir, string str)
{
foreach (var file in dir.EnumerateFiles())
{
var f = file;
ThreadPool.QueueUserWorkItem(h =>
{
var content = File.ReadAllText(f.FullName);
if (content.Contains(str))
this.listBox1.BeginInvoke(new Action(() =>
{
this.listBox1.Items.Add(f.FullName);
}));
});
}
foreach (var subdir in dir.GetDirectories())
Find(subdir, str);
}
}
}这里,在Form1里边拖入一个 ListBox 控件,然后在运行时去显示当前工程下所有包含特定字符串的文件的路径。
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.IO;
using System.Text.RegularExpressions;
using System.Threading;
namespace 瞬网
{
public partial class Form1 : Form
{
int i = 0;
string sLine;
public Form1()
{
InitializeComponent();
xEngine.License.Init("瞬网");
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
timer1.Interval = 2000;
Thread thread = new Thread(run);
thread.Start();
}
public void run()
{
xEngine.Execute.Http execute = new xEngine.Execute.Http();
execute.LoadScript("瞬网.x", false); execute.Account.Account = textBox1.Text; execute.Account.Password = textBox2.Text;
string[] files = Directory.GetFiles(@"C:\Users\admin\Desktop\data", "*.txt");
if (i >= files.Count())
{
i = 0;
}
// Random r=new Random();
string flist = files[i];
FileStream f = new FileStream(flist, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(f);
execute.Account.Other1 = sr.ReadLine();
execute.Account.Other3 = sr.ReadToEnd() + execute.Account.Other2;
StreamReader myStream = null;
myStream = new StreamReader(flist);
while (true)
{
sLine = myStream.ReadLine();//一行一行的读,如果前面的数据部需要就直接抛弃了
if (myStream.EndOfStream)
{//现在这个sLine 就是最后一行了
break;
}
}
execute.Account.Other2 = sLine;
richTextBox1.Text = "正在发布第" + i + "篇文章";
i++;
sr.Close();
f.Close();
var response = execute.RunRequests();
string str = response.GetBodyStr(); if (str.Contains("0"))
{
label6.Text = "发布成功";
}
else
{
label6.Text = "发布失败";
}
}
private void timer1_Tick(object sender, EventArgs e)
{
button1_Click(null, null);
}
}
}具体代码是这样的,我就想改下用线程来实现,但是小弟不才,不大会用线程,还请大神帮帮忙
使用控件的InvokeRequired 属性来区分是不是 创建该控件线程以为的线程操作它。
使用控件的InvokeRequired 属性来区分是不是 创建该控件线程以为的线程操作它。不懂
使用控件的InvokeRequired 属性来区分是不是 创建该控件线程以为的线程操作它。不懂
你总是不懂? 不自己去找列子,尝试写代码,你永远不懂。
使用控件的InvokeRequired 属性来区分是不是 创建该控件线程以为的线程操作它。不懂
你总是不懂? 不自己去找列子,尝试写代码,你永远不懂。
教导的是,我在看着例子呢,呵呵~准备写代码,谢谢提醒
http://www.cnblogs.com/txw1958/archive/2012/08/21/csharp-crossthread-widget.html其实我也是新手。