2011-12-13 2 views
-1

저는 VS2010에 C# windows 폼 어플리케이션을 작성하고 있습니다. 많은 계산을하는 함수가 있습니다. 그리고이 함수로 button을 누르면 프로그램은 계산이 끝날 때까지 모든 클릭에 응답하지 않습니다.다른 스레드에서 계산하기

어떻게 Window Form을 만들 수 있으며 이러한 계산을 병렬로 실행할 수 있습니까?

+2

당신이 그물 스레드에 대해 Google에 시도 해 봤나? – zerkms

+0

이 튜토리얼을 며칠 전에 사용해 보았습니다. http://msdn.microsoft.com/en-us/library/aa645740(v=vs.71).aspx – jValdron

답변

0

아마도 BackgroundWorker을 사용해야합니다. 문서에서 피보나치 계산기 샘플을보십시오.

+0

예, 50 개 이상의 스레드를 계획하지 않는 한 :) – Maess

+0

@Maess는 CLR afaik의 이전 버전에만 해당됩니다. – alexn

1
  • 당신은 사용하기 쉬운 backgroundworker을 사용할 수 있습니다 - 덜
    오버 헤드가 새 thread을 사용하는 경우에 비해, 스레드에 내장 된 사용합니다.

  • action과 같은 구분을 사용하고 begininvoke.을 콜백으로 호출 할 수도 있습니다.

예 (BackgroundWorker에) :

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      var back = new BackgroundWorker(); 
      back.DoWork += new DoWorkEventHandler(back_DoWork); 
      back.RunWorkerCompleted += new RunWorkerCompletedEventHandler(back_RunWorkerCompleted); 
      back.RunWorkerAsync(); 
     } 

     //Do work here (not safe to call control elemnts here - if so, use this.invoke(deligate); 
     private decimal myResult = 0; 
     void back_DoWork(object sender, DoWorkEventArgs e) 
     { 
      myResult = 5 + 5; 
     } 

     //Update form here - threadsafe 
     void back_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 
     { 
      label1.Text = "result: " + myResult.ToString(); 
     } 
    } 
} 

예 begininvoke :

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     Action DoWork = new Action(CalcHere); 
     DoWork.BeginInvoke(new AsyncCallback(CallBack), null); 
    } 

    //Work here 
    private decimal myResult = 0; 
    private void CalcHere() 
    { 
     myResult = 5 + 5; 
    } 

    //callback here 
    private void CallBack(IAsyncResult result) 
    { 
     this.Invoke((Action)(() => { label1.Text = "Result: " + myResult.ToString(); })); 
    } 
} 
0
var calculations = new Thread(() => DoCalculations()); 

private void DoCalculations() { 
    // Do long work 
    // ... 
} 
0

월 당신을 위해이 일? 당신의 onButtonClick에

public class Calculator 
{ 
    public void doHeavyCalculations() 
    { 
     //Do what you want to here. 
    } 

}; 

, 당신은 할 수 있습니다 :

Calculator calc = new Calculator(); 
Thread mThread = new Thread(new ThreadStart(calc.doHeavyCalculations)); 

mThread.Start(); //This will start the work 

/* 
Make some code to show a messagebox to tell users to wait while the calculations are being done. Dont forget to tell the form to update, so it'll show the progress. 
*/ 
+0

이 경우 새 스레드를 사용하기 위해 오버 헤드가 발생합니다. Backgroundworker가 선호하는 것입니다. – Niklas

관련 문제