another "check my vb.net code for me" thread....

Associate
Joined
8 Mar 2007
Posts
2,176
Location
between here and there
hey all,

I writen this code (below) it use's a background worker to run the 2 dos commands and display them to the listbox as they run.

I had it working well but i made one samll change and now it keeps failing.

the error is;

System.Reflection.TargetInvocationException was unhandled
Message="Exception has been thrown by the target of an invocation."
Source="mscorlib"
StackTrace:
at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Delegate.DynamicInvokeImpl(Object[] args)
at System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme)
at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)
at System.Threading.ExecutionContext.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)
at System.Windows.Forms.Control.InvokeMarshaledCallbacks()
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at Helpdesk_support.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()



here's the code. If anyone knows why this is happening please tell me!!!

Public Class menu2


Private WithEvents testworker As System.ComponentModel.BackgroundWorker



Private Sub start_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles start.Click

start.Enabled = False
Btncancel.Enabled = True
statuslist.Items.Clear()
prgthread.Value = 0
prgthread.Maximum = 2
testworker = New System.ComponentModel.BackgroundWorker
testworker.WorkerReportsProgress = True
testworker.WorkerSupportsCancellation = True
testworker.RunWorkerAsync()
End Sub

Private Sub testworker_dowork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles testworker.DoWork

Dim value As Integer

For value = 0 To 0
Dim listtext As String
Dim args As String = "/c net use \\crc3 /user:meg_swan\tristan.drinkwater password"
Dim p As New Process()
Dim FileName As String = "cmd"
Dim Arguments As String = args

p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardOutput = True
p.StartInfo.RedirectStandardError = True
p.StartInfo.CreateNoWindow = True
p.StartInfo.FileName = FileName
p.StartInfo.Arguments = Arguments
p.Start()

Dim output1 As IO.StreamReader = p.StandardOutput
While (Not output1.EndOfStream)
listtext = String.Concat(output1.ReadLine)
testworker.ReportProgress(value, listtext)
End While

If testworker.CancellationPending Then
Exit Sub

End If
'Threading.Thread.Sleep(10 * 100)
Next

GoTo lable1

lable1:


Dim value1 As Integer

For value1 = 0 To 0
Dim listtext1 As String
Dim args1 As String = "/c copy c:\test.txt \\crc3\c$\nice10.txt"
Dim p As New Process()
Dim FileName1 As String = "cmd"
Dim Arguments1 As String = args1

p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardOutput = True
p.StartInfo.RedirectStandardError = True
p.StartInfo.CreateNoWindow = True
p.StartInfo.FileName = FileName1
p.StartInfo.Arguments = Arguments1
p.Start()

Dim output1 As IO.StreamReader = p.StandardOutput
While (Not output1.EndOfStream)
listtext1 = String.Concat(output1.ReadLine)
testworker.ReportProgress(value1, listtext1)
End While

If testworker.CancellationPending Then
Exit Sub

End If
'Threading.Thread.Sleep(10 * 100)
Next


End Sub

Private Sub testworker_progresschanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles testworker.ProgressChanged
prgthread.Value = prgthread.Value + 1
statuslist.Items.Add(e.UserState)
End Sub



Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btncancel.Click
testworker.CancelAsync()
statuslist.Items.Clear()
prgthread.Value = 0

End Sub

Private Sub testworker_runworkercompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles testworker.RunWorkerCompleted
start.Enabled = True
Btncancel.Enabled = False
End Sub


End Class




cheers guys!
 
having been playing with this for most the afternoon i have found what i think is at falut.

The line;

Code:
testworker.ReportProgress(value1, listtext1)

in the first "for loop"

If it is left where it is, the "commands completed" from the net use command does show in the listbox but it then errors.

If i move it to:

Code:
Dim output1 As IO.StreamReader = p.StandardOutput
While (Not output1.EndOfStream)
listtext = String.Concat(output1.ReadLine)

End While
testworker.ReportProgress(value, listtext)

If testworker.CancellationPending Then
Exit Sub

End If
'Threading.Thread.Sleep(10 * 100)
Next


it connects and doesn't make vb error but it doesn't display in my list box.

i'm stumped!!
 
Last edited:
It's gonna be hell for me to explain (mainly because my English is so poor), but what you're doing is a common problem that everyone who starts to use Threads faces.

In a nutshell, imagine your program is a railway station with several railway tracks with a train on each track. Each train is a thread in your program. If you want both trains running alongside each other at the same time, the tracks will need to be together.

Each track, is your thread.

What you're trying to do is use one thread and update the other, but BEFORE you do that, you need to tell the other thread it's ok to do it.

It's called, Invocation.

The best source of learning I can offer is this : http://forums.vbcity.com/forums/faq.asp?fid=15&cat=Threading&#TID123101

It explains the problem you've got, and how to do what you want to do in dead-easy examples and you'll be surprised how easy it is to do.

Let us know if you get success.
 
nice, cheers ~j~, I think I'll have to back a few stages first.

I noticed on their forum a few free vb.net books.

I'll read up me thinks.

cheers again
 
The free Vb.Net books are really good, and I must plug the site too because they are absolutely brilliant. I'd seriously recommend you sign up with them, never met a more friendlier group of people who are VERY VERY patient and know exactly what they are talking about.

Hope you get it sorted. :)
 
I've already signed up. I downloaded some source code to try and help with my understanding, but I've got a deadline to have this project finished by so i might have to get it working and then find out how and why afterwards (not ideal i know). Then once a working app has been released i have time to re-do it making it better.

cheers dude
 
Back
Top Bottom