[dba-VB] C# - Presenting process status

Shamil Salakhetdinov shamil at smsconsulting.spb.ru
Sun Nov 29 15:33:05 CST 2009


Hi John,

Here is one of the methods I use (there are zillion combinations) - I dunno
where it comes from, did I borrowed it from somewhere or I got it elaborated
myself, is it recommended way or not - but it just works, and I have
"cooked" it for you here from scratch - this is codebehind for Windows Form
named Form1:

using System;
using System.Windows.Forms;

namespace WinFormsSample
{
    public partial class Form1 : Form
    {
        // Create Windows Form Form1 with three controls:
        //
        // 1. Button: cmdTest1 -> cmdTest1_Click
        // 2. Button: cmdTest2 -> cmdTest2_Click
        // 3. TextBox: txtLog (Multiline = true)

        #region Test class
        public class Test
        {
            public delegate void ProgressStatus(string message);

            private static int _id;
            private System.Threading.Thread _thread; 
            public void Run(ProgressStatus progressStatus)
            {
                _testId = ++_id;
                _progressStatus = progressStatus;
  
                System.Threading.ThreadStart ts =
                    new System.Threading.ThreadStart(runThread);
                _thread = new System.Threading.Thread(ts);
                _thread.IsBackground = true;
                _thread.Start();  
            }

            private int _testId;
            private ProgressStatus _progressStatus;
            private void runThread()
            {
                for (int i = 1; i < 10; i++)
                {
                    _progressStatus(string.Format("Test#{0}, cycle#{1}",
_testId, i));
                    System.Threading.Thread.Sleep(500);
                }
            }
        }
        #endregion // Test class

        public Form1()
        {
            InitializeComponent();
        }

        private Test _test1;
        private void cmdTest1_Click(object sender, EventArgs e)
        {
            _test1 = new Test();
            _test1.Run(progressStatus);  
        }

        private Test _test2;
        private void cmdTest2_Click(object sender, EventArgs e)
        {
            _test2 = new Test();
            _test2.Run(progressStatus);  
        }

        private void progressStatus(string message)
        {
            if (this.InvokeRequired)
            {
                object[] args = { message };
                this.Invoke(
                    new Test.ProgressStatus( 
                    progressStatusInThisFormThread),
                      args); 
            }
            else
                progressStatusInThisFormThread(message); 

        }

        private static object _statusMessageOutputLocker = new object(); 
        private void progressStatusInThisFormThread(string message)
        {
            lock (_statusMessageOutputLocker)
            {
                txtLog.Text =
                    String.Format("{0:hh:mm:ss.fff}: {1}", 
                    DateTime.Now,  
                    message) +
                    System.Environment.NewLine + 
                    txtLog.Text; 
            }
        }
    }
}

I will try to post about another one I use in the coming days...

Thank you.

--
Shamil

-----Original Message-----
So how do you folks display status information in a form?
-- 
John W. Colby
www.ColbyConsulting.com





More information about the dba-VB mailing list