G
greener.jay
I'm a C# newb and am trying to write a multithreaded app to free up my
UI thread so that it can repaint the screen etc. The application does
spawn another thread (I've verified this with Performance Monitor and
by naming the different threads I am working with), which spawns
another thread which turns control . However, when I issue the invoke
statement it reverts back to the original thread and my UI freezes
until the work is completed. I think I have added additional steps for
nothing and I am obviously not understanding something about the worker
thread. Here is a snippet of my code.
public Form1()
{
InitializeComponent();
Thread.CurrentThread.Name = "Main Thread";
MessageBox.Show(Thread.CurrentThread.Name); (This returns "Main
Thread")
logFile = Path.Combine(@servername.Trim(), @"path to
logfile");
Thread invoker = new Thread(logFileInvoker);
invoker.Name = "Log File Invoker Thread";
invoker.Start();
public void logFileInvoker()
{
MessageBox.Show(Thread.CurrentThread.Name); (This returns
"logFileInvoker")
readLogFile(logFile);
}
delegate void readLogDelegate(string parameter);
private void readLogFile(string logFile)
{
MessageBox.Show(Thread.CurrentThread.Name); (This returns
"logFileInvoker")
readLogDelegate readlog = new readLogDelegate(dothework);
readlog.BeginInvoke(logFile, null, null);
}
private void dothework(string logFile)
{
MessageBox.Show(Thread.CurrentThread.Name); (The first time
through this returns empty because I never set this new thread name,
the second time through it is back to my inital "Main Thread")
if (this.listView1.InvokeRequired) (This returns True the
first time through but not the second)
{
this.Invoke(new readLogDelegate(dothework), new object[]
{ logFile });
}
else
{
ListView ListView1 = new ListView();
listView1.Clear();
listView1.View = View.Details;
listView1.Columns.Add("", -1,
HorizontalAlignment.Left);
listView1.Columns.Add("", 10000,
HorizontalAlignment.Left);
}
UI thread so that it can repaint the screen etc. The application does
spawn another thread (I've verified this with Performance Monitor and
by naming the different threads I am working with), which spawns
another thread which turns control . However, when I issue the invoke
statement it reverts back to the original thread and my UI freezes
until the work is completed. I think I have added additional steps for
nothing and I am obviously not understanding something about the worker
thread. Here is a snippet of my code.
public Form1()
{
InitializeComponent();
Thread.CurrentThread.Name = "Main Thread";
MessageBox.Show(Thread.CurrentThread.Name); (This returns "Main
Thread")
logFile = Path.Combine(@servername.Trim(), @"path to
logfile");
Thread invoker = new Thread(logFileInvoker);
invoker.Name = "Log File Invoker Thread";
invoker.Start();
public void logFileInvoker()
{
MessageBox.Show(Thread.CurrentThread.Name); (This returns
"logFileInvoker")
readLogFile(logFile);
}
delegate void readLogDelegate(string parameter);
private void readLogFile(string logFile)
{
MessageBox.Show(Thread.CurrentThread.Name); (This returns
"logFileInvoker")
readLogDelegate readlog = new readLogDelegate(dothework);
readlog.BeginInvoke(logFile, null, null);
}
private void dothework(string logFile)
{
MessageBox.Show(Thread.CurrentThread.Name); (The first time
through this returns empty because I never set this new thread name,
the second time through it is back to my inital "Main Thread")
if (this.listView1.InvokeRequired) (This returns True the
first time through but not the second)
{
this.Invoke(new readLogDelegate(dothework), new object[]
{ logFile });
}
else
{
ListView ListView1 = new ListView();
listView1.Clear();
listView1.View = View.Details;
listView1.Columns.Add("", -1,
HorizontalAlignment.Left);
listView1.Columns.Add("", 10000,
HorizontalAlignment.Left);
}