axacropdflib - set source from worker thread - vb.net

I have a vb.net project which has a treeview of pdf's on the left and the acrobat AxAcroPDF viewer control on the right. Click a item in the treeview, I get the fileinfo.fullname value and pass that over to the AxAcroPDF src property.
While testing I noticed that pdf's were slow to load and would block my ui thread so I decided that a workerthread would be a good helper to lazy load these pdf's in the background.
When I run my code with the worker thread's DoWork method and it tries to update my pdfviewer object I get an invalid cast exception.
System.InvalidCastException was caught HResult=-2147467262
Message=Unable to cast COM object of type 'System.__ComObject' to
interface type 'AcroPDFLib.IAcroAXDocShim'. This operation failed
because the QueryInterface call on the COM component for the interface
with IID '{3B813CE7-7C10-4F84-AD06-9DF76D97A9AA}' failed due to the
following error: No such interface supported (Exception from HRESULT:
0x80004002 (E_NOINTERFACE)). Source=mscorlib StackTrace:
at System.StubHelpers.StubHelpers.GetCOMIPFromRCW(Object objSrc, IntPtr pCPCMD, IntPtr& ppTarget, Boolean& pfNeedsRelease)
at AcroPDFLib.IAcroAXDocShim.set_src(String pVal)
at AxAcroPDFLib.AxAcroPDF.set_src(String value)
at myapp.fill_treeview_with_filesfolders_docked_andthreads.LoadPDFInBackground(String
selectedfile) in
C:\Users\me\Desktop.....\fill_treeview_with_filesfolders_docked_andthreads.vb:line
84 InnerException:
I can't find any other threads online with this exception detail so I am not sure what the issue is here. I thought my problem had to do with a cross thread access violation but even if I set Control.Checkforillegalcrossthreadcalls to false I get the same exception. It didn't make sense to me that I would check for invokerequired from the DoWork routine anyways because the point of my worker thread is to handle the load for me, not shove it back into the UI thread.
Can anyone recommend a workaround that I can try to achieve what I am after here?
my Code:
The treeview afterselect is wired to displayfile
AddHandler TreeView.AfterSelect, AddressOf displayfile
Private Sub displayfile(sender As Object, e As TreeViewEventArgs)
Try
Dim selectedfile As FileInfo = New FileInfo(e.Node.Tag) 'tag has our full path embedded.
'todo: Future - consider type of the file and load a pre-made panel with appropriate host object
If selectedfile.Extension.ToLower.Equals(".pdf") Then
'show "loading...."
LoadingPanel.BringToFront()
backgroundworker.RunWorkerAsync(selectedfile.FullName)
End If
Catch ex As Exception
End Try
End Sub
Background Worker Stuff:
#Region "Background Worker Events"
' This event handler is where the time-consuming work is done.
Private Sub backgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As DoWorkEventArgs) Handles backgroundworker.DoWork
Dim worker As BackgroundWorker = CType(sender, BackgroundWorker)
e.Result = LoadPDFInBackground(e.Argument)
End Sub
' This event handler updates the progress.
Private Sub backgroundWorker_ProgressChanged(ByVal sender As System.Object, ByVal e As ProgressChangedEventArgs) Handles backgroundworker.ProgressChanged
ProgressBar.Value = e.ProgressPercentage
End Sub
' This event handler deals with the results of the background operation.
Private Sub backgroundWorker_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As RunWorkerCompletedEventArgs) Handles backgroundworker.RunWorkerCompleted
If e.Result Then
'hide loading panel and show pdf panel
pdfviewer.BringToFront()
Else
'what to do if failed to load???
End If
End Sub
#End Region
Private Function LoadPDFInBackground(ByVal selectedfile As String) As Boolean
Try
pdfviewer.src = selectedfile
Return True
Catch ex As Exception
Return False
End Try
End Function

Just a thought, but try changing this line:
pdfviewer.src = selectedfile
to the following:
If pdfviewer.InvokeRequired Then
pdfviewer.Invoke(Sub() pdfviewer.src = selectedfile)
It might work around the error. Interesting to see if it does.

Related

WinForms.IllegalCrossThreadCall with filewatcher

