Click here to Skip to main content
15,882,152 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a button,a rich text box and a process. I want to show to output of process in the rich text box I am Using the Following code. Please Help
Imports System.IO
Imports System.Threading
Public Class Form1
    Public tInterval As Integer
    Public Writer As Thread
    Public func As Thread = New Thread(AddressOf processStarter)
    Public read As Thread = New Thread(AddressOf fileReader)
    Public a As Integer
    Public p As Process = New Process
    Public reader As String
    Public Function fileWriter()
        Dim wri As StreamWriter
        wri = File.CreateText(Application.StartupPath + "\fix.bat")
        wri.WriteLine("@echo off")
        wri.WriteLine("echo HELLO WORLD")
        wri.WriteLine("echo pause")
        a = 1
        Return a
    End Function
    Public Function processStarter()
        p.StartInfo.FileName = Application.StartupPath + "\fix.bat"
        p.StartInfo.Arguments = ""
        p.StartInfo.UseShellExecute = False
        p.StartInfo.RedirectStandardOutput = True
        p.StartInfo.Verb = "runas"
        p.StartInfo.WindowStyle = ProcessWindowStyle.Maximized
        Return 0
    End Function

    Public Function fileReader()
            MsgBox("I HAVE OPENED")
            reader = p.StandardOutput.ReadToEnd
            RichTextBox1.Text = reader
        Catch ex As Exception
        End Try
        Return 0
    End Function
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Timer1.Enabled = True
        Button1.Text = "Please Wait.."
        Button1.Enabled = False
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        tInterval = 0
        a = 0
    End Sub
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        tInterval = tInterval + 1
        Label1.Text = Str(tInterval)
    End Sub
    Private Sub Label1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.TextChanged
        If tInterval = 1 Then
            Writer = New Thread(AddressOf fileWriter)
        End If
        If tInterval = 2 Then
        End If
    End Sub
End Class

Please use asynchronous Output processing like outlined here:[^].

This really fits the bill much better.

Best Regards,

Share this answer
Sergey Alexandrovich Kryukov 19-Apr-11 0:20am    
Good advice, my 5, only... completely useless. I'm almost sure you did not read the code, I and don't blame you, who would read such thing. I took time to read a bit... Wow! I rarely saw such a concentration of absurdity.
If you look at my Answer, it could be more clear...
Manfred Rudolf Bihy 19-Apr-11 4:37am    
You bet your sweet life I didn't fully read OP's code. I got a little nauseous when I started and that feeling grew on me before I got halfway through, so I decided to quit. Besides that the code is written in V- :choke: :cough: :gurgling-sound: ...
Sergey Alexandrovich Kryukov 19-Apr-11 12:31pm    
I saw one weird thing; what to write about? -- made a deep breath: "oh, not again!". :-)
Espen Harlinn 19-Apr-11 8:26am    
Good link, will hopefully help OP when he's rewriting his code :)
This is a heavy abuse of technologies. Your problem looks so simple, but you're doing a number of crazy things. This is spaghetti which I don't want to analyze in full. You should never do most of the things you're trying to do.

You're creating a new thread on each change of the label text, but the text is changed, but the label text is changed by timer. From each of those threads you try to write the same text in the same file. You use hard-coded values of 0, 1 and 2 of tInterval to control timer start/stop. Wow! You should not use timer at all (avoid it by all means, by the way), one thread is quite enough. What is your ultimate goal anyway? To generate as many trouble per second as you can?

This is complete absurdity, beyond repair. Throw it out (unless you want to use it for trolling, would make a wonderful tool :-)), think of what are you doing…

Share this answer
Manfred Rudolf Bihy 19-Apr-11 4:39am    
Agree FUBAR code: F***d up beyond all repair! 5+
Sergey Alexandrovich Kryukov 19-Apr-11 12:21pm    
Thank you, Manfred.
Manfred Rudolf Bihy 19-Apr-11 4:40am    
Seems like OP didn't take your criticism all to well!
You'll have to do with my 5 then. :)
Sergey Alexandrovich Kryukov 19-Apr-11 12:26pm    
Thank you.
Do you mean the vote of 1? I'm not sure.
Not taking criticism can be OK, but in this ***this*** case it will lead OP nowhere. And who would be the looser?
Espen Harlinn 19-Apr-11 8:25am    
My 5, a rewrite wouldn't hurt :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900