473,395 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Optimal solution for processing some info in a thread

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
Jan 11 '08 #1
6 1304
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

"roundcrisis" <ro*********@gmail.comwrote in message
news:c5**********************************@k2g2000h se.googlegroups.com...
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

Jan 11 '08 #2
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(.....)

}

--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
"roundcrisis" <ro*********@gmail.comwrote in message
news:c5**********************************@k2g2000h se.googlegroups.com...
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

Jan 11 '08 #3
Hi,

"Kevin Spencer" <un**********@nothinks.comwrote in message
news:Oo**************@TK2MSFTNGP03.phx.gbl...
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.
--
HTH,

Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP
--
Ignacio Machin
http://www.laceupsolutions.com
Mobile & warehouse Solutions.
Jan 11 '08 #4
On Jan 11, 2:35*pm, "Ignacio Machin \( .NET/ C# MVP \)" <machin TA
laceupsolutions.comwrote:
Hi,

"Kevin Spencer" <unclechut...@nothinks.comwrote in message

news:Oo**************@TK2MSFTNGP03.phx.gbl...
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.
--
HTH,
Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP

--
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
Jan 11 '08 #5
On Fri, 11 Jan 2008 09:49:31 -0800, roundcrisis <ro*********@gmail.com>
wrote:
[...]
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
Jan 11 '08 #6
On Fri, 11 Jan 2008 06:35:51 -0800, Ignacio Machin ( .NET/ C# MVP )
<machin TA <"laceupsolutions.com>"wrote:
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
Jan 11 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Prashanth Ellina | last post by:
Hi, I have used the low-level thread module to write a multi-threaded app. tid = thread.start_new_thread(process, ()) tid is an integer thread ident. the thread takes around 10 seconds to...
5
by: me | last post by:
I have a Class Library that contains a Form and several helper classes. A thread gets created that performs processing of data behind the scenes and the Form never gets displayed (it is for debug...
0
by: me | last post by:
I have a Class Library that contains a Form and several helper classes. A thread gets created that performs processing of data behind the scenes and the Form never gets displayed (it is for debug...
8
by: Raed Sawalha | last post by:
I have form with progress bar ,when application begin processing and progress bar moving if I minimized the form and try to restore it ,it is not showing until the processing completed ,how can I...
6
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing...
0
by: techie | last post by:
List of Solution manuals: Engineering Circuit Analysis 6Ed - Hayt Solutions Manual Norbury - Solutions manual for mechanics and thermodynamics Physics For Scientists And Engineers - Solution...
10
by: spam_list | last post by:
Hi I am deciding how to animate some numerical data. So far, I have kept the graphics and science(number crunching) codes separate which I like. The main issue confronting me is how to handle...
1
by: almurph | last post by:
Folks, I want to modify the Needleman Wunsch algorithm (pseudo-code here: http://en.wikipedia.org/wiki/Needleman-Wunsch_algorithm) so that it produces more than the optimal solution. What I...
5
by: Christian Schlemmer | last post by:
Hi, in a webapplication, after a submit a page is called which is processing images uploaded by the user. currently after submit a status bar will be visible (the page for the image processing...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.