我目前正在尝试为新的线程增加堆栈大小,以便我可以在下面运行我的方法,而不会让应用程序抛出 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;
}
我尝试过以人们在其他问题中提出的几种方式进行编码,但我无法弄清楚如何在新线程中使用我的方法。
以下是我的尝试之一:
void NewThread(QuoteResult result)
{
const int stackSize = 0x400000;
var T = new Thread(CalculateRates, stackSize);
T.Start();
T.Join();
}
但是,它抛出了错误:
最佳重载方法匹配 ' System.Threading.Thread.Thread(System.Threading.ParameterizedThreadStart, INT)'有一些无效的论点
你可以清楚地看到,我不知道我在做什么或如何为我的方法创建一个新线程,所以我确实期待很多批评。如果有人需要更多代码示例或信息,请不要犹豫。
提前感谢您的帮助!
抱歉,如果这是一个模糊的问题
修改
也使用 CalculateRates 方法的方法:
public QuoteResult GetQuote(QuoteData Data)
{
using (TruckDb db = new TruckDb())
{
var result = CalculateRates(db, Data);
return result;
}
}
和
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;
}
答案 0 :(得分:2)
您没有传递参数CalculateRates
需要。
您可以通过传入一个简单的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();
}
答案 1 :(得分:0)
您可以使用Task
参考上述链接Task
答案 2 :(得分:0)
除了上面提到的那些人之外,你有一个简单的语法错误。
您没有传递Thread方法的参数" CalculateRates"。 Thread方法应该只有一个类型" object"的参数。您可以传入,例如
var t = new Thread(new ParameterizedThreadStart(youmethod);
t.Start(yourparam);
另一种解决方案是使用闭包。但一般来说,你只是没有传递你试图调用的方法的参数。
检查来自MSDN的示例: https://msdn.microsoft.com/en-us/library/system.threading.parameterizedthreadstart%28v=vs.110%29.aspx