0

How would I go about returning the "var T" like the "var result" in the example below(code comments) in the new Thread that I created?

public QuoteResult GetQuote(QuoteData Data)
{
    using (TruckDb db = new TruckDb())
    {
        const int stackSize = 8000000;
        var T = new Thread(() => CalculateRates(db, Data), stackSize);
        T.Priority = ThreadPriority.Highest;
        T.Start();
        T.Join();

        //var result = CalculateRates(db, Data);
        //return result; //Example of how I want it in the new thread created above
    }
}

My CalculateRates method coding:

public QuoteResult CalculateRates(TruckDb db, QuoteData data)
{
    var result = new QuoteResult
    {
        Successful = false,
        Data = data
    };

    List<QuoteItemSectionGroup> quoteItems = new List<QuoteItemSectionGroup>();

    var quoteItem = new QuoteItem
    {
        ChassisModel = db.ChassisModel.Find(data.ChassisId),
        ChassisManufacturer = db.ChassisManufacturer.Find(data.ChassisManufacturerId),
        BodyType = db.BodyTypes.Find(data.BodyTypeId),
        //...10 lines more
    };

    //...unnecessary to see codes and calculations...

    result.QuoteItem = quoteItem;
    result.Successful = true;
    return result;
}

I just need to run my CalculateRates method in a new thread to increase the stack size. If you need any more code or information, please don't hesitate to ask. Thank you!

CareTaker22
  • 1,260
  • 3
  • 18
  • 36
  • 1
    see also : http://stackoverflow.com/q/1314155/327083 Many of these don't directly apply since you seem to want a distincly synchronous operation (ie: the thread's purpose is not for background work), but it's worth reading for ideas just the same. – J... Nov 30 '15 at 10:38

1 Answers1

3

The most straightforward way to do this is

QuoteResult qr = null;
var T = new Thread(() => qr = CalculateRates(db, Data), stackSize);
T.Priority = ThreadPriority.Highest;
T.Start();
T.Join();
return qr;

Alternatively, another way to do this, in this context, is to use an out parameter instead of a function return value. This is of particular use if you need to return more than one value.

public void CalculateRates(TruckDb db, QuoteData data, out QuoteResult qrResult)
{
    qrResult= new QuoteResult
    {
        Successful = false,
        Data = data
    };    

    //...unnecessary to see codes and calculations...

    qrResult.QuoteItem = quoteItem;
    qrResult.Successful = true;        
}

Then call as :

 QuoteResult qr = null;
 var T = new Thread(() => CalculateRates(db, Data, out qr), stackSize);
 T.Priority = ThreadPriority.Highest;
 T.Start();
 T.Join();
 return qr;

In both cases the null initialization is necessary because the compiler cannot see across the thread boundary to determine that a value has actually been assigned. Without the .Join call, for example, it's quite likely that a value would not yet have been assigned so you have to be explicit.

J...
  • 30,968
  • 6
  • 66
  • 143
  • Thanks for the comment man! :) I get the error: *Use of unassigned local variable 'qr'* when running your code – CareTaker22 Nov 30 '15 at 10:48
  • @CareTaker22 You can initialize it with null and compiler error would disappear. – Jenish Rabadiya Nov 30 '15 at 10:56
  • 2
    An alternative is do simply set the value inside your method you've passed to the thread. For example: `new Thread(() => qr = CalculateRates(new object()), stackSize);`. Saves you having to modify the `CalculateRates` method. – Rob Nov 30 '15 at 11:13
  • @Rob Worth an answer if you'd care to post it. Didn't think about that myself. – J... Nov 30 '15 at 12:09
  • @Rob Yes please, could you post your answer still? – CareTaker22 Nov 30 '15 at 12:54
  • @CareTaker22 I've added Rob's suggestion to my answer in the meantime. – J... Nov 30 '15 at 12:55
  • 1
    @Rob If you do post an answer, ping me and I'll revert the addition ;) – J... Nov 30 '15 at 12:56
  • @J... Your answer works and I can now calculate my values correctly. Thank you very much! :D The Only thing now is that *qr* returns **null**? The values that I need are in **Data** and not in **qr** :( – CareTaker22 Nov 30 '15 at 12:57
  • 1
    @CareTaker22 This sounds like the subject for a new question. Most likely what is happening is that you are only holding a reference to `Data` in the `QuoteResult` and not to the parent `TruckDb` - since you are using a `using` statement, `db` is falling out of scope and being disposed. You may need to copy the `Data` object rather than just hold a reference. – J... Nov 30 '15 at 13:02