I'm new to Visual Basic and overall kind of new to coding in general.
Currently I work on a program which uses a filewatcher. But If I try this:
Public Class Form1
Private WithEvents fsw As IO.FileSystemWatcher
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
fsw = New IO.FileSystemWatcher("PATH")
fsw.EnableRaisingEvents = True
' fsw.Filter = "*.settings"
End Sub
Private Sub GetSettingsFromFile()
Some Code
More Code
CheckBox1.Checked = True
End Sub
Private Sub fsw_Changed(sender As Object, e As FileSystemEventArgs) Handles fsw.Changed
fsw.EnableRaisingEvents = False 'this is set because the file is changed many times in rapid succesion so I need to stop the Filewatcher from going of 200x (anyone has a better idea to do this?)
Threading.Thread.Sleep(100)
GetSettingsFromFile()
fsw.EnableRaisingEvents = True 'enabling it again
End Sub
End Class
But when I do this (trying to change anyhting in the form) I get this error:
System.InvalidOperationException (WinForms.IllegalCrossThreadCall)
It wont stop the program from working, but I want to understand what is wrong here and why the debugger is throwing this at me
regards
The event is being raised on a secondary thread. Any changes to the UI must be made on the UI thread. You need to marshal a method call to the UI thread and update the UI there. Lots of information around on how to do that. Here's an example:
Private Sub UpdateCheckBox1(checked As Boolean)
If CheckBox1.InvokeRequired Then
'We are on a secondary thread so marshal a method call to the UI thread.
CheckBox1.Invoke(New Action(Of Boolean)(AddressOf UpdateCheckBox1), checked)
Else
'We are on the UI thread so update the control.
CheckBox1.Checked = checked
End If
End Sub
Now you simply call that method wherever you are and whatever thread you're on. If you're already on the UI thread then the control will just be updated. If you're on a secondary thread then the method will invoke itself a second time, this time on the UI thread, and the control will be updated in that second invocation.

Cant load image in context menu as icon

