线程同步问题

KissXYL 2007-02-11 05:50:24
线程A一直做一件事情。主界面线程如果要访问A对象中某个数据,要停止A线程的更新,避免我要读取的数据被A线程删除了。我做了个例子。但发现例子有问题。请您帮我调试一下,新建工程。WindowsApplication
以下是Form3.CS
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace LearnProject
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}

private void Form3_Load(object sender, EventArgs e)
{
MyUpdate = new MyUpdateDelegate(UpdateText);
System.Threading.Thread th = new System.Threading.Thread(new System.Threading.ThreadStart(MyThreadProc));
ThreadRunEnable = true;
th.Start();
}

private volatile bool ThreadRunEnable = false;
private void MyThreadProc()
{
const string strConst = "This is a test sentence.";
int i = 0,istrLen = strConst.Length;
try
{
while (ThreadRunEnable && i < int.MaxValue)
{
System.Threading.Thread.Sleep(80);
Monitor.Enter(Sync);
this.Invoke(MyUpdate, new object[] { strConst[i % istrLen].ToString() });
Monitor.Exit(Sync);
i++;
Application.DoEvents();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.StackTrace+"\r\n"+ex.Message);
return;
}
//MessageBox.Show("Thread abort.");
}

private MyUpdateDelegate MyUpdate;
private delegate void MyUpdateDelegate(string strText);
public void UpdateText(string strText)
{
textBox1.AppendText(strText);
textBox1.Update();
}

private void button1_Click(object sender, EventArgs e)
{
Monitor.Enter(Sync);
textBox1.AppendText("\r\n");
Monitor.Exit(Sync);
}

private void button2_Click(object sender, EventArgs e)
{
//m_Mutex.WaitOne();
ThreadRunEnable = false;
//m_Mutex.ReleaseMutex();
}

private volatile object Sync = new object();
//private volatile System.Threading.AutoResetEvent eventMutex = new System.Threading.AutoResetEvent(false);
private void Form3_FormClosing(object sender, FormClosingEventArgs e)
{
//m_Mutex.WaitOne();
ThreadRunEnable = false;
//m_Mutex.ReleaseMutex();
//m_Mutex.WaitOne();
//m_Mutex.ReleaseMutex();
}

private void button3_Click(object sender, EventArgs e)
{
System.Threading.Thread th = new System.Threading.Thread(new System.Threading.ThreadStart(MyThreadProc));
ThreadRunEnable = true;
th.Start();
}
}
}
...全文
216 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
灰太狼 2007-02-12
  • 打赏
  • 举报
回复
参见
http://blog.csdn.net/tjvictor/archive/2007/01/20/1488290.aspx
grooving 2007-02-12
  • 打赏
  • 举报
回复
if(control.invokerequired)
control.begininvoke

lock(control)
KissXYL 2007-02-12
  • 打赏
  • 举报
回复
thank you
up
jxf654 2007-02-12
  • 打赏
  • 举报
回复
up
KissXYL 2007-02-11
  • 打赏
  • 举报
回复
复制到你的工程,替换即可。如果可以,请和我联系:
QQ:32610303
msn:simtel_006@hotmail.com

反复执行button2按钮事件,会死锁。请教如何解决这个问题呢。
KissXYL 2007-02-11
  • 打赏
  • 举报
回复
以下是界面:
namespace LearnProject
{
partial class Form3
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows 窗体设计器生成的代码

/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(0, 0);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(295, 232);
this.textBox1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(1, 243);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(236, 23);
this.button1.TabIndex = 1;
this.button1.Text = "Pause and write immediately";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(266, 243);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(25, 23);
this.button2.TabIndex = 2;
this.button2.Text = "X";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(239, 243);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(25, 23);
this.button3.TabIndex = 2;
this.button3.Text = "N";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// Form3
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Name = "Form3";
this.Text = "Form3";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form3_FormClosing);
this.Load += new System.EventHandler(this.Form3_Load);
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
}
}

110,535

社区成员

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

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

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