开发者

How can open print dialog box in another thread

If i simply call

If PrintDialog1.ShowDialog = DialogResult.OK Then
                                        PrintDocument1.Print()
                                    End If

its working fin开发者_Python百科e but if i use this function in another thread then it will shows error

{"External component has thrown an exception."}


You have to call SetApartmentState on the thread to switch it to STA before you start the thread. This is not possible if the thread is a threadpool thread or if you are using BackgroundWorker.

This is otherwise a bad idea, the dialog won't have a parent and is likely to disappear behind another window. Nor will it act modal. By far the best solution is to display this dialog by code that runs on the main thread. The actual printing can still take place on the worker thread. Use Control.Invoke() as required.


External threads need to Invoke requests on main UI. You can't directly make a call from a Thread to make UI changes.


Create object of PrintDialog in new thread & then call

Dim myPDia As New PrintDialog
 If myPDia.ShowDialog() = Windows.Forms.DialogResult.OK Then
    PrintDocument1.Print()
 End If
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