Anywhere you go, let me go too

關於部落格
對人海闊天空,做事仔細周密
----------------------
因為改了平台後...覺得不是很好用....所以有另外......(評估中)
http://blog.xuite.net/king119wang/myskills
  • 32543

    累積人氣

  • 2

    今日人氣

    0

    訂閱人氣

How to DoEvents in WPF

 找了好多網站......試了好多....下面這個是可以work的,但還是要改一下.....

參考自:http://blogs.microsoft.co.il/blogs/tamir/archive/2007/08/21/How-to-DoEvents-in-WPF_3F00_.aspx

f you want to test this code and see something, put Thread.Sleep(1000); after label1.text... :) Just in case
 
But in WPF we have no DoEvents() method in application class? What to do? Well, we know, what Dispatcher is. We also know, that it use DispatcherFrame to pump messages, so, why not create our own DoEvents?
 
void DoEvents(){
DispatcherFrame f = new DispatcherFrame();
Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, 
(SendOrPostCallback)delegate(object arg) {
    DispatcherFrame fr =  arg as DispatcherFrame;
    fr.Continue=False;  ---> 這是重點
}, f);
Dispatcher.PushFrame(frame);
}
 
Now, by using this method, we'll release message pump and make our long asynchronous methods not block dispatcher thread, but still wait for the end of execution. Here the example how to do it.
 
DispatcherOperation op = Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background,
(DispatcherOperationCallback)delegate {
    //DoSomethingReallyLong  ----> 這個block就是寫你想另外處理的事
    int res = 1;
    int pre = -1;
    for(int i=0;i<1000;++i) {
     int sum = res + pre;
    pre = res;
    res = sum;
     }
    return res; 
},null);
 
while(op.Status != DispatcherOperationStatus.Completed) {
     DoEvents();
}
 
相簿設定
標籤設定
相簿狀態