.NET Forum / .NET Framework / New Users / July 2007
Threading on Functions returning values
|
|
Thread rating:  |
A.K.Seerajdeen - 19 Jul 2007 06:52 GMT Hi All,
I am a developer in .Net 2005, I am facing a problem in Multithreading concept. The problem is, I have a function which returns a value of string type and also carries some parameters(Arguments) and I am calling that function in a button click. When ever I call the fuction the GUI gets hanged Up. So, I want to apply threading concept on it since that function parses number of webpages, it takes lot of time and the GUI gets struck up mean while, it has become a very big problem.
I hope, have understood the problem, Please anyone help me solving this problem, any sample code or any explaination regarding this would me a great help for me.
Thanks In advance to all, I hope this forum would be a great help for me, since I am new to this forum.
Thanking You,
With Regards, A.K.Seerajdeen, Jr. Software Programmer, Nanna Computers (Vizag), Tel: +919885444639, siraj@nannacomputers.com.
(This electronic message contains information from Nanna Computers, which may be privileged or confidential. The information is for the intended individual(s) or entity named above. If you are not the intended recipient be aware that any copying, disclosing, distribution or use of the above email and/or its attachments is prohibited and is illegal. If you have received this electronic message in error, please notify us by telephone or email (to the numbers or address above) immediately. This e-mail is not un-solicited e-mail under the relevant regulations.)
ivar - 19 Jul 2007 07:26 GMT Hi,
You should call your method through Control.Invoke() , that creates call on UI thread.
> Hi All, > [quoted text clipped - 30 lines] > or email (to the numbers or address above) immediately. This e-mail is not > un-solicited e-mail under the relevant regulations.) Benny Skjold Tordrup - 19 Jul 2007 08:20 GMT Ivar,
How do You call Control.Invoke on a method that has a return type other than void?
Best regards,
Benny
> Hi, > [quoted text clipped - 35 lines] >> telephone or email (to the numbers or address above) immediately. This >> e-mail is not un-solicited e-mail under the relevant regulations.) ivar - 19 Jul 2007 09:08 GMT you must declare delegate before, then you can call delegate.
like:
private delegate OnCompletedOnUIDelegate void (string value)
class xxx .....
// Assume that this is called from not UI thread private void OnCompleted(string value) { this.Invoke(new OnCompletedOnUIDelegate (this.OnCompletedOnUIThread),new object[]{value}); }
// This is called on UI thread. private void OnCompletedOnUIThread(string value) { }
> Ivar, > [quoted text clipped - 45 lines] >>> immediately. This e-mail is not un-solicited e-mail under the relevant >>> regulations.) Peter Duniho - 19 Jul 2007 17:46 GMT > you must declare delegate before, then you can call delegate. > > like: > > private delegate OnCompletedOnUIDelegate void (string value) Except that he specifically asked about calling delegates that have a return type other than void. So I'm not sure your example is the best one. :)
As far as the actual answer goes:
Control.Invoke() returns an object. This is the return value of the delegate being called. If the return value isn't a reference type (ie, not derived from Object), that's not a problem. The value type is simply boxed and returned as an Object reference. You can then assign this back to the expected value type and the return value will be unboxed.
So, it works exactly as you'd expect. Just have your delegate return a value, and then assign the return value of Control.Invoke() to whatever you need it to be assigned to (or use it in an expression however you need it, whatever's appropriate).
Pete
A.K.Seerajdeen - 27 Jul 2007 04:54 GMT Hi Mr.Peter,
Thank you for responding to my query. I could able to understand the = explaination given below by you. I have tried the way which you have = explained but I am not able to do the coding has you have mentioned = since lack of idea in delegates. It will be more helpful if you could = explain me in terms of coding. Please do not mind and please help me out = of this problem.
Thanking You,
With Regards, A.K.Seerajdeen, Emp ID: NGV33051, Jr. Software Programmer, Nanna Computers (Vizag), Tel: +919885444639, siraj@nannacomputers.com
>> you must declare delegate before, then you can call delegate. >> [quoted text clipped - 20 lines] > > Pete Peter Duniho - 27 Jul 2007 05:46 GMT > Hi Mr.Peter, > [quoted text clipped - 3 lines] > since lack of idea in delegates. It will be more helpful if you could = > explain me in terms of coding. As an example:
delegate string MyDelegate();
void InvokingMethod() { string strT;
strT = (string)this.Invoke((MyDelegate)InvokedMethod); }
string InvokedMethod() { return "This is a test string"; }
In other words, it works just like calling a method directly, in that when the delegate returns a value, so too does the Invoke() method. Just cast the return value from Invoke() to whatever you need, and as long as the cast matches the actual return type of the invoked method, it will work fine.
If the above does not solve the issue, I recommend that you read the MSDN documentation on delegates as well as the Control.Invoke() method. If you still have questions, please feel free to post them, but do make sure that the questions are specific enough to be able to answered in a straight-forward way. It would be especially useful if you phrase the question in terms of what in the documentation you are having trouble understanding.
Pete
A.K.Seerajdeen - 28 Jul 2007 08:01 GMT Hi Mr.Peter,
Thank you for responding to my request of coding. I am very glad and happy to add in this forum. People like you are very Important for this forum. From the below code which you have send me I could able to solve my problem, Thank very much for this code. I hope I could also help others in the same way like you did.
Thanking You,
With Regards, A.K.Seerajdeen, Emp ID: NGV33051, Jr. Software Programmer, Nanna Computers (Vizag), Tel: +919885444639, siraj@nannacomputers.com.
(This electronic message contains information from Nanna Computers, which may be privileged or confidential. The information is for the intended individual(s) or entity named above. If you are not the intended recipient be aware that any copying, disclosing, distribution or use of the above email and/or its attachments is prohibited and is illegal. If you have received this electronic message in error, please notify us by telephone or email (to the numbers or address above) immediately. This e-mail is not un-solicited e-mail under the relevant regulations.)
As an example:
delegate string MyDelegate();
void InvokingMethod() { string strT;
strT = (string)this.Invoke((MyDelegate)InvokedMethod); }
string InvokedMethod() { return "This is a test string"; }
In other words, it works just like calling a method directly, in that when the delegate returns a value, so too does the Invoke() method. Just cast the return value from Invoke() to whatever you need, and as long as the cast matches the actual return type of the invoked method, it will work fine.
If the above does not solve the issue, I recommend that you read the MSDN documentation on delegates as well as the Control.Invoke() method. If you still have questions, please feel free to post them, but do make sure that the questions are specific enough to be able to answered in a straight-forward way. It would be especially useful if you phrase the question in terms of what in the documentation you are having trouble understanding.
Peter..
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|