Shamil Salakhetdinov
shamil at smsconsulting.spb.ru
Sun Nov 29 15:51:48 CST 2009
OK, here is another sample more close to VB6/VBA WithEvents approach:
using System;
using System.Windows.Forms;
namespace WinFormsSample
{
public partial class Form2 : Form
{
// Create Windows Form Form2 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 class ProgressStatusEventArgs : EventArgs
{
public string Message { get; set; }
}
public event EventHandler<ProgressStatusEventArgs>
ProgressStatus;
private static int _id;
private System.Threading.Thread _thread;
public void Run()
{
_testId = ++_id;
System.Threading.ThreadStart ts =
new System.Threading.ThreadStart(runThread);
_thread = new System.Threading.Thread(ts);
_thread.IsBackground = true;
_thread.Start();
}
private int _testId;
private void runThread()
{
for (int i = 1; i < 10; i++)
{
string message = string.Format("Test#{0}, cycle#{1}",
_testId, i);
ProgressStatusEventArgs e = new
ProgressStatusEventArgs();
e.Message = message;
// raise event
if (ProgressStatus != null)
ProgressStatus(this, e);
System.Threading.Thread.Sleep(500);
}
}
}
#endregion // Test class
public Form2()
{
InitializeComponent();
}
private Test _test1;
private void cmdTest1_Click(object sender, EventArgs e)
{
_test1 = new Test();
_test1.ProgressStatus += new
EventHandler<Test.ProgressStatusEventArgs>(progressStatus);
_test1.Run();
}
private Test _test2;
private void cmdTest2_Click(object sender, EventArgs e)
{
_test2 = new Test();
_test2.ProgressStatus += new
EventHandler<Test.ProgressStatusEventArgs>(progressStatus);
_test2.Run();
}
public delegate void ProgressStatusDelegate(string message);
private void progressStatus(object sender,
Test.ProgressStatusEventArgs e)
{
if (this.InvokeRequired)
{
object[] args = { e.Message };
this.Invoke(
new ProgressStatusDelegate(
progressStatusInThisFormThread),
args);
}
else
progressStatusInThisFormThread(e.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;
}
}
}
}
-----Original Message-----
From: dba-vb-bounces at databaseadvisors.com
[mailto:dba-vb-bounces at databaseadvisors.com] On Behalf Of Shamil
Salakhetdinov
Sent: Monday, November 30, 2009 12:33 AM
To: 'Discussion concerning Visual Basic and related programming issues.'
Subject: Re: [dba-VB] C# - Presenting process status
<<<
I will try to post about another one I use in the coming days...
>>>