简体   繁体   English

为方法创建新线程

[英]Creating a new thread for a method

I am currently trying to increase the Stack size for a new Thread , so that I can run my method below without the application throwing out a StackOverflowException . 我目前正在尝试增加新线程堆栈大小,以便可以在不运行应用程序抛出StackOverflowException的情况下在下面运行我的方法。

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

    //...EDIT with code samples

        //Floor - 1
        var qisg = new QuoteItemSectionGroup
        {
            SectionGroup = db.SectionGroups.Where(x => x.Name == "Longitudinals" && x.Section == TruckSection.Floor).First(),
            StockItem = quoteItem.FloorSpec.Name == "Ali Floor Bakery" || quoteItem.FloorSpec.Name == "Trailer Floor" || quoteItem.FloorSpec.Name == "Use Existing Floor" ? null : quoteItem.BodyType.Name == "Insulated" || quoteItem.BodyType.Name == "Insulated Meat Hanger" ? db.StockItems.Where(x => x.StockCode == "SCH095").First()
                : quoteItem.BodyType.Name == "Royal Corrugated" || quoteItem.BodyType.Name == "Royal Smooth Glued" || quoteItem.BodyType.Name == "Royal Smooth Riveted" ||
                quoteItem.BodyType.Name == "Royal Tautliner" || quoteItem.BodyType.Name == "Penta Slider Curtain-side" || quoteItem.BodyType.Name == "Bakery Light Weight GRP" ||
                quoteItem.BodyType.Name == "Free Flow Bakery Body" || quoteItem.BodyType.Name == "Boardside GRP Body" || quoteItem.BodyType.Name == "Drop Side" || quoteItem.BodyType.Name == "Flat Deck" ? quoteItem.ChassisModel.Longitudinal : null
        };
        qisg.Quantity = qisg.StockItem == null ? 0 : 2;
        qisg.Length = qisg.StockItem == null ? 0 : Math.Round((quoteItem.BodyType.Name == "Royal Corrugated" || quoteItem.BodyType.Name == "Royal Smooth Glued" || quoteItem.BodyType.Name == "Smooth Folded" || quoteItem.BodyType.Name == "Royal Smooth Riveted" || quoteItem.BodyType.Name == "Bakery Light Weight GRP" || quoteItem.BodyType.Name == "Free Flow Bakery Body" || quoteItem.BodyType.Name == "Boardside GRP Body" || quoteItem.BodyType.Name == "Insulated" || quoteItem.BodyType.Name == "Insulated Meat Hanger" || quoteItem.BodyType.Name == "Drop Side" || quoteItem.BodyType.Name == "Flat Deck" ? -0.157
            : quoteItem.BodyType.Name == "Penta Slider Curtain-side" ? -0.150 : quoteItem.BodyType.Name == "Royal Tautliner" ? -0.060
            : 0) + (quoteItem.ExternalLength / 1000), 3);
        qisg.Weight = qisg.StockItem == null ? 0 : Math.Round(((double)qisg.Length * (double)qisg.Quantity) * (double)qisg.StockItem.Mass, 3);
        qisg.Cost = qisg.StockItem == null ? 0 : Math.Round(((double)qisg.Length * (double)qisg.Quantity) * (double)qisg.StockItem.UnitCost, 2);
        if (qisg.StockItem != null)
            quoteItem.SectionGroups.Add(qisg);

        //Floor - 2
        qisg = new QuoteItemSectionGroup
        {
            SectionGroup = db.SectionGroups.Where(x => x.Name == "Cross Member" && x.Section == TruckSection.Floor).First(),
            StockItem = quoteItem.FloorSpec.Name == "Ali Floor Bakery" || quoteItem.FloorSpec.Name == "Trailer Floor" || quoteItem.FloorSpec.Name == "Use Existing Floor" ? null : quoteItem.BodyType.Name == "Insulated" || quoteItem.BodyType.Name == "Insulated Meat Hanger" || quoteItem.BodyType.Name == "Penta Slider Curtain-side" ? db.StockItems.Where(x => x.StockCode == "SCH095").First()
                : quoteItem.BodyType.Name == "Royal Corrugated" || quoteItem.BodyType.Name == "Royal Smooth Glued" || quoteItem.BodyType.Name == "Royal Smooth Riveted" || quoteItem.BodyType.Name == "Royal Tautliner" || quoteItem.BodyType.Name == "Bakery Light Weight GRP" ||
                quoteItem.BodyType.Name == "Free Flow Bakery Body" || quoteItem.BodyType.Name == "Boardside GRP Body" || quoteItem.BodyType.Name == "Drop Side" || quoteItem.BodyType.Name == "Flat Deck" ? db.StockItems.Where(x => x.StockCode == "SCH075").First() : null
        };
        qisg.Quantity = qisg.StockItem == null ? 0 : Math.Floor(quoteItem.BodyType.Name == "Insulated" || quoteItem.BodyType.Name == "Insulated Meat Hanger" || quoteItem.BodyType.Name == "Royal Corrugated"
            || quoteItem.BodyType.Name == "Royal Smooth Glued" || quoteItem.BodyType.Name == "Royal Smooth Riveted" || quoteItem.BodyType.Name == "Royal Tautliner"
            || quoteItem.BodyType.Name == "Bakery Light Weight GRP" || quoteItem.BodyType.Name == "Free Flow Bakery Body" || quoteItem.BodyType.Name == "Boardside GRP Body" || quoteItem.BodyType.Name == "Drop Side" || quoteItem.BodyType.Name == "Flat Deck" ? ((quoteItem.ExternalLength / quoteItem.FloorChannelSpacing.Amount) + 2) 
            : quoteItem.BodyType.Name == "Penta Slider Curtain-side" ? (quoteItem.ExternalLength / quoteItem.FloorChannelSpacing.Amount) : 0);
        qisg.Length = qisg.StockItem == null ? 0 : Math.Round((quoteItem.BodyType.Name == "Royal Corrugated" || quoteItem.BodyType.Name == "Royal Smooth Glued" || quoteItem.BodyType.Name == "Smooth Folded" || quoteItem.BodyType.Name == "Royal Smooth Riveted" || quoteItem.BodyType.Name == "Bakery Light Weight GRP" || quoteItem.BodyType.Name == "Free Flow Bakery Body" || quoteItem.BodyType.Name == "Boardside GRP Body" || quoteItem.BodyType.Name == "Insulated" || quoteItem.BodyType.Name == "Insulated Meat Hanger" || quoteItem.BodyType.Name == "Drop Side" || quoteItem.BodyType.Name == "Flat Deck" ? -0.020
            : quoteItem.BodyType.Name == "Penta Slider Curtain-side" ? -0.070 : quoteItem.BodyType.Name == "Royal Tautliner" ? -0.030
            : 0) + (quoteItem.ExternalWidth / 1000), 3);
        qisg.Weight = qisg.StockItem == null ? 0 : Math.Round(((double)qisg.Length * (double)qisg.Quantity) * (double)qisg.StockItem.Mass, 3);
        qisg.Cost = qisg.StockItem == null ? 0 : Math.Round(((double)qisg.Length * (double)qisg.Quantity) * (double)qisg.StockItem.UnitCost, 2);
        if (qisg.StockItem != null)
            quoteItem.SectionGroups.Add(qisg);   

    //It continues on like this with about 80 more blocks of code like the above samples          

    result.Successful = true;
    return result;
}

