|
Use of AutoResetEvent is good idea by using Set and Waitone methods.
You will require two instance of AutoResetEvent, one is for triggering condition(in your case button click) and other is for waiting for triggering.
Have you ever heard about producer/consumer pattern? Your problem can be easily solved by using this pattern. This pattern can be implemented in conditions where you have one thread producing the data and other thread consuming it. I cant make it out from your example whether the background worker is producing data or not (but i am sure it must be).
I would suggest you to go and search for this pattern you will get many good articles describing this pattern.
some of them are
www.albahari.com/threading/part2.aspx
Producer/Consumer Implementation Using Thread,Semaphore and Event
I hope this helps you to solve your problem
oops...I forgot to mention that recursive is not required if you use the above pattern use while(someflag){} instead.
Best Regards,
AD
|
|
|
|
|
Solution on: http://www.sendspace.com/file/f4pcjo
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
_tw = new ThreadingWrapper(this);
}
private ThreadingWrapper _tw;
private void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
_tw.ButtonClicked();
}
public void SetText(object o)
{
string text = (string)o;
label1.Text = text;
}
public void WaitingOnClick(object o)
{
label1.Text += " - WAITING";
button1.Enabled = true;
}
}
public class ThreadingWrapper
{
private AutoResetEvent _are = new AutoResetEvent(false);
private bool _waiting;
private Form1 _form;
public ThreadingWrapper(Form1 toControl)
{
_form = toControl;
}
internal void ButtonClicked()
{
if (_waiting)
{
_waiting = false;
_are.Set();
}
else
{
ThreadPool.QueueUserWorkItem(new WaitCallback(functionCall), 0);
}
}
private void functionCall(object p)
{
int num = (int)p;
Thread.Sleep(500);
_form.BeginInvoke(new WaitCallback(_form.SetText), num.ToString());
if (num % 10 == 0)
{
_form.BeginInvoke(new WaitCallback(_form.WaitingOnClick), "");
_waiting = true;
_are.WaitOne();
}
functionCall(num + 1);
}
}
Let me know if you need abort functionality... and I'm also open to any suggestions on how to improve code... always love to learn something new.
|
|
|
|
|
If you want to eliminate _waiting field in ThreadingWrapper class you should probably use _are.WaitOne(int timeout) overload instead of if(_waiting).
However, first make sure that you understand what current code is doing... and then try eliminating _waiting as a practice.
|
|
|
|
|
I'm using my own thread now, because I've read that ThreadPool should be only used for short operations. I replaced the waiting flag with ThreadState, so I managed to eliminate _waiting. I set the thread's IsBackground property to true, so that it gets aborted on exit.
But I want to abort the thread early myself in certain situations and I've read that using Abort() method isn't a wise thing to do. The only other way that I can think of is to create a flag, which is checked after each function call. If true, the function will return. The function gets 52 calls deep at most, usually around 10 calls deep.
However, I don't know if this will really "destroy the thread", or will the thread just be stopped. Will creating a new ThreadingWrappper object (to the same variable) destroy the thread?
Thanks in advance!
|
|
|
|
|
Thread should be created in situations when you need to control the flow of the thread (although, as you also read Abort is not a good idea) and you are not creating bunch of threads at once. If you wish to see for yourself most "common sense" benchmark for this situation try following code:
private void benchmarkThread()
{
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < 1000; i++)
{
Thread t = new Thread(foo);
t.Start();
}
sw.Stop();
MessageBox.Show(sw.ElapsedMilliseconds.ToString());
}
private void benchmarkQueue()
{
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < 1000; i++)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(foo));
}
sw.Stop();
MessageBox.Show(sw.ElapsedMilliseconds.ToString());
}
private void foo(object o)
{
}
private void button2_Click(object sender, EventArgs e)
{
benchmarkThread();
}
private void button3_Click(object sender, EventArgs e)
{
benchmarkQueue();
}
In my case I get around 5000 ms for Threads and 23 ms for Queue... so winner is more than clear. The reason why this difference is so big is that there are bunch of things that need to be done just so you can get a Thread. And ThreadPool is nothing more than a Pool of Threads that are pre-created for your application context.
So - conclusion or why I have started telling all this - if you have pretty good design/understanding of your code and KNOW how many threads you'll be spawning (be sure to understand that things like Form.BeginInvoke also use ThreadPool!!)... and that number is less then a number of Threads pre-created in ThreadPool (default is 25 I believe; you can influence this number) - then definitely go with ThreadPool.
Recommendation you've read is there because in bigger teams/on bigger projects often people carelessly use ThreadPool. So, situations in which requests come faster than they are processed arise, and then you see large memory/CPU increase of your app; because if all 25 threads of ThreadPool are doing something new QueueUserWorkItem requests will be - of course - queued. If your application can eventually process all requests and recover, everything will be fine; however if requests come faster then they are processed for a long time - you are eventually in some serious trouble (if you can't resubmit requests, app will lose all of them when it eventually crashes because of OutOfMemory). Notice that using Threads manually in this situation isn't silver bullet - just you will probably run out of resources much more quickly / problem will be more obvious.
Finally, using Thread.Abort - I understand you. I'm the one that also holds reference to the thread and then fires Abort... who gives a damn when it works in most cases . However, if you want to know on why it's not a good practice - go here[] (some of the explanations are really good).
|
|
|
|
|
Thanks, I think I understand ThreadPool a bit better now, but I still have some questions:
-Should I worry about the limit of 25 threads or will ThreadPool remove finished ones when the program queues 26th+ thread?
-Is there any safe way to abort/stop a thread from ThreadPool, such as with a flag within the function, so that it gets aborted at the right time?
I have 3 different functions to work on a separate thread, so I basically just need 3 different threads (not counting the STAThread and BeginInvoke threads). The functions can be launched any number of times though (but only one instance at the time), so the finished WorkItems should be removed if there's no way of reusing them.
Thanks in advance!
modified on Friday, September 10, 2010 4:59 AM
|
|
|
|
|
You probably found answer by now, but:
1. Threads are automatically recycled when they finish performing work assigned to them (exit outside method)
2. Not as far as I know - you can't say from outside: "Hey, turn off Thread #23". But inside method you can look for certain switches/flags (class bool parameter for example) and have some condition like:
if (flag)
return;
|
|
|
|
|
Thanks, that's great. Heading to MSDN now to get some understanding about ThreadPool.QueueUserWorkItem and WaitCallBack... Thanks again!
|
|
|
|
|
i just configure win task scheduler in one server that it will run a exe file at specific time but the problem is win task scheduler invoking the exe file many times. suppose when someone connect to that machine over the vpn & remote desktop then suddenly win task scheduler calling that exe. so as many time a user is connecting that server over the vpn some time that exe is calling by win task scheduler. so i just could not understand this behavior of win task scheduler.
so please tell me how to prevent win task scheduler to call exe before the setting time if many user connect to that server over the vpn. i need solution......very urgent.
thanks in advance.
tbhattacharjee
|
|
|
|
|
Tridip Bhattacharjee wrote: so please tell me how to prevent win task scheduler to call exe before the setting time if many user connect to that server over the vpn.
This has absolutely nothing to do with C#. Try posting this in the infrastructure[^] forum.
|
|
|
|
|
I AM STUDENT OF BSCS AND MY FINAL PROJECT IS NOW STARTED. MY FINAL PROJECT IS ABOUT ICMP CONTROLLING SYSTEM.PLZ HELP ME ABOUT THIS PROJECT.
TAHNXXXXXXXXXX
|
|
|
|
|
STOP SHOUTING!
All uppercase is considered as shouting on the internet. And rude.
All lowercase is considered childish.
Also, learn to spell. "PLZ" is not a word. You have a keyboard. Find the vowels. Use them.
"TAHNXXXXXXXXXX" is not, has never been, and will never be, a word. It's not even close to a word.
Now. the question:
What question? Are we supposed to hack into your PC and work out what your problem is? Read your mind? Or just guess?
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
|
"Bachelor of Science, Computer Studies"?
I are Troll
|
|
|
|
|
HUSSNAIN TOUFIQ wrote: MY FINAL PROJECT IS ABOUT ICMP CONTROLLING
THANK THE LORD. For a moment there I thought that said ICBM[^] Controlling.
|
|
|
|
|
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
HUSSNAIN TOUFIQ wrote: PLZ HELP ME ABOUT THIS PROJECT
No. With a childish behaviour like you show here (shouting, crappy text speak, not being able to ask a proper question) it's maybe better for you, if you don't graduate since you wouldn't stand a chance in working live.
"I love deadlines. I like the whooshing sound they make as they fly by." (DNA)
|
|
|
|
|
One of the great joys of the final year project is that it's the place where you really get to shine. It's not just about coding an application, it's all the bits that go with it - the design, thinking about the edge cases, working out the user experience with your software. This is your chance to really show how well you've learned your subjects; that you've listened and taken it all in, and that you are now ready to get to grips with core functionality in an imaginative and enthusiastic manner. You are doing yourself a disservice if you don't take the opportunity to actually deal with this by yourself; by all means ask for help when something gets too much for you, but show us what you can do. Astonish us. Be exceptional.
|
|
|
|
|
Pete O'Hanlon wrote: Astonish us. Be exceptional.
He definitely astonished me with his exceptional first impression of an angry elephant with constipation, shouting down the place and requesting unspecific help in a kind of language, that looks like it has been written by a monkey with rabies. Totally impressive how he seems to think, that anybody might help him that way.
"I love deadlines. I like the whooshing sound they make as they fly by." (DNA)
|
|
|
|
|
Smithers-Jones wrote: He definitely astonished me with his exceptional first impression of an angry elephant with constipation
5
|
|
|
|
|
Smithers-Jones wrote: an angry elephant with constipation
Smithers-Jones wrote: a monkey with rabies
Is this you subtly suggesting the development of a zoo management system? some merger of a hotel management system and a patient tracking system?
|
|
|
|
|
This has got to be one of the best, most constructive forum replies ever. It's just a shame it's too long for a quote in a signature!
5 from me
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
DaveyM69 wrote: It's just a shame it's too long for a quote in a signature!
You could try to cut down its length by using textspeak
"I love deadlines. I like the whooshing sound they make as they fly by." (DNA)
|
|
|
|
|
Thanks Dave. You could always use the "Astonish Us. Be Exceptional" bit if you like.
|
|
|
|
|
Done
|
|
|
|