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 / Languages / C# / January 2008

Tip: Looking for answers? Try searching our database.

Optimal solution for processing some info in a thread

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
roundcrisis - 11 Jan 2008 11:30 GMT
hi there:

this is the situation:

I have a function in a class the processing of this takes some time,
this function can either be called from a winform or a command line
with several parameters. I want to display some sort of progress bar
what is the optimal way to do this
i e been reading about background worker beignInvoque,etc but its not
clear to me how and why to use them
any pointers?

Thanks
Kevin Spencer - 11 Jan 2008 12:22 GMT
The class containing the function can implement an Event, which the function
can call whenever it wants to report something during execution. The client
application can subscribe to the Event and handle it in any way desired.

Signature

HTH,

Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP

> hi there:
>
[quoted text clipped - 9 lines]
>
> Thanks
Ignacio Machin ( .NET/ C# MVP ) - 11 Jan 2008 14:35 GMT
Hi,

> The class containing the function can implement an Event, which the
> function can call whenever it wants to report something during execution.
> The client application can subscribe to the Event and handle it in any way
> desired.

An event is not the right solution, the event will be executed in the
calling thread, not in the main thread. That is why you need to use
Control.Invoke cause its assure you that the call will be executed in the
Control's creation thread.

Signature

Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.

roundcrisis - 11 Jan 2008 17:49 GMT
On Jan 11, 2:35 pm, "Ignacio Machin \( .NET/ C# MVP \)" <machin TA
laceupsolutions.com> wrote:
> Hi,
>
[quoted text clipped - 18 lines]
> Ignacio Machinhttp://www.laceupsolutions.com
> Mobile & warehouse Solutions.

i read somewhere that i should use begin invoke over invoke
does this make sense and if so why and where should i use end invoke?

thanks
Peter Duniho - 11 Jan 2008 18:18 GMT
> [...]
> i read somewhere that i should use begin invoke over invoke
> does this make sense and if so why

You can use either.  Invoke() is useful if you need or want to wait on the  
return value for the delegate being executed, or if you want to take  
advantage of the inherent synchronization Invoke() provides.  
BeginInvoke() is useful if you don't care about the result of the  
invocation and want the invoking thread to continue with its processing.  
But using BeginInvoke() you will have to ensure synchronization between  
any data structures used both in the invoked method and the invoking  
thread, if any.

> and where should i use end invoke?

For Control.BeginInvoke() _only_, the unofficial information is that  
calling Control.EndInvoke() is not required.  For all other examples of a  
BeginXXX() method, you must call the corresponding EndXXX() method after  
the asynchronous operation has completed.

Using Control.BeginInvoke(), you might still want to call  
Control.EndInvoke() if you want to retrieve the return value from the  
method that was invoked.  In that case, you can call EndInvoke() whenever  
you want, but be aware that if the invoked method hasn't completed yet,  
EndInvoke() will wait and not return until the invoked method is done.

Pete
Peter Duniho - 11 Jan 2008 18:21 GMT
> An event is not the right solution, the event will be executed in the
> calling thread, not in the main thread.

Events and cross-thread issues are not mutually exclusive.  I use events  
for thread signaling on a regular basis and it works fine.

Of course, you do need to ensure that you use Invoke() or BeginInvoke()  
when calling GUI method.  If the event-raising code is in a  
Control-derived class, this is easy enough to do all the time, but even if  
not it's simple enough to do so from an event handler.

Pete
Ignacio Machin ( .NET/ C# MVP ) - 11 Jan 2008 14:34 GMT
Hi,

You can create a new Thread and do the work there. This thread can report
its progress to the UI thread (in the case of win apps) or simply use
Console.Write in the case of a console app.

In a win app you have to pass a reference to a Control in the form. Then
the thread can call Invoke on it:. The thread can decide which way to
commiunicate based on tis control:

void m1(...... , Control c){
........
if ( c == null )
 Console.Write (".");
else
 Control.Invoke(.....)

}

Signature

Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.

> hi there:
>
[quoted text clipped - 9 lines]
>
> Thanks

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.