Optimal solution for processing some info in a thread

R

roundcrisis

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
 
K

Kevin Spencer

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.

--
HTH,

Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP
 
I

Ignacio Machin \( .NET/ C# MVP \)

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(.....)

}
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,



Kevin Spencer said:
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.
 
R

roundcrisis

Hi,




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.

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
 
P

Peter Duniho

[...]
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
 
P

Peter Duniho

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top