AutoResetEvent Estado = new AutoResetEvent(false);
ThreadPool.QueueUserWorkItem(new WaitCallback(Trabalhadora), Estado);
Estado.WaitOne();
it gives me great control but i soon discovered that the main thread freezes...do i have to create a thread for the thread who controls or threads? or am i missing something?
What does the worker function do with the event object you pass to it? Are you sure you want to wait for the object to be signaled? If you have to wait for the signal then you shouldn't use a thread.
All the buzzt CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
well i have a download class...that must run on a separate thread..it has an event that fires when the download is complete...when in my project i try to create a object at run-time from that event i cant because it doesnt me allow to make that on a separate thread..so now i am making that the download class has a function that calls the thread and then waits for the thread to stop and sends the event itself so it wont raise that problem when i try to use it in my project..or u have any better idea?
How does the first thread react when the download is done? Maybe the other could send a user-defined message instead of raising the event. But I admit I have no idea how to send messages in .Net. Must have something to do with the struct System.Windows.Forms.Message.
All the buzzt CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemwindowsformsmessageclasstopic.htm
i dont get what i can do for me...it lets me the child function(download) thread to talk to the main (download class) thread?
Basically my idea is that the download thread should somehow set a flag for the main thread which the main thread checks from time to time. A message posted to the message loop would be ideal, but I'm not sure if you can send user-defined messages in .Net.
All the buzzt CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
class MyForm : Form
{
// ...
delegate void NoArgDelegate();
void DownloadFinished() {
// whatever
}
// ...
void Trabalhadora(object state) {
// download here
// and when finished:
BeginInvoke(new NoArgDelegate(DownloadFinished));
}
}
I might have syntax errors there as I don't really program in C#.
This code should call DownloadFinished in the thread that created the form some time after the download is finished. Not immediatly, only when the thread has some time.
All the buzzt CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
the problem would still be the same...i cant call directly a function from the function who's downloading because then all functions that are made in future can't create form objects because this is a separate thread...
hmm but to use beginvoke i have to inherit from Control...that sucks! and i dont want to inherit my control of anything...one of the reasons is that my control might already inherit from another control
delegate void NoArgDelegate();
how do i use this? i know how to use delegates but i am getting an "<function name> is referred without parantheses
on the form...
Code:
private void lol() {
}
form load(blablabla) {
this.BeginInvoke(Form1.NoArgDelegate(lol));
}
how should i write then? i tryed puting 'lol()' instead 'lol' but it gives me error once again ('WindowsApplication34.Form1.NoArgDelegate' denotes a 'class' which is not valid in the given context)
The delegate keyword declares a delegate, which is a special kind of class that always derives from System.MultiCastDelegate, which in turn derives from System.Delegate.
i had an idea now that i saw it on msdn...my download function could have a reference to my form and then it'd use the form's begininvoke
That's what I meant.
All the buzzt CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
hmm have a problem now...first i tried to assign to a var in the download class a var that would be assigned in the constructor pointing to the form who executed it:
constructor(this);
but soon i realised i couldn't do this because this way i was sending a new var to the class..so i tried with:
constructor(ref this);
but i can't because i get that this is read-only...what do i do then?
i want that when my download thread download finishes instead of directly fire the event to use the BeginInvoke with a reference made by the constructor to the main form and only then i'd fire the event.
the problem is that i can't make refereces to the 'this' word...how can i achieve the same?
class Download
{
private System.Windows.Forms.Control m_control;
public Download(System.Windows.Forms.Control c) {
m_control = c;
}
}
class MyForm : System.Windows.Forms.Form
{
public StartDownload() {
Download dl = new Download(this);
}
}
Can't imagine this not working...
All the buzzt CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Hey PT, this might be of some help. I was browsing CSharpToday.com and found an article on Thread Pooling. Its from the subscribers section, so I copied it to a word doc for you. Here is the link.
edit: and yes bee ur right lol but i have some error in my code what is driving me crazy is the message error i'm getting. i posted about that in vb.net's forum cuz i though that more ppl passed by there so i'd get a answer quickly but i didnt get a solution yet u might if u want to drop a view by the thread: http://www.vbforums.com/showthread.p...01#post1325401
Last edited by PT Exorcist; Jan 10th, 2003 at 03:14 PM.
yes thats what i am doin now...puted break points in ALL FUNCTIONS that are used and it still gives me error "outside" the break points...i dont get it..without the damn message saying where is the error's line this sucks and i am not willin to have to re-install the damn visual studio......
well i hate doin this but...can someone download and try to help me catchin the bug? it has to be something related to delegates..anyway i would be much appreciated...