4

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.

Pankaj Mishra
  • 20,197
  • 16
  • 66
  • 103
  • 2
    What is httpHelper and why do you need to pass it by reference? Usually the only time you want to pass a reference type by reference is when you need to assign it a new reference. – Dismissile Oct 21 '10 at 14:32
  • Sorry, I'm having trouble following what you're trying to do... do you have code you can post for what you have so far? Agree about passing the object by ref. – James King Oct 21 '10 at 14:44

4 Answers4

8

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 :)

Community
  • 1
  • 1
sumit_batcoder
  • 3,369
  • 1
  • 25
  • 36
1

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

dashton
  • 2,684
  • 1
  • 18
  • 15
0

If don't want your method to hang user interface you should use BackgroundWorker class. Look at http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx.

Best regards.

Ash
  • 1,924
  • 1
  • 13
  • 14
0

Here's how to invoke a worker method on its own thread that invokes a callback to pass data back to the main thread:

class Program
{
    public static volatile bool done = false;
    static void Main(string[] args)
    {
        WorkerClass worker = new WorkerClass();
        worker.Callback = new WorkerCallbackDelegate(WorkerCallback);
        System.Threading.Thread thread = new System.Threading.Thread(worker.DoWork);
        thread.Start();

        while (!done)
        {
            System.Threading.Thread.Sleep(100);
        }
        Console.WriteLine("Done");
        Console.ReadLine();
    }

    public static void WorkerCallback(object dataArg)
    {
        // handle dataArg
        done = true;
    }
}

public delegate void WorkerCallbackDelegate(object dataArg);
class WorkerClass
{
    public WorkerCallbackDelegate Callback { get; set; }
    public void DoWork()
    {
        // do your work and load up an object with your data
        object data = new object();
        Callback(data);
    }
}
Ed Power
  • 8,310
  • 3
  • 36
  • 42