Re: Intra-Thread communication
From: Ignacio Machin \( .NET/ C# MVP \) ("Ignacio)
Date: 03/16/04
- Next message: HomeyDaClown: "Visual FoxPro and VB.Net"
- Previous message: Simon Middlemiss: "Re: auto copyrighter.."
- In reply to: Just Tom: "Intra-Thread communication"
- Messages sorted by: [ date ] [ thread ]
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?
>
>
>
>
>
- Next message: HomeyDaClown: "Visual FoxPro and VB.Net"
- Previous message: Simon Middlemiss: "Re: auto copyrighter.."
- In reply to: Just Tom: "Intra-Thread communication"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|