开发者

UI updates using BeginInvoke hung until thread completes?

I have 1 class (acBL) which handles 2 threads (fpDoWork). Any work that is done in fpDoWork will trigger an event back to the acBL class. On my form 开发者_高级运维I declare the acBL class and associate Event handlers on acBL - this way whenever the event is called, it 'should' update the UI with changes.

What is not happening is, when each fpDoWork thread is started, it performs the operations, the ProcessingEvent is called and does go into the frmMain.handlerProcessing1 event. It gets to the point where this.BeginInvoke(new Processing2Event (handlerProcessing2), status) is called, then it just hangs and waits until the thread has finished its work before continuing to update the UI. I tried this.Invoke, but that method just seems to hang. Any ideas?

Code inside frmMain

Earlier in the code:

this.acBL.Processing1Event += new acBL.Processing1(handlerProcessing1);
this.acBL.Processing2Event += new acBL.Processing2(handlerProcessing2);

Handlers:

private void handlerProcessing1(string status) {
  if (InvokeRequired) {
    this.BeginInvoke(new MethodInvoker(this.Refresh));
    this.BeginInvoke(new Processing1Event (handlerProcessing1), status);

  }
  else {
    Console.WriteLine("UPDATING 1: "+status);
    lblForProcessing1.Text = status;
    this.Refresh();
    return;
  }
}


private void handlerProcessing2(string status) {
  if (InvokeRequired) {
    this.BeginInvoke(new MethodInvoker(this.Refresh));
    this.BeginInvoke(new Processing2Event (handlerProcessing2), status);

  }
  else {
    Console.WriteLine("UPDATING 2: "+status);
    lblForProcessing2.Text = status;
    this.Refresh();
    return;
  }
}

Code inside acBL

In a main method:

bool thread1Complete = false;
fpDoWork fpDoWork1 = new fpDoWork();
fpDoWork1.GUIForm = frmMain;
fpDoWork1.ProcessingEvent += new fpDoWork.Processing(handlerProcessing1Event);
fpDoWork1.ThreadCompleteEvent += new fpDoWork.ThreadComplete(thread1Complete);
Thread fpDoWork1Thread= new Thread(new ThreadStart(fpDoWork1.StartWork));

bool thread2Complete = false;
fpDoWork fpDoWork2 = new fpDoWork();
fpDoWork2.GUIForm = frmMain;
fpDoWork2.ProcessingEvent += new fpDoWork.Processing(handlerProcessing2Event);
fpDoWork2.ThreadCompleteEvent += new fpDoWork.ThreadComplete(thread2Complete);
Thread fpDoWork2Thread= new Thread(new ThreadStart(fpDoWork2.StartWork));

Console.WriteLine("Work for 1 Thread started...");
fpDoWork1Thread.Start();
Console.WriteLine("Work for 2 Thread started...");
fpDoWork2Thread.Start();

while (!thread1Complete && !thread2Complete ) {
  if (!thread1Complete && !thread2Complete ) {
    Console.WriteLine("Waiting for both copying threads...");
  }
  else if (!thread1Complete && thread2Complete ) {
    Console.WriteLine("Waiting for thread 1...");
  }
  else if (thread1Complete && !thread2Complete ) {
    Console.WriteLine("Waiting for thread 2...");
  }
  Thread.Sleep(1000);
}
Console.WriteLine("All done");

Else where in the code:

public delegate void ProcessingFor1 (string filename);
public delegate void ProcessingFor2 (string filename);
public event ProcessingFor1 ProcessingEventFor1;
public event ProcessingFor2 ProcessingEventFor2;


private void handlerProcessing1Event(string filename) {
  Console.WriteLine("Processing 1: " + filename);
  ProcessingEventFor1(filename);
}

private void handlerProcessing1Event(string filename) {
  Console.WriteLine("Processing 2: " + filename);
  ProcessingEventFor2(filename);
}


the reason your UI is not updating is that the primary UI thread is stuck in your while() loop. BackgroundWorker is your friend

try to avoid creating threads in one thread and sit there waiting for them to finish, its a bad use of threads in the first place

cheers

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