|
Hello all,
I am currently using ThreadPool.QueueUserWorkItem to queue items onto the thread pool, the code I am using is relatively simple and looks something like this:
private static void StartQueue
{
StringReader sr = new StringReader(rawData);
string line = string.Empty;
while (((line = sr.ReadLine()) != null) && (this.allowQueuing))
{
ThreadPool.QueueUserWorkItem(new WaitCallback(DoMagicalThings), line);
}
}
private static void DoMagicalThings(object data)
{
Console.WriteLine(data.ToString())
}
What I need to work out is when the last item in the queue has completed processing. At first I came up with a fairly heath robinsion solution which was after the While in StartQueue I would add a null object onto the queue and then check this in DoMagicalThings and set a completed flag.
The problem here is that in the real app DoMagicalThings does alot more than just a simple console write, so I have found that in some cases the last item added to the queue is not always the last item whose execution finishes.
So the question is does anybody know how I can validate that all items I have added to the thread pool have completed
|
|
|
|
|
Keep a running count of how many threads you have going. Each time you queue up a job, increment a counter. In order to wait for all of the jobs to have completed, do something like this:
lock(lockObject)
{
while(jobCount > 0)
{
Monitor.Wait(lockObject);
}
}
And in each of threads, do this:
private void ThreadJobProcedure(object data)
{
lock(lockObject)
{
jobCount--;
Monitor.Pulse(lockObject);
}
}
|
|
|
|
|
That looks like it would do what I need, however considering I am a bit thick I am not sure what to use as the lockObject. Using the example in my first post the code has changed to something like this:
private static void StartQueue
{
StringReader sr = new StringReader(rawData);
string line = string.Empty;
int jobCount = 0;
while (((line = sr.ReadLine()) != null) && (this.allowQueuing))
{
ThreadPool.QueueUserWorkItem(new WaitCallback(DoMagicalThings), line);
jobCount++
}
lock(lockObject)
{
while(jobCount > 0)
{
Monitor.Wait(lockObject);
}
}
Console.WriteLine("All done");
}
private static void DoMagicalThings(object data)
{
Console.WriteLine(data.ToString())
lock(lockObject)
{
jobCount--;
Monitor.Pulse(lockObject);
}
}
An additional problem is that externally to this a user could click a Stop button, this should stop all threads and stop any further items being processed. Could I somehow use this Lock and Monitor functionality to do this?
In my original code, I had a simple class var bool that would be checked at the top of the worker method. If the value was false then the worker method would immediately return
|
|
|
|
|
You'll need to make the jobCount variable a member of the class instead of using a local variable as you did above. If you're using static methods, the variable will need to be a static variable.
As far as a lock object, what I usually do is just create a variable of type object:
private readonly object lockObject = new object();
It will need to be static if you're using it at the class level, i.e. in static methods.
MrEyes wrote: An additional problem is that externally to this a user could click a Stop button, this should stop all threads and stop any further items being processed. Could I somehow use this Lock and Monitor functionality to do this?
In my original code, I had a simple class var bool that would be checked at the top of the worker method. If the value was false then the worker method would immediately return
This works. The structure of your thread method could be something like this:
private void ThreadMethod(object data)
{
while(notStopped)
{
}
lock(lockObject)
{
jobCount--;
Monitor.Pulse(lockObject);
}
}
This way if the user clicks "Stop" and the notStopped variable is set to false, all of the threads break out of their loop, decrement the job counter, and signal that they're done.
You may want to make the jobCount and notStopped variables volatile so that when one thread updates them, their values are immediately updated, i.e. the compiler won't try to do any optimization in delaying their update.
|
|
|
|
|
Hi,
I want to create a windows c# .net control to use in another application.
Basicly this application is a OpenGL application that i wanna control as a Windows control.
How can i do this?
I have been searching for how to's on windows controls and i found some, i just want to know if i need to take any special care.
Thx,
Nuno
|
|
|
|
|
You can try to develop usercontrols
My small attempt...
|
|
|
|
|
Yes, i have already tried to do that, only a small try but without success.
The problem is that OpenGl is a infinite loop application and i tried to put the opengl program constructor in the usercontrol constructor and visual studio, when running told me that maybe i had a infinite loop in the control.
How can i bypass this problem?
Thx,
Nuno
|
|
|
|
|
Can anybody tell me how to programmaticaly (C#) clear printer buffer?
Printer spooler has sent printer jobs to local printer. I need to remove these jobs from printer buffer on user command.
Thanks for your ideas
Marian
|
|
|
|
|
|
Hi Martin,
thanks for your idea, but your solution is usable for clearing printer queue (i.e. jobs waiting in the printer spooler file). My jobs are already sent to the printer (printer queue is empty) and printer is processing them. I need to clear jobs from internal printer buffer.
Marian
|
|
|
|
|
Is there a Cancel button (usually red) on the printer?
|
|
|
|
|
I'm using Microsoft WinForms ReportViewer in local mode in C# applications.
If user clicks in report in preview , form should be opened (my form class
should be instantiated)
I tried
<textbox name="test">
<value>Open Winform
<action>
<hyperlink>appl://myWinFormClass
however nothing happens when I click in report in textbox.
How to force ReportViewer to open Windows Form when clicked in report?
Andrus
|
|
|
|
|
Hi all, as far as I know you can free a port used by a socket by calling the method disconnect(). I'm using .net remoting and I need to restart a connection with a client without closing them, but when I try I get an exception telling me that I cannot use two times the same socket. I suppose I should wait for the system to free that port, but is there a way to free it from the code? Can I get access to the port used by the channel? Because I cannot wait 2mins to have the connection back.
Thank you for your help!
Alessandro
|
|
|
|
|
Hello
I want to show Animated Gif on start form of my application. But i don't know which control can do this!?
I used PictureBox, Panel, ... but all of them just show first frame like a picture and there is no animation. Finally i used Label control that shows animation incorrectly and don't show some frames.
I know i should use PictureBox but should i set some properties which i don't know about them?
Please send me your suggestions...
Tanx
|
|
|
|
|
Hi,
Well, as a first idea, I would say to use the original HTML tag; unless you need PictureBox for some other processing later. But if it is just to display a simple GIF animation, this will most probably work, unless your GIF animation wasn't compiled correctly in the first place.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning."
--Rich Cook
|
|
|
|
|
Hi again
Dear friend i'm developing windows form application. tag is using in WebForms. No, there is no problem with gif image.
Tanx
|
|
|
|
|
Oops sorry, I didn't notice that. I should have, though, since PictureBox is a Windows Forms control and not a Web Forms control. Well anyway, as I know it, animated GIFs only work with web pages and I don't think you can make them work with a windows application. Please get another opinion on this matter because i'm not 100% sure about that but I have personally never seen an animated GIF on a windows forms application.
Sorry that I can't be of more help.
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning."
--Rich Cook
|
|
|
|
|
|
Hi guys,
How i can make Enable foldering in richtextbox like edtiors ?
|
|
|
|
|
|
|
Oh, you mean Visual Studio's ability to expand and contract blocks of code. That is a function of the Visual Studio IDE. It isn't available in the RichTextBox. You would have to write the functionality yourself.
|
|
|
|
|
|
ahmed17 wrote: How i can do it ?
That would take to long to explain in a forum post. Start by writing down all the functionality you need in your editor. Itemise everything, every minute detail. How it looks, how it response to mouse clicks, key presses and so on. Then work out how you would implement it. What classes you need, how they will interact, what events it will raise and respond to, and so on. Then start writing code. Write unit tests, ensure it works. Use the control. Once you have it to the quality that you want, you can use it in your application.
|
|
|
|
|
Yup, for some bizarre reason the very control we all spend so much time looking at is not part of the .NET framework AFAIK.
If you dont want to do all the code then have a quick look at SharpDevelop (http://www.sharpdevelop.com/[^])
They have written everything from the ground up, but you'll have to spend a fair amount of time extracting the relevant bits - i did read somewhere that you can use their code as it has it's own namespace, and of course it is OpenSource - but there's a lot of code there.
It also a good IDE.
|
|
|
|