AccessViolationException在C#中的多线程形式 [英] AccessViolationException in C# multithreaded form

查看:204
本文介绍了AccessViolationException在C#中的多线程形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,这是我的第一个C#项目。我一直在做这个项目的6个月左右。

First off this is my first C# project. I have been working on this project for about 6 months.

我们有一个WinForms程序,它包含了一个日志GUI。为了保持程序响应的其余部分,我想在一个单独的线程创建日志贵,因为它可以是相当密集的时候很多东西是怎么回事。

We have a winforms program and it contains a logging GUI. In order to keep the rest of the program responsive I wanted to create the logging gui on a separate thread since it can be quite intensive when lots of stuff is going on.

这是我尝试在一个新的GUI线程打开窗体。一般来说它的工作原理,并保持主界面响应。但是我们现在随机得到一个AccessViolationException( http://pastebin.com/7tLtBSei )时,这是激活和我处于亏损状态。

This is how I attempted to open the the form on a new GUI thread. In general it works and keeps the main gui responsive. However we now randomly get a AccessViolationException (http://pastebin.com/7tLtBSei) when this is activated and I am at a loss.

var thread = new Thread(() =>
{
    loggingForm = new LoggingForm(Logger.path);
    Application.Run(loggingForm);
});
thread.Name = "LoggingFormGUIThread";
thread.Start();

日志记录GUI只有一批读取日志文件并将其追加到一个RichTextBox。它不触及任何管理code。

The logging GUI just batch reads the log file and appends it to a RichTextBox. It doesn't touch any managed code.

推荐答案

您需要的线程的单元状态设置为STA。

You need to set the apartment state of the thread to STA.

thread.SetApartmentState(ApartmentState.STA);
thread.Name = "LoggingFormGUIThread";
thread.Start();

这是需要很多的用户界面组件(如的RichTextBox )才能正常工作。

This is required for many user interface components (such as RichTextBox) to function correctly.

这篇关于AccessViolationException在C#中的多线程形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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