Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / July 2007

Tip: Looking for answers? Try searching our database.

Threading on Functions returning values

Thread view: 
Enable EMail Alerts  Start New Thread
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 Magazines

Get 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 ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.