Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Pass refrence and Get Return in Thread?

I Working on desktop application where i am get struck. I have a method through I am doing HTTP Post And Get. I am managing this object through ref in entire application. This object fetching category from website and i am using same ref for posting as well.

This category Fetcher method return datatable of categories. This Method hang my UI, So i need to implement this in thread.

But i don't know how to pass ref in thread and get return values.

This is How I am passing values.

Categorydt = objPostDataFetcher.FetchCategories(ref httpHelper);

I want to call this method in Thread. Please give me any idea and suggestion. Thanks in Advance.

like image 632
Pankaj Mishra Avatar asked Jan 21 '23 05:01

Pankaj Mishra


2 Answers

I think this should solve the problem of passing ref.

new Thread(() => { YourMethod(ref httpHelper);

in your case, it looks to be

new Thread(() => { objPostDataFetcher.FetchCategories(ref httpHelper);

And if you want to use method with return type in thread, you can use this link how to call the method in thread with aruguments and return some value

Good Luck :)

like image 132
sumit_programmer Avatar answered Jan 23 '23 20:01

sumit_programmer


The simplest approach would be to use an asynchronous delegate, as this will give you parameter passing and return values. However, it is worth bearing in mind that this will run on a thread-pool thread and may not be suitable if your calls are very long-running. Anyway, start with delegates and see how it performs. There is a good tutorial here:

http://msdn.microsoft.com/en-us/library/h80ttd5f.aspx

like image 36
dashton Avatar answered Jan 23 '23 19:01

dashton