CSharp的Lock相关处理逻辑

2016-01-14 10:39:15  访问(4582) 赞(0) 踩(0)


相关下载:eKing.StudyLock[code]  eKing.StudyLock[release]     


  • 测试代码显示:

    lock锁定下,one和two不能同时执行

  • 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;
    using eKing.StudyLock.Classes;
    
    namespace eKing.StudyLock
    {
        public partial class MainForm : Form
        {
            public MainForm()
            {
                InitializeComponent();
            }
    
            protected bool isStopOne = false;
            protected bool isStopTwo = false; 
    
            /// <summary>  
            /// 启动线程  
            /// </summary>  
            protected void MainThreadStart(string tName)
            {
                Thread execThread = new Thread(new ThreadStart(ExecThreadRunProcess));
                // 设为后台线程 //  
                execThread.IsBackground = true;
                execThread.Name = tName;
    
                if (tName == "one")
                    isStopOne = false;
                else
                    isStopTwo = false;
    
                execThread.Start();
    
    
            }
    
            /// <summary>  
            ///   
            /// </summary>  
            protected void ExecThreadRunProcess()
            {
                Thread ch = Thread.CurrentThread;
    
                string strName = "";
    
                if (ch != null)
                    strName = ch.Name;
    
                int idx = 0;
    
                while (true)
                {
                    ++idx;
                    if (strName == "one")
                    {
                        lock (Class1.i)
                        {
                            LockFn(strName, idx);
                        }
                    }
                    else
                    {
                        lock (Class1.i)
                        {
                            LockFn2(strName, idx);
                        }
                    }
    
    
                    System.Threading.Thread.Sleep(1);
    
                    if (isStopOne && strName == "one")
                        break;
    
                    if (isStopTwo && strName == "two")
                        break;
    
                }
    
                AddText(strName + ":end [" + DateTime.Now.ToString("HH:mm:ss") + "]"
                        + System.Environment.NewLine);
    
            }
    
            /// <summary>  
            /// 通过委托实现线程对控件RichTextBox设置值  
            /// </summary>  
            /// <param name="str"></param>  
            delegate void AddTextCallback(string str);
    
            /// <summary>  
            /// 通过委托实现线程对控件RichTextBox设置值  
            /// </summary>  
            /// <param name="str"></param>  
            protected void AddText(string str)
            {
                if (this.InvokeRequired)
                {
                    AddTextCallback d = new AddTextCallback(AddText);
    
                    this.Invoke(d, new object[] { str });
    
                    return;
                }
    
                RichTextBox rtb = richTextBox_Output;
    
                rtb.AppendText(str);
                rtb.Focus();
                rtb.Select(richTextBox_Output.TextLength, 0);
                rtb.ScrollToCaret();
            }  
       
    
            /// <summary>
            /// 
            /// </summary>
            /// <returns></returns>
            protected string ToDT()
            {
                return "[" + DateTime.Now.ToString("HH:mm:ss") + "]";
            }
    
            protected void LockFn(string str, int idx)
            {
    
                AddText(idx.ToString() + ".." + str + "开始Lock" + ToDT() + '\r' + '\n');
    
                System.Threading.Thread.Sleep(3000);
     
                AddText(str + "执行1" + ToDT() + '\r' + '\n');
    
                System.Threading.Thread.Sleep(3000);             
    
                AddText(str + "结束Lock" + ToDT() + '\r' + '\n');
            }
    
            protected void LockFn2(string str, int idx)
            {
    
                AddText(idx.ToString() + ".." + str + "开始Lock" + ToDT() + '\r' + '\n');
    
                System.Threading.Thread.Sleep(3000);
    
    
    
                AddText(str + "执行1" + ToDT() + '\r' + '\n');
    
                System.Threading.Thread.Sleep(1000);
    
                AddText(str + "执行2" + ToDT() + '\r' + '\n');
    
                System.Threading.Thread.Sleep(1000);
    
                AddText(str + "执行3" + ToDT() + '\r' + '\n');
    
                System.Threading.Thread.Sleep(1000);
    
                AddText(str + "结束Lock" + ToDT() + '\r' + '\n');
            }
     
    
            private void toolStripButton_启动线程1_Click(object sender, EventArgs e)
            {
    
                try
                {
                    MainThreadStart("one");
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.Message);
                }
    
            }
    
            private void toolStripButton_停止线程1_Click(object sender, EventArgs e)
            {
                isStopOne = true;
            }
    
            private void toolStripButton_启动线程2_Click(object sender, EventArgs e)
            {
    
                try
                {
                    MainThreadStart("two");
                }
                catch (Exception err)
                {
                    MessageBox.Show(err.Message);
                }
    
            }
    
            private void toolStripButton_停止线程2_Click(object sender, EventArgs e)
            {
                isStopTwo = true;
            }
        }
    }
    
    

标签:CSharp的Lock相关处理逻辑 

上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)