The BackgroundWorker or "How to make responsive WPF windows"
Posted: Sun Dec 11, 2016 10:54 am
Unfortunately, in WPF the AppExec() call does not work if you start a longer process from your window.
The first approach would be to use ASYNC/AWAIT, but unfortunately this works only if you are using some methods that are implementing this.
For own processings, the BackgroundWorker class (from System.ComponentModel) seems to be a better solution.
Please look at this code (how to start such a processing):
and the relative AsyncProcess implementation:
You can find the complete working program as attachment to this message - it is a zipped XIDE export file.
Wolfgang
The first approach would be to use ASYNC/AWAIT, but unfortunately this works only if you are using some methods that are implementing this.
For own processings, the BackgroundWorker class (from System.ComponentModel) seems to be a better solution.
Please look at this code (how to start such a processing):
Code: Select all
method StartButtonClick( oSender as object, e as RoutedEventArgs ) as void
if _oWorker == null
_oWorker := BackgroundWorker{}
_oWorker:WorkerReportsProgress := true
_oWorker:WorkerSupportsCancellation := true
_oWorker:DoWork += ExecuteProcessAsync
_oWorker:ProgressChanged += ProgressProcessAsync
_oWorker:RunWorkerCompleted += CompletedProcessAsync
_oWorker:RunWorkerAsync()
endif
return
method ExecuteProcessAsync( oSender as object, e as DoWorkEventArgs ) as void
local oProcess as AsyncProcess
oProcess := AsyncProcess{ _oWorker }
oProcess:Process()
return
Code: Select all
class AsyncProcess
protect _oWorker as BackgroundWorker
constructor( oWorker as BackgroundWorker )
_oWorker := oWorker
return
method Process() as void
local nLoop as int
local nInner as int
local nMax as int
local cString as string
local cMessage as string
nMax := 32000
for nLoop := 1 upto nMax
for nInner := 1 upto nMax
cString := string{ 'X', nMax }
next
cMessage := String.Format( "round {0} of {1}", nLoop:ToString(), nMax:ToString() )
_oWorker:ReportProgress( ( nLoop * 100 / nMax ), cMessage )
Thread.Sleep( 100 )
if _oWorker:CancellationPending
exit
endif
next
_oWorker:ReportProgress( ( nLoop * 100 / nMax ), "terminated" )
end class
Wolfgang