I've tried coding in a couple of ways that people have suggested in other questions, but I cannot figure out how to use my method in a new thread. 我已经尝试了人们在其他问题中建议的几种方式进行编码,但是我无法弄清楚如何在新线程中使用我的方法。

Here is one of my tries: 这是我的尝试之一:

void NewThread(QuoteResult result)
{
    const int stackSize = 0x400000;
    var T = new Thread(CalculateRates, stackSize);
    T.Start();
    T.Join();
}

But, it throws out the error: 但是,它抛出了错误:

The best overloaded method match for 'System.Threading.Thread.Thread(System.Threading.ParameterizedThreadStart, int)' has some invalid arguments 最佳重载方法匹配'System.Threading.Thread.Thread(System.Threading.ParameterizedThreadStart,int)'有一些无效的参数

As you can clearly see, I have NO clue what I am doing or how to create a new thread for my method, so I do expect a lot of criticism. 正如您可以清楚地看到的那样,我不知道我在做什么或者如何为我的方法创建一个新线程,所以我确实会受到很多批评。 If anyone needs any more code samples or information, please do not hesitate to ask. 如果有人需要更多代码示例或信息,请随时询问。

Thanks for any help in advance! 感谢您的任何帮助!

And sorry if it's a vague question 抱歉,如果这是一个模糊的问题

