2014-07-25 2 views
0

코드를 완료하는 데 약 1 분 정도 소요됩니다. 그 때 아무 일도 일어나지 않는다는 표시는 없습니다. 작업이 완료되면 양식의 label.text에 메시지가 표시됩니다. 여기 코드는 다음과 같습니다길게 작동하는 동안 메시지를 표시 하시겠습니까?

private void UpdateLesson() 
{ 
    var bridgeBll = new BridgeBll(); 
    foreach (DataGridViewRow row in this.dataGridView1.Rows) 
    { 
     bridgeBll.UpdateLesson(row); 
    } 
    lblMessage.Text = "Saved at " + DateTime.Now.ToShortTimeString(); 
} 

내가 뭘하려는 것은 ... "저장"같은 것을라는 메시지 박스를 표시하고 작업이 완료되면 다음 가까운 그 메시지 박스가 있습니다. 그러나 문제는 메시지 상자를 열면 사용자가 메시지 상자를 수동으로 닫고 프로그램이 계속 될 때까지 작업이 시작되지 않는다는 것입니다.

어떻게 이런 일을 할 수 있습니까?

+5

진행률 표시 줄을 사용하십시오. % 완료를 표시하려면 행 수를 사용하거나 marque bar를 사용하면됩니다. –

+0

진행률 표시 줄은 사용자에게 더 안심합니다. 특히 메시지 상자가 사라질 때까지 기다리지 않고도 뭔가 진행되고 있음을 알리는 등 예상 된 완료율을 쉽게 표시 할 수 있습니다. – RobH

+0

커서를 대기 커서로 변경하는 것도 좋습니다. 'Mouse.OverrideCursor = Cursors.WaitCursor'. –

답변

0

코드를 다음 양식 사용에 btnStart 버튼과 LABEL1 레이블 넣어 : 여기

public partial class Form1 : Form 
{ 

    MyAsyncClass worker; 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 

    } 

    private void btnStart_Click(object sender, EventArgs e) 
    { 
     label1.Text = string.Format("Worker Started at {0}", DateTime.Now); 
     if (worker == null) 
     { 
      worker = new MyAsyncClass(); 
      worker.NotifyCompleteEvent += worker_NotifyCompleteEvent; 
     } 
     worker.Start(); 
    } 

    void worker_NotifyCompleteEvent(string message) 
    { 
     MessageBox.Show(string.Format("Worker completed with message: {0}", message)); 
    } 

} 

그리고 노동자 클래스 :

public class MyAsyncClass 
{ 

    public delegate void NotifyComplete(string message); 
    public event NotifyComplete NotifyCompleteEvent; 

    public void Start() 
    { 
     System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(DoSomeJob)); 
     t.Start(); 
    } 

    void DoSomeJob() 
    { 
     //just wait 5 sec for nothing special... 
     System.Threading.Thread.Sleep(5000); 
     if (NotifyCompleteEvent != null) 
     { 
      NotifyCompleteEvent("My job is completed!"); 
     } 
    } 
} 

당신은 사용자 인터페이스없이이 원리를 사용할 수 있습니다. 해피 코딩!

관련 문제