Re: Intra-Thread communication

From: Ignacio Machin \( .NET/ C# MVP \) ("Ignacio)
Date: 03/16/04


Date: Tue, 16 Mar 2004 09:30:55 -0500

Hi Tom,

First I would suggest you against that many threads, if you do this process
on a directory like "Program Files" you can get poor results.

Anyway, regarding how to comunicate with the main form is very easy,
basically you create a delegate, a method with that signature and then from
the thread you use Control.Invoke of the delegate, where Control is a
control in the form. You only need the control to force the method to be
execute in the correct thread.

here is some code, I do a "similar" thing than you, I copy all the images
from a directory to another directory and rename them in the process. I
chopped the code but all you need is still there.

Cheers,

-- 
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
  // the delegate
  public delegate void ProcessedFileHandler( );
  //the needed variables
  Thread workingThread;
  ProcessedFileHandler procfilehandler;
  //the controls.
  private System.Windows.Forms.ProgressBar progressBar1;
  private System.Windows.Forms.Label FileNameLBL;
  //This goes in the Load event
  procfilehandler = new ProcessedFileHandler( this.UpdateProgressBar);
  //Start the thread
  this.workingThread = new Thread( new ThreadStart( this.ExportImage ));
  this.workingThread.Start();
   //The worker method
  void ExportImage( )
  {
   rapi = new RAPI();
   rapi.Connect(true);
   //pudate the variables, here I use a lock ( not shown )
   lock( this.syslock)
   {
    this.currentpos++;
    this.currentfile = Path.GetFileName(imageURL);
   }
   //Update the interface
   this.progressBar1.Invoke( procfilehandler, null);
   //All we need is a control in the main thread to execute a method on the
main thread.
   //this is another delegate ( not shown ) it's needed for the UI to know
the process is over.
   this.progressBar1.Invoke( new ProcessedFileHandler( this.Done), null);
  }
  //the handler
  void UpdateProgressBar( )
  {
   this.progressBar1.Value++;
   FileNameLBL.Text = currentfile;
   currentLBL.Text = currentpos.ToString();
  }
"Just Tom" <JustTom@TomJust.JustTom.COMmunist.com> wrote in message
news:eQU$P4xCEHA.1072@TK2MSFTNGP09.phx.gbl...
> I have a Winform that will spawn a thread to work on each file within a
> directory.  For each FileSystemInfo in this directory, it will get the
hash
> of that file.  If the FileSystemInfo is infact another directory, I will
> spawn another thread and repeat the whole process.  However I want to keep
> track of which directory is being parsed, so I want to list this in a Text
> box back in the Winform.  Here is my problem.
>
> How do I get a thread to communicate back to the Winform the directory
that
> it is operating upon?  The thread knows nothing about the form's class as
> the thread is not instantiating that class.  Everything I have tried so
far
> does not work.
>
> Can somebody point me in the right direction?
>
>
>
>
>


Relevant Pages

  • Re: Intra-Thread communication
    ... basically you create a delegate, a method with that signature and then from ... control in the form. ... procfilehandler = new ProcessedFileHandler; ... > box back in the Winform. ...
    (microsoft.public.dotnet.framework)
  • Re: Intra-Thread communication
    ... basically you create a delegate, a method with that signature and then from ... control in the form. ... procfilehandler = new ProcessedFileHandler; ... > box back in the Winform. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: advanced question: feasability of using delegates on original obje
    ... why not take a look at some of the "AJAX" ... and adds this to the control render: ... But there's one problem - delegate accumulation. ... objects or session into a state where calling delegates / accessing the ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Threading WinForm
    ... you are calling BeginInvoke, then you should have a corresponding call to EndInvoke. ... UI thread processes the delegate, and in this situation, is the better option, IMO. ... private delegate void AddItemDelegate; ... I have a simple winform with a button, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Delegating Control...
    ... | Thread-Topic: Delegating Control... ... Reset user passwords ... domain user has permission to join 10 clients into domain. ... |> You may want to delegate user/group create, list, view permission to ...
    (microsoft.public.win2000.active_directory)

Loading