|
You could call the Thread.Join[^] method on each thread you start, this will block the calling thread until all threads are finished.
regards
modified 12-Sep-18 21:01pm.
|
|
|
|
|
My standard model for multithreading (in a windows app) is to create a a wait form. You can put a progress bar or a label or whatever you want to use. For my threads I use the System.ComponentModel.BackgroundWorker . I'll briefely explain the basics.
My code will create the BackgroundWorker and set up the method it will need to run.
BackgroundWorker myWorker = new BackgroundWorker();
myWorker.DoWork += new DoWorkEventHandler(myWorker_DoWork);
The method passed in looks like this
private void myWorker_DoWork(object sender, DoWorkEventArgs e)
{
}
My WaitForm accepts the BackgroundWorker as a parameter to the constructor.
My WaitForm accepts the background worker as an input parameter. You can set up the worker in the WaitForm to handle progress changed events to modify the message and even a progress bar in the wait form. Also, you will want to set the RunWorkerCompleted event and make it close the WaitForm.
Then, append this below the first block of code.
WaitForm waiting = new WaitForm(myWorker);
waiting.ShowDialog();
By using ShowDialog you are forcing the current thread to wait until the dialog is done. Because the worker is working the dialog won't close until myWorker_DoWork completes.
Also, this is another good approach which I have used on a few applications.
Broken Bokken
You can't carry out a ninja-style assasination dressed as an astronaut. It's the luminous fabric; too visible. - Tripod
The story of your fighting is a poem of two words: YOU SUCK.
|
|
|
|
|
This is not what I want to do.
Im starting up some slow remote task async and I need the result from all of them before the method can return its result.
So Im not asking how to show forms or anything, just whats the correct way to wait for the result of all of them while still inside the method that fired up the async tasks.
|
|
|
|
|
If you block execution of the calling method right after you make the thread. My question to you is: Why did you make it another thread?
-Spacix
All your skynet questions[ ^] belong to solved
I dislike the black-and-white voting system on questions/answers.
|
|
|
|
|
Im firing up multiple async tasks, so its not just one thread.
its multiple remote services that are called async
and I need to wait for all of them to finish so I can pick up the result and process it and then finally return the processed result from my method.
|
|
|
|
|
Greeeg's answer of Join is what I would suggest you look into.
The Join method basically waits until other threads are finished before resuming executin on the current thread
Continuous effort - not strength or intelligence - is the key to unlocking our potential.(Winston Churchill)
|
|
|
|
|
If you go with a ISyncResult object with a delegate called through begineinvoke then you can use waitone and do the same thing...
There are multiple ways to do what the OP asked, which method to use is mostly based off of the context of what you are doing.
-Spacix
All your skynet questions[ ^] belong to solved
I dislike the black-and-white voting system on questions/answers.
|
|
|
|
|
I have three threads that are trying to write events to a single event log -- with each thread being a separate source. Each thread creates an instance of the EventLog and uses eventLog.WriteLine() to post the event. In initialization each thread checks to see if its' source name is registered to the log. If it not, it registers itself. Then each thread creates an instance of the eventlog passing the logname and source name, after which a success message is posted to the EventLog.
The first thread establishes itself and its' events are getting logged without problems, but the other two threads are not getting their events written.
Any help would be appreciated.
|
|
|
|
|
At what point is it failing?
1.
LongRange.Shooter wrote: each thread checks to see if its' source name is registered to the log
2.
LongRange.Shooter wrote: If it not, it registers itself
3.
LongRange.Shooter wrote: a success message is posted to the EventLog
4.
LongRange.Shooter wrote: thread creates an instance of the eventlog passing the logname and source name
Working through the above list should help solve your problem.
You obviously have it coded correctly in the first thread - have you tried altering/controlling the order of these threads to check if the code is functional in the other two - i.e make the second tread do it's stuff first?
Just seen the thread below! It looks like you might have found the root of the problem.
Dave
modified on Wednesday, May 7, 2008 3:03 PM
|
|
|
|
|
I am trying to automate Excel from within my C# application, and am receiving the above error.
I have added a reference in my project to the Microsoft Excel 12.0 object library which adds the Microsoft.Office.Core and Microsoft.Office.Interop.Excel namespaces (but strangely not the the Excel namespace - this is is only added when I add a reference to the Microsoft Excel 5.0 object library).
I have downloaded the latest Primary Interop Assemblies (PIA) for Microsoft Office from the MS download site, and added the following code to my program as well:
using Excel;
I still get the error. I have run out of ideas!!
Any help much appreciated.
|
|
|
|
|
Instead of "using Excel;" you should have "Microsoft.Office.Interop.Excel;" or you can create an aliases for that like this: "using ExcelInterop = Microsoft.Office.Interop.Excel;"
Giorgi Dalakishvili
#region signature
my articles
#endregion
|
|
|
|
|
"Cannot create an instance of the abstract class or interface 'Excel.Application"
You cannot create instances of abstract classes or interfaces... Also, I don't see how your question has anything to do with this title.
Excel is not the namespace, use Microsoft.Office.Interop.Excel.
Cheers,
|
|
|
|
|
Please me Remoting concept of dot net in detail
---
Regards
ilango gandhi
' Knowledge can only be got in one way, the way of experience; there is no other way to know.'
|
|
|
|
|
ilango gandhi wrote: Remoting concept of dot net
The concept is like taking two tin cans and linking them together with a string.
led mike
|
|
|
|
|
|
|
You have a habit of asking very general questions that could be answered with the use of Google. Please study the language, and then come back with specific questions.
|
|
|
|
|
Hi,
I need help to create something like a news ticker. I want text to scroll from the right site of the screen to the left. I've been playing around with a DoubleAnimation but than you won't get a continuasly stream of text and it won't show all the text that is my TextBlock wich is in a UserControl.
I don't wan't to use substring to remove characters because this doesn't give a smooth animation.
I'm using C# code in a WPF project.
Thanks
Kevin
|
|
|
|
|
KBou wrote: DoubleAnimation
KBou wrote: WPF
Oh, you did say it was WPF. I suggest using the WPF forum.
Christian Graus
Please read this if you don't understand the answer I've given you
"also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
|
|
|
|
|
I have a service that launches multiple threads. Each thread has a unique purpose and thus they are unique sources. I have a successfully created log file named OneSource and I want all of the sources logging events to that log file.
Currently I have each thread initializing and accessing the EventLog with their own instances, the service itself does not do any logging. My first thread that I start successfully initialized and logs to the file but subsequent ones do not. Here is my eventlog initialization logic (some names were changed to protect the innocent :
#region Eventlog initialization
bool creationNeeded = false;
if ( !EventLog.SourceExists( "someSource" ) )
creationNeeded = true;
else if ( !EventLog.LogNameFromSourceName( "someSource", "." ).Equals( "OneSource" ) )
{
creationNeeded = true;
EventLog.DeleteEventSource( "someSource" );
}
if ( creationNeeded )
{
EventLog.CreateEventSource( "someSource", "OneSource" );
}
events = new System.Diagnostics.EventLog( "OneSource", ".", "someSource" );
events.WriteEntry( "someSource: Event log successfully started.", EventLogEntryType.Information );
#endregion
FYI: We had management constantly changing their minds on the log file to use so we had to add code to make sure that the old log was deleted before we used the new logfile name and source.
I see the initialization message from sourceOne, but not from sourceTwo or sourceThree. Nor do I see any logged messages from anything but sourceOne. Also...I see Information logged entries from sourceOne, but I never see anything logged where I use an AuditSuccess state for the log event.
Any guidance would be appreciated.
|
|
|
|
|
Wow, is that production code with all those string literals? Are you kidding me?
LongRange.Shooter wrote: I see the initialization message from sourceOne, but not from sourceTwo or sourceThree.
Well I don't see any string literals for anything but someSource and OneSource in the code you posted so based on that seeing anything else is just not possible, period.
ASIDE: The code you posted is horrible, literally.
led mike
|
|
|
|
|
Well, smartass, you don't see it because it is just the initialization code for someSource. The other initialization routines are the same, but someSource is replace with anotherSource, finalSource (for example). It is representative code of the initialization routine. And, BTW, in a recent technology testing I took I rated in the top 5% internationally as a C# coder.
So do you have anything helpful to add or do you just like hearing yourself blow crap out your mouth?
|
|
|
|
|
F*** off a**hole
led mike
|
|
|
|
|
LongRange.Shooter wrote: in a recent technology testing I took I rated in the top 5% internationally as a C# coder.
So was it an easy test then?
Bob
Ashfield Consultants Ltd
|
|
|
|
|
No it was the Robert Half test, locally most got only 75% and internationally the average was 79% while I scored 92%.
|
|
|
|