D
Dmitri Lazarev
I have this code:
using System;
namespace Test
{
public class Control : System.Windows.Forms.Control
{
public Control()
{
}
}
public class Application
{
[STAThread]
static void Main()
{
control.CreateControl();
AnotherThreadCall().Join();
ThisThreadCall();
AnotherThreadCall().Join();
}
static public Control control = new Control();
static public System.Threading.Thread AnotherThreadCall()
{
System.Threading.Thread thread = new
System.Threading.Thread(new System.Threading.ThreadStart(DoSomething));
thread.Start();
return thread;
}
static public void ThisThreadCall()
{
DoSomething();
}
static public void DoSomething()
{
if ( control.InvokeRequired )
{
System.Diagnostics.Trace.WriteLine("Invoke required");
System.Diagnostics.Trace.Flush();
// System.Windows.Forms.MessageBox.Show("Invoke required");
}
else
{
System.Diagnostics.Trace.WriteLine("Invoke not required");
System.Diagnostics.Trace.Flush();
// System.Windows.Forms.MessageBox.Show("Invoke not
required");
}
}
}
}
after executing this code in the output will be
Invoke required
Invoke not required
Invoke required
but if i remove comments before MessageBox.Show output will be:
Invoke required
Invoke not required
Invoke not required
and after this InvokeRequired for control will be always false.
if i replace MessageBox dialog with my own window InvokeRequired works fine.
What is it?
WBR, Lazarev Dmitri
using System;
namespace Test
{
public class Control : System.Windows.Forms.Control
{
public Control()
{
}
}
public class Application
{
[STAThread]
static void Main()
{
control.CreateControl();
AnotherThreadCall().Join();
ThisThreadCall();
AnotherThreadCall().Join();
}
static public Control control = new Control();
static public System.Threading.Thread AnotherThreadCall()
{
System.Threading.Thread thread = new
System.Threading.Thread(new System.Threading.ThreadStart(DoSomething));
thread.Start();
return thread;
}
static public void ThisThreadCall()
{
DoSomething();
}
static public void DoSomething()
{
if ( control.InvokeRequired )
{
System.Diagnostics.Trace.WriteLine("Invoke required");
System.Diagnostics.Trace.Flush();
// System.Windows.Forms.MessageBox.Show("Invoke required");
}
else
{
System.Diagnostics.Trace.WriteLine("Invoke not required");
System.Diagnostics.Trace.Flush();
// System.Windows.Forms.MessageBox.Show("Invoke not
required");
}
}
}
}
after executing this code in the output will be
Invoke required
Invoke not required
Invoke required
but if i remove comments before MessageBox.Show output will be:
Invoke required
Invoke not required
Invoke not required
and after this InvokeRequired for control will be always false.
if i replace MessageBox dialog with my own window InvokeRequired works fine.
What is it?
WBR, Lazarev Dmitri