EDIT 编辑

Methods that also use the CalculateRates method: 还使用CalculateRates方法的方法:

public QuoteResult GetQuote(QuoteData Data)
{
    using (TruckDb db = new TruckDb())
    {
        var result = CalculateRates(db, Data);
        return result;
    }
}

AND

    public bool FinalizeQuote(QuoteResult quoteResult)
    {
        using (TruckDb db = new TruckDb())
        {
            var calcResult = CalculateRates(db, quoteResult.Data);

            //Make sure that result still gives the same amount and that they where successful calculations
            if (calcResult.TotalAmount != quoteResult.TotalAmount || !quoteResult.Successful || !calcResult.Successful)
                return false;

            var company = db.Companies.Where(x => x.Name == "Test Co.").FirstOrDefault();
            if (company == null)
                company = db.Companies.Add(new Company { Name = "Test Co." });

            var rep = company.Represetatives.Where(x => x.Name == "Person").FirstOrDefault();
            if (rep == null)
            {
                rep = new Represetative { Name = "Person" };
                company.Represetatives.Add(rep);
            }
            var quote = new Quote { Date = DateTime.Now, QuoteNumber = DateTime.Now.ToString() };
            rep.Quotes.Add(quote);

            quote.Items.Add(calcResult.QuoteItem);


            db.SaveChanges();
        }
        return true;
    }

You are not passing in the parameters CalculateRates needs. 您没有传递CalculateRates需要的参数。

You can do this by passing in a simple lambda expression: 您可以通过传递一个简单的lambda表达式来做到这一点:

void NewThread(QuoteResult result)
{
    //example for creating the parameters to pass in
    TruckDb db = new TruckDb();
    QuoteData data = new QuoteData();

    const int stackSize = 0x400000;
    var T = new Thread(() => CalculateRates(db, data), stackSize);
    T.Start();
    T.Join();
}

You can use the Task 您可以使用Task

refer the mentioned link Task 参考提到的链接任务

other than what the guys above said already, but you are having a simple syntax error. 不同于上面的人已经说过的,但是您遇到了一个简单的语法错误。

You are not passing in the parameters for the Thread method "CalculateRates". 您没有传递Thread方法“ CalculateRates”的参数。 The Thread method should have only one parameter of type "object" which you can pass in, like Thread方法应该只有一个可以传递的“ object”类型的参数,例如

var t = new Thread(new ParameterizedThreadStart(youmethod);
t.Start(yourparam);

The other solution would be to use closures. 另一种解决方案是使用闭包。 But in general, you are simply not passing in the parameters for the method you are trying to call. 但是总的来说,您只是没有为尝试调用的方法传递参数。

check the samples from the MSDN: https://msdn.microsoft.com/en-us/library/system.threading.parameterizedthreadstart%28v=vs.110%29.aspx 检查来自MSDN的示例: https : //msdn.microsoft.com/zh-cn/library/system.threading.parameterizedthreadstart%28v=vs.110%29.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM