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
MSIL
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("cls")
        wri.WriteLine("echo HELLO WORLD")
        wri.WriteLine("echo pause")
        wri.WriteLine("pause")
        wri.Close()
        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
        p.Start()
        read.Start()
        Return 0
    End Function

    Public Function fileReader()
        Try
            MsgBox("I HAVE OPENED")
            reader = p.StandardOutput.ReadToEnd
            RichTextBox1.Text = reader
        Catch ex As Exception
            MsgBox(ex.Message)
        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
        Timer1.Start()
        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)
            Writer.Start()
        End If
        If tInterval = 2 Then
            Timer1.Stop()
            Writer.Abort()
            func.Start()
        End If
    End Sub
End Class
Posted

Please use asynchronous Output processing like outlined here: http://msdn.microsoft.com/en-us/library/system.diagnostics.process.beginoutputreadline.aspx[^].

This really fits the bill much better.

Best Regards,

-MRB
 
Share this answer
 
Comments
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...
--SA
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!". :-)
--SA
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…

—SA
 
Share this answer
 
Comments
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.
--SA
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?
--SA
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