Отладка и диагностирование проблем сопровождения блокировки в.NET

Это - классический способ, которым необходимо сделать это:

using System;
using System.Windows.Forms;
using System.Threading;

namespace Test
{
    public partial class UIThread : Form
    {
        Worker worker;

        Thread workerThread;

        public UIThread()
        {
            InitializeComponent();

            worker = new Worker();
            worker.ProgressChanged += new EventHandler<ProgressChangedArgs>(OnWorkerProgressChanged);
            workerThread = new Thread(new ThreadStart(worker.StartWork));
            workerThread.Start();
        }

        private void OnWorkerProgressChanged(object sender, ProgressChangedArgs e)
        {
            // Cross thread - so you don't get the cross-threading exception
            if (this.InvokeRequired)
            {
                this.BeginInvoke((MethodInvoker)delegate
                {
                    OnWorkerProgressChanged(sender, e);
                });
                return;
            }

            // Change control
            this.label1.Text = e.Progress;
        }
    }

    public class Worker
    {
        public event EventHandler<ProgressChangedArgs> ProgressChanged;

        protected void OnProgressChanged(ProgressChangedArgs e)
        {
            if(ProgressChanged!=null)
            {
                ProgressChanged(this,e);
            }
        }

        public void StartWork()
        {
            Thread.Sleep(100);
            OnProgressChanged(new ProgressChangedArgs("Progress Changed"));
            Thread.Sleep(100);
        }
    }


    public class ProgressChangedArgs : EventArgs
    {
        public string Progress {get;private set;}
        public ProgressChangedArgs(string progress)
        {
            Progress = progress;
        }
    }
}

Ваш рабочий поток имеет событие. Ваш поток UI начинается другой поток, чтобы сделать работу и поднимает трубку то событие рабочего, таким образом, можно отобразить состояние рабочего потока.

Тогда в UI необходимо пересечь потоки для изменения фактического управления... как маркировка или индикатор выполнения.

5
задан LBushkin 28 August 2009 в 20:33
поделиться

3 ответа

Lock convoys are hard to debug in general. Does your code path have sequential lock statements either directly or in branches?

The Total # of Contentions performance counter gives a base estimate of contention in the app.

Also break open a profiler and look. You can also write some perf counters to track down the slow parts of a code path. Also make sure that locks are only being held for as long as absolutely necessary.

Also check out the Windows Performance Tools. I have found these to be extremely useful as you can track down lots of low level problems like abnormal amounts of context switching.

4
ответ дан 14 December 2019 в 04:43
поделиться

A good place to start is by having a look at the Lock and Thread performance counters. Out of interesting what exactly are you locking for in your Web app? Locking in most ASP.NET applications isn't common.

2
ответ дан 14 December 2019 в 04:43
поделиться

I can't provide much insight into the diagnostics, but if you find proof to back up your assumption then you might be interested in System.Threading.ReaderWriterLockSlim which allows for concurrent reads, but prevents concurrent writes.

2
ответ дан 14 December 2019 в 04:43
поделиться
Другие вопросы по тегам:

Похожие вопросы: