2010-12-07 27 views
1

我很感激你的幫助提前。我對編程相當陌生,所以對我來說很簡單。現在我有一個耗時的循環,我真的需要在UI線程之外運行,因爲Update()會更新進度條,直到循環完成纔會發生。我已經看了一些簡短的線程教程,他們的更新永遠不會渲染,直到大循環結束。將一段代碼放在另一個線程上?

private void Button_Convert_Click(object sender, RoutedEventArgs e) 
{ 
MachineAngleCalculations.Instance.Arm1Length_Arbitrary = 12; 
MachineAngleCalculations.Instance.Arm2Length_Arbitrary = 11; 

int resolution = 100000; //points per shape 
double xstep, ystep, x, y; 

int totalpoints = resolution * ShapeList.Count; 
int calculatedpoints = 0; 
ProgStat.Update(calculatedpoints, totalpoints, "Calculated", "individual instructions."); 

InstructionList.Clear(); 

foreach (MachineLine item in ShapeList) 
{ 
    xstep = (item.End.X - item.Start.X)/(resolution - 1); 
    ystep = (item.End.Y - item.Start.Y)/(resolution - 1); 

    for (int i = 0; i < resolution; i++) 
    { 
    x = item.Start.X + xstep * i; 
    y = item.Start.Y + ystep * i; 
    InstructionList.Add(MachineAngleCalculations.Instance.XYtoMachineInstrution(x, y, 1)); 
    calculatedpoints += 1; 
    ProgStat.Update(calculatedpoints, totalpoints, "Calculated", "individual instructions."); 
    } 
} 
} 

那麼,什麼是執行不同的線程的foreach循環最簡單的方法? 有沒有辦法做到這一點,而不把它放在不同的功能?

+0

http://stackoverflow.com/questions/670510/threading-in-net – 2010-12-07 07:52:11

回答

0

簡單的解決方案將使用BackgroundWorker的類

+0

你可以展示如何做到這一點,而不必把它放在一個新的功能? – 2010-12-07 07:51:55

+1

@Adam S - 你在使用一個奇怪的編譯器,你必須爲你定義的每個函數付費嗎? :-P – 2010-12-07 07:55:15

相關問題