I am trying to use the code from the following link:
VB- Helper Create menu items at run time with images, shortcut keys, and event handlers in Visual Basic .NET
The only difference is that I want a local image and not one from my.Recources
What I have is the following:
''Tool 2 displays a string and image.
Dim tool2 As New ToolStripMenuItem("Tool 2", (Image.FromFile("C:\test\icon.jpg")))
tool2.Name = "mnuToolsTool2"
tool2.ShortcutKeys = (Keys.D2 Or Keys.Control) ' Ctrl+2
AddHandler tool2.Click, AddressOf mnuTool2_Click
ToolStripMenuItem1.DropDownItems.Add(tool2)
I could not reproduce this "error". However, from the given text, code and link, my best guess is as follows:
You are using a 64 bit machine.
You run the code inside the Form.Load event.
An error occurs somewhere in this method.
Private Sub _Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Code...
Throw New Exception("ooops..")
'Code...
End Sub
As you might not know is that errors thrown in the Form.Load on a 64 bit machine are "swallowed" by the system.
For more information, read this SO post: Why the form load can't catch exception?
You should move your code inside the constructor:
Public Sub New()
Me.InitializeComponent()
'Code goes here...
End Sub
Or change to the Form.Shown event:
Private Sub _Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
Try
'Code goes here...
Catch ex As Exception
MessageBox.Show(ex.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub

backgroundworker throws "An error occurred creating the form..."

I have main forms that has a button that opens a master-customer on datagrid. I use dataview for the purpose of filtering the data using dataview.rowfilter.
The problem is, during the form load. It takes 5-6 seconds (the program is unresponsive during that time). What I'm trying to do is to load the data to the dataview on the background and show it on the gridview on workercompleted.
it gave me this error: "An error occurred creating the form. See Exception.InnerException for details. The error is: Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it." --> on dowork
I read somewhere that i should use Invoke. But i don't know how to use it.
here is my code:
Private Sub custcall_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TBfind.Enabled = False
SetMyCustomFormat("yyyy-MM-dd HH:mm:ss")
BWcustload.RunWorkerAsync()
End Sub
Private Sub BWcustload_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BWcustload.DoWork
mydataview = New DataView(datatablecust)
End Sub
Private Sub BWcustload_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BWcustload.RunWorkerCompleted
DGVcustomer.DataSource = mydataview
TBfind.Enabled = True
End Sub
Have you tried moving the code to the event Form_Shown?
Also, have you tried putting the code in your DoWork section inside a SyncLock?
Try this:
SyncLock mydataview
mydataview = New DataView(datatablecust)
End SyncLock
As for the STA model ... try adding this to your code and set the startup object to Sub Main()
<STAThread()> _
Public Shared Sub Main()
Dim mainForm As New custcall()
Application.Run(mainForm)
End Sub
EDITED:
As far as invoke is concerned. ... that would definitely work, too ... but I don't know that it would have solved the STAThread issue.
To use invoke, first you have to declare a delegate sub in your form:
Delegate Sub LoadDataCallback()
Declare a function to take care of the actual loading of the data:
Private Sub LoadData()
mydataview = New DataView(datatablecust)
End Sub
Then, you would start a new thread in your Shown event (or Load event):
Dim mythread As New Thread(Sub()
Dim callLoad As New LoadDataCallBack(LoadData)
Me.Invoke(callLoad)
End Sub)
mythread.Start()
This gets around having to use SyncLock (although in some cases you may want to if it doesn't end up working correctly).
Don't forget to add Imports System.Threading to the top of your code file.

SerialPort and Control Updating in MDI form

As my title implies i have the following problem, i am receiving data from serial port and i update a richtextbox in a MDI Form with the control.invoke method
(Code in SerialPort.DataReceived Event)
If myTerminal.Visible Then
myTerminal.MyRichTextBox1.Invoke(New MethodInvoker(Sub()
myTerminal.MyRichTextBox1.AppendText(dataLine & vbCrLf)
End Sub))
End If
But as a mdi form it has the ability to close and reopen. So when the serialport is sending data to richtextbox and the user click the close button and the form gets disposed. Then the error "Invoke or BeginInvoke cannot be called on a control until the window handle has been created."... Any Idea????
My regards,
Ribben
That code is not in the SerialPort.DataReceived event it is in the event handler. (Yes, I'm nitpicking, but it points to a solution.) The best thing to do is have the form that owns myTerminal add the handler when it is created and remove the handler when it closes.
Thank you for your answer but unfortunately that's not the solution. First of all my SerialPort Class must inform 2 Forms (Form with richtextbox, Form with Listview) and another class which is responsible for drawing (Unmanaged Directx 9.0c about 4 Forms), so to implement right the serialport class i have made my own events. Again to the problme, it caused because the Serialport.DataReceived everytime it occurs creates a thread in the threadpool and when i dispose the form simply it's too slow to catch up with all the threads and so there is at least one thread which invokes the control which is already disposed!
As a temp solution i came up with (The Below code is in the TerminalForm Class which inherits Form):
Private VisibleBoolean As Boolean = False
Private Index As Integer = 0
Private Sub DataToAppend(ByVal _text As String)
If VisibleBoolean Then
Me.MyRichTextBox1.Invoke(New MethodInvoker(Sub()
Me.MyRichTextBox1.AppendText(_text & vbCrLf)
End Sub))
ElseIf Index = 1 Then
Index = 0
myDispose()
RemoveHandler myserialport.DataToSend2, AddressOf DataToAppend
End If
End Sub
Private Sub Me_Activated(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Activated
VisibleBoolean = True
AddHandler myserialport.DataToSend2, AddressOf DataToAppend
End Sub
Private Sub myDispose()
If Index = 0 And Not Me.IsDisposed Then
Me.Invoke(New MethodInvoker(Sub()
MyBase.Dispose(True)
End Sub))
End If
End Sub
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
End Sub
Protected Overrides Sub OnFormClosing(ByVal e As System.Windows.Forms.FormClosingEventArgs)
Index = 1
VisibleBoolean = False
End Sub
I know i don't like either but at least it's working!
Anyother improvement or suggestion is more

VB.NET Cross-thread operation not valid

I have a loop (BackgroundWorker) that is changing a PictureBox's Location very frequently, but I'm getting an error -
Cross-thread operation not valid: Control 'box1' accessed from a thread other than the
thread it was created on.
I don't understand it at all, so I am hoping someone can help me with this situation.
Code:
box1.Location = New Point(posx, posy)
This exception is thrown when you try to access control from thread other than the thread it was created on.
To get past this, you need to use the InvokeRequired property for the control to see if it needs to be updated and to update the control you will need to use a delegate. i think you will need to do this in your backgroundWorker_DoWork method
Private Delegate Sub UpdatePictureBoxDelegate(Point p)
Dim del As New UpdatePictureBoxDelegate(AddressOf UpdatePictureBox)
Private Sub UpdatePictureBox(Point p)
If pictureBoxVariable.InvokeRequired Then
Dim del As New UpdatePictureBoxDelegate(AddressOf UpdatePictureBox)
pictureBoxVariable.Invoke(del, New Object() {p})
Else
' this is UI thread
End If
End Sub
For other people which coming across this error:
Try the dispatcher object: MSDN
My code:
Private _dispatcher As Dispatcher
Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
_dispatcher = Dispatcher.CurrentDispatcher
End Sub
Private Sub otherFunction()
' Place where you want to make the cross thread call
_dispatcher.BeginInvoke(Sub() ThreadSafe())
End Sub
Private Sub ThreadSafe()
' here you can make the required calls
End Sub