跨线程错误 [英] Cross Threading Errors

查看:78
本文介绍了跨线程错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮,一个丰富的文本框和一个过程.我想在富文本框中显示进程的输出,我正在使用以下代码.请帮助

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("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

推荐答案

请使用异步输出处理,如下所示:http://msdn.microsoft.com/en-us/library/system.diagnostics.process. beginoutputreadline.aspx [ ^ ].

这确实更适合该法案.

最好的问候,

-MRB
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


这是对技术的严重滥用.您的问题看起来很简单,但是您正在做很多疯狂的事情.这是意大利面,我不想完整分析.您绝不应该做您想做的大多数事情.

您将在每次更改标签文本时创建一个新线程,但是该文本已更改,但是标签文本已被计时器更改.从这些线程的每一个中,您尝试在同一文件中写入相同的文本.您使用tInterval的硬编码值0、1和2来控制计时器的启动/停止.哇!您根本不应该使用计时器(一定要避免使用计时器),一个线程就足够了.无论如何,您的最终目标是什么?要每秒产生尽可能多的麻烦?

这是完全荒谬的,无法修复.扔掉它(除非您想使用它来拖钓,否则将成为一个很棒的工具:-)),想一想您在做什么……

—SA
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


这篇关于跨线程错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