0

我正在尝试执行一个简单的代码。我希望结果 RWL 工作将显示在消息框中。当我按下按钮时,我确实等待文本框中发生事件。当文本框事件发生时,我需要处理事件的结果。我正在尝试使用读取锁定机制。但是它不起作用。是机制有什么问题吗?

using System;
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.Threading;

namespace ReadWriteLockTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        static ReaderWriterLock rwl = new ReaderWriterLock();
        static int resource = 0;

        private void button1_Click(object sender, EventArgs e)
        {
            scanner();
        }

        private void scanner()
        {
            int falg = 0;
            int i = 0;

            while (true)
            {
                Thread.Sleep(5000);
                try
                {
                    rwl.AcquireReaderLock(100);
                    try
                    {
                        Console.WriteLine(i);
                         i++;

                        if (resource == 1)
                            falg = 1;
                    }
                    finally
                    {
                        rwl.ReleaseReaderLock();
                    }
                }
                catch (ApplicationException)
                {

                }
                if (falg == 1)
                    break;
            }

            MessageBox.Show("RWL WORKS");

        }

        private DateTime CharReadTime;
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
           CharReadTime = DateTime.Now;
            if (!timer1.Enabled)
                timer1.Start();
        }

        int j = 0;
        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            timer1.Interval = 1000;
        }

        private void timer1_Tick_1(object sender, EventArgs e)
        {
            const int timeout = 3;

            Console.WriteLine("j =" + j);
            j++;
            if ((DateTime.Now - CharReadTime).Seconds < timeout)
                return;

            if (String.Compare(textBox1.Text, "") == 0)
                return;

            try
            {
                rwl.AcquireWriterLock(100);
                try
                {
                    resource = 1;
                }
                finally
                {
                    rwl.ReleaseWriterLock();
                }
            }
            catch (ApplicationException)
            {
            }

        }


    }
}
4

2 回答 2

3

您从工具箱中拖动的常规计时器在 UI 线程中运行,因此 timer1_tick 永远不会与 button1_click 同时运行。system.timers.timer 从另一个线程运行,如果是那种计时器,您将需要锁。

我建议使用较新的 readerwriterlockslim,它更快且错误更少,请参阅https://issues.apache.org/jira/browse/LOG4NET-232http://msdn.microsoft.com/en-us/library/ system.threading.readerwriterlockslim.aspx

于 2012-07-21T21:20:38.100 回答
0

添加更多细节。System.Windows.Forms.Timer(唯一具有 Tick 事件的 Timer 类,您可以在 WinForms 中访问而无需添加 WPF 程序集)在 UI 线程上运行 tick 事件处理程序。在扫描仪方法运行时,UI 线程很忙,无法调用 Tick 事件处理程序。这意味着您永远无法在使用您详细说明的代码获得写入器锁的同时获得读取器锁。

也许如果你详细说明你希望完成什么,有人可以提供一些建议。

于 2012-07-21T22:14:54.517 回答