471,123 Members | 873 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,123 software developers and data experts.

BackgroundWorker Problem


I would like to be able to pass the BackgroundWorker object and DoWork Event
Args to a second function (third function?) and be able to still report the
progress.

I'm getting the following exception when trying to access
BackgroundWorker.CancellationPending or e.Cancel

An unhandled exception of type 'System.Reflection.TargetInvocationException'
occurred in mscorlib.dll
Additional information: Exception has been thrown by the target of an
invocation.

The problem is I can't stuff everything into the DoWork event to accomplish
what I want, but need to call another function which inturn calls a third
function.

I'm sure a code example would better explain:

main()

Dim bw as new BackgroundWorker

'Set bw properties (reports progress/support cancelation and call
DoWorkAsynch

End main
Sub bw_DoWork(sender As Object, e As DoWorkEventArgs) handles bw.DoWork

'Get the BackgroundWorker object that raised this event.
Dim worker as BackgroundWorker = cType(sender, BackgroundWorker)

'I have multiple arguments to pass
Dim args As EncryptFileArgs = cType(e.Argument, EncryptFileArgs)

e.Result = mEncryptFile(args.fileIn, args.fileOut, args.keySize, _
args.password, args.fileExpireTime, worker, e)

End sub

Function mEncryptFile( fileIn, fileOut,keySize, _
password, bworker as BackgroundWorker, e as
DoWorkEventArgs) as Boolean

'I need to do some pre-processing here

return EncryptFile(keys.IV, keys.KQ,
keySize, fileIn,
ref fsOut, bworker, e)

End Function
'The actual encryption/work goes here
EncryptFile(keys.IV, keys.KQ,
keySize, fileIn,
ref fsOut, bworker, e)

'Report Progress
bw.ReportProgress(percent)

'Check if user canceled 'This line throws the exception
If bw.CancellationPending Then
e.Cancel = true

End Function

Thanks in advance for any help,

Chris

Apr 9 '07 #1
0 2304

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by dm1608 | last post: by
2 posts views Thread by Sebastian Crewe | last post: by
4 posts views Thread by =?Utf-8?B?U3ZlbiBXLg==?= | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.