Re: MultiThreaded App always reverts back to UI Thread

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



greener.jay@xxxxxxxxx wrote:
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 that in turn issues the invoke statement. This results
in the program reverting 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
concept of the worker thread. Here is a snippet of my code.

Not really enough for me to say categorically what the problem is, but if we look here:

[all fine so far - we have created a new thread to run this method]
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);

}

So dothework will be run on the new thread, which is correct, because this is the worker thread, and we want to do the work :)


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);

}

OK let's hold it right here. Why is the *worker* thread going to the UI at this time? The code suggested to me that the 'work' was going to involve reading a log file, but there isn't any of that here. You have the concept of going to the UI thread with InvokeRequired and Invoke when you want to update the UI, but that isn't what you should be doing here - you should be doing 'the work' of reading the log file, and *when that is done* (or when you have useful intermediate results), you can go to the UI.

Have a method which takes actual results (the readings from the log file, I suppose) and puts them in the UI. Put appropriate InvokeRequired checks to make sure it only runs on the UI thread. Then call this method from dothework when you actually have results.


--
Larry Lard
larrylard@xxxxxxxxxxxxxx
The address is real, but unread - please reply to the group
For VB and C# questions - tell us which version
.



Relevant Pages

  • Re: corpse timer
    ... Finding them the first time I came back was hard ... i used to bard the titans in the valley and hit up the hell spawn in depise ... a herd of magic users causing instant death. ... A properly designed system would give you the time to at least think ...
    (rec.games.computer.ultima.online)
  • Re: MultiThreaded App always reverts back to UI Thread
    ... This is what updates the UI so you use invoke ... private void readLogFile(string logFile) ... MessageBox.Show; (The first time ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: Delegates Subscribe and Unscubscribe
    ... The first time this form is invoked everything works fine. ... The second time I ... invoke the form it seems theat NameChanged is still holdng a reference to ... event when disposing of an object? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Family Circus in LA Times, 04/06 - it was the 2006 strip
    ... Recklessly refusing to invoke the Fifth Amendment, on Tue, 10 Apr 2007 ... said it was the first time that it occurred to her how ingrained the ...
    (rec.arts.comics.strips)
  • Delegates Subscribe and Unscubscribe
    ... I have a usercontrol which has an event called NameChanged; ... The first time this form is invoked everything works fine. ... invoke the form it seems theat NameChanged is still holdng a reference to ... event when disposing of an object? ...
    (microsoft.public.dotnet.languages.csharp)