M
mick
Understand all these on a *very* basic level but combine them and I`m at
a complete loss.
I was looking into how to access controls from another thread so went
looking
for an example and came across this but I`m struggling to follow what is
going
on. From http://www.osix.net/modules/article/?id=832 the odd label name has
been altered.
delegate void SetClockCallBack(string name);
//smart method to set labels`s text
public void SetClock(string name)
{
if(this.InvokeRequired)
{
SetClockCallBack callback = new SetClockCallBack(SetClock);
this.Invoke(callback,datetime);
}
else
{
this.lblClock.Text = datetime;
}
private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
Thread t = new Thread(new ThreadStart(ClockRun));
t.IsBackground = true;
t.Start();
}
private void ClockRun()
{
try
{
while (true)
{
// we'll just call the SetClock method here, no invoke
this.SetClock(DateTime.Now.ToString());
Thread.Sleep(1000);
}
}
catch (Exception ex)
{
// nothing is done here
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
As I see it - in the button.Click event a new thread is created that will
run the ClockRun() method. This in turn calls SetClock() in which we
come to this -
if(this.InvokeRequired)
{
SetClockCallBack callback = new SetClockCallBack(SetClock);
this.Invoke(callback,datetime);
}
If you had to explain this to an idiot how would you do it? It looks to me
that SetClock is called again through Invoke via the delegate. A very
childlike
explanation or a better, smaller easier to understand example would help
a complete loss.
I was looking into how to access controls from another thread so went
looking
for an example and came across this but I`m struggling to follow what is
going
on. From http://www.osix.net/modules/article/?id=832 the odd label name has
been altered.
delegate void SetClockCallBack(string name);
//smart method to set labels`s text
public void SetClock(string name)
{
if(this.InvokeRequired)
{
SetClockCallBack callback = new SetClockCallBack(SetClock);
this.Invoke(callback,datetime);
}
else
{
this.lblClock.Text = datetime;
}
private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
Thread t = new Thread(new ThreadStart(ClockRun));
t.IsBackground = true;
t.Start();
}
private void ClockRun()
{
try
{
while (true)
{
// we'll just call the SetClock method here, no invoke
this.SetClock(DateTime.Now.ToString());
Thread.Sleep(1000);
}
}
catch (Exception ex)
{
// nothing is done here
System.Diagnostics.Debug.WriteLine(ex.ToString());
}
}
As I see it - in the button.Click event a new thread is created that will
run the ClockRun() method. This in turn calls SetClock() in which we
come to this -
if(this.InvokeRequired)
{
SetClockCallBack callback = new SetClockCallBack(SetClock);
this.Invoke(callback,datetime);
}
If you had to explain this to an idiot how would you do it? It looks to me
that SetClock is called again through Invoke via the delegate. A very
childlike
explanation or a better, smaller easier to understand example would help