现在有一批数据,我想一个线程处理记录的用户身份,另一个线程处理记录的时间,之后一个线程等到两个线程完成后,根据两个线程的结果把这些数据放到相应的表中,最后一个线程,就等到刚才那个线程完成后,再做一些善后工作.我想向各位求一个这样的线程框架,就是说,线程之间怎么样协调的写个样式给我就行了,例如:
static void Main()
{
Thread thread1 = new Thread(...)
.......
}
void ...(...)
{
//此处放...代码
}
如果觉得分不够的,我可以加,谢谢,谢谢!
static void Main()
{
Thread thread1 = new Thread(...)
.......
}
void ...(...)
{
//此处放...代码
}
如果觉得分不够的,我可以加,谢谢,谢谢!
解决方案 »
- 怎样实现RichTextBox中的图文混排,文本是从数据库中导出,图片存的是路径
- C#的OWC组件做出的曲线图问题!!
- C#画图问题
- 我的窗体上有几个按钮,在属性Enabled=false时,当鼠标放上去以后,ToolTip黄色提示窗口的文字不停地闪动,约5次/秒,Enabled=true时闪动停止。是怎么回事。
- 动态生成的TabPage怎么访问它?
- PIVOT 新的运算符,谁能帮忙?
- 关于gzipstream的使用
- 我作的打印datagrid(windows 控件),有要的留下email
- Java有什么好的开发意见
- 两个对象数组 ojb1[],obj2[] 每个都有十几万条记录怎么在内存中做sql运算?
- 关于建立多个Windows服务的小问题
- DataGrid控件中,空的日期字段如何进行区分表达?
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
public class ThreadMarshal : System.Windows.Forms.Form {
private System.ComponentModel.IContainer components;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.ProgressBar progressBar1; private Thread timerThread; public ThreadMarshal() { // Windows 窗体设计器所必需的
InitializeComponent(); } //在背景线程上执行此函数 - 它封送处理调用以
//将用户界面更新回前景线程
public void ThreadProc() { try {
MethodInvoker mi = new MethodInvoker(this.UpdateProgress);
while (true) {
//在窗体上调用 BeginInvoke
this.BeginInvoke(mi);
Thread.Sleep(500) ;
}
}
//当该线程被主线程中断时引发 - 退出该循环
catch (ThreadInterruptedException) {
//只需退出....
}
catch (Exception) {
}
} //此函数是从背景线程调用的
private void UpdateProgress() { //如果需要,重置启动
if (progressBar1.Value == progressBar1.Maximum) {
progressBar1.Value = progressBar1.Minimum ;
} progressBar1.PerformStep() ;
} //启动背景线程以更新进度栏
private void button1_Click(object sender, System.EventArgs e) {
StopThread();
timerThread = new Thread(new ThreadStart(ThreadProc));
timerThread.IsBackground = true;
timerThread.Start();
} //停止背景线程以更新进度栏
private void button2_Click(object sender, System.EventArgs e) {
StopThread();
} //停止背景线程
private void StopThread()
{
if (timerThread != null)
{
timerThread.Interrupt();
timerThread = null;
}
} protected override void Dispose(bool disposing)
{
StopThread();
if (disposing) {
if (components != null) {
components.Dispose();
}
} base.Dispose(disposing);
} private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.SuspendLayout();
//
// button1
//
this.button1.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold);
this.button1.Location = new System.Drawing.Point(164, 74);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(153, 46);
this.button1.TabIndex = 1;
this.button1.Text = "开始!";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold);
this.button2.Location = new System.Drawing.Point(328, 74);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(153, 46);
this.button2.TabIndex = 1;
this.button2.Text = "停止!";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// progressBar1
//
this.progressBar1.Font = new System.Drawing.Font("宋体", 8F, System.Drawing.FontStyle.Bold);
this.progressBar1.Location = new System.Drawing.Point(13, 12);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(448, 46);
this.progressBar1.TabIndex = 2;
this.progressBar1.Text = "开始!";
//
// ThreadMarshal
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(501, 135);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button1,
this.button2,
this.progressBar1});
this.Name = "ThreadMarshal";
this.Text = "已使用设计器生成";
this.ResumeLayout(false); } [STAThread]
public static void Main(string[] args) {
Application.Run(new ThreadMarshal());
}
}
}
{
thread1 = new Thread(...)
thread2 = new Thread(...)
thread1.Start();
thread2.Start();
Thread thread3 = new Thread( new ThreadStart(thProc));
thread3.Start();
.......
}
void ...(...)
{
//此处放...代码
}void thProc()
{
thread1.Join();
thread2.Join();
//两个线程搞定//善后}
thread3不是由thread1和thread创建的,他能知道thread1.Join()、thread2.Join()吗?
这个问题好象比较麻烦,因为thread3很难访问到thread1和thread2的数据,必须要用到回掉函数或者内存映射
只是同步等它执行完毕而已,与谁创建的何干!你和楼主都跑一下如下代码
Thread t1,t2; private void Form1_Load(object sender, System.EventArgs e)
{
t1 = new Thread(new ThreadStart(thWork));
t2 = new Thread(new ThreadStart(thWork));
Thread t3 = new Thread(new ThreadStart(thProc));
t1.Start();
t2.Start();
t3.Start();
} void thWork()
{
for(int i=0;i<100;i++)
{
Console.WriteLine(i.ToString());
Thread.Sleep(50);
}
} void thProc()
{
t1.Join();
t2.Join(); Console.WriteLine("两个都完蛋了!应该我了!!");
}
ServicesToRun =new System.ServiceProcess.ServiceBase[]{new WatchFiles(inipz)};
其中 WatchFiles为服务名称