Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / August 2006

Tip: Looking for answers? Try searching our database.

MultiThreaded App always reverts back to UI Thread

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
greener.jay@gmail.com - 29 Aug 2006 16:44 GMT
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);

             }
Patrice - 30 Aug 2006 12:44 GMT
"dothework" is badly named ? This is what updates the UI so you use invoke
so that the UI update is done by the UI thread. So you are back to the UI
thread. Isn't it what you wanted ?

I'm not sure where the actual work lies in this code snippet...

Also it looks like you have an extra level of delegate calls that I'm not
sure is usefull...

You could also try the BackGroundWorker component that already take care of
calling each event on the appropriate thread for you (the event that does
the work is called on the worker thread and the update progress event is
called on  the UI thread).

Also double check that multithreading is actually a win in you particular
case (AFAIK an UI that is very frequently updated by the worker thread is
not a win as the UI thread uses anyway most of its time to respond to worker
thread calls rather than to user interaction).

Signature

Patrice

> 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
[quoted text clipped - 64 lines]
>
>              }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.