Tag:

ThreadStateException c#

我有一个ThreadStateException,我需要有STAThread …问题出现昨天,我甚至从我的git repo(100%工作)ckecked以前的版本 – 现在他们不是。 主要代码: [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new ClientList()); } 和ClientList表单方法: private void button2_Click(object sender, EventArgs e) { […] OpenFileDialog ofd = new OpenFileDialog(); DialogResult result = ofd.ShowDialog(); […] } 知道为什么STA不能工作吗? 编辑:在新的(测试)应用程序中一切正常。 只有这个项目会在OpenFileDialog上抛出exception。 编辑2: getAppartmentState显示应用程序位于Main的第一行MTA中。 [STAThread]被忽略了吗?

为什么Microsoft的示例代码崩溃了?

我想了解一下.NET中的multithreading,并从MSDN中获取此示例。 它编译很好但在运行时崩溃。 我希望微软会告诉我创建多个线程的正确方法。 我无法理解为什么它崩溃了。 有人可以帮忙吗? // Mutex.cs // Mutex object example using System; using System.Threading; public class MutexSample { static Mutex gM1; static Mutex gM2; const int ITERS = 100; static AutoResetEvent Event1 = new AutoResetEvent(false); static AutoResetEvent Event2 = new AutoResetEvent(false); static AutoResetEvent Event3 = new AutoResetEvent(false); static AutoResetEvent Event4 = new AutoResetEvent(false); public […]

Interlocked.CompareExchange真的比简单的锁更快吗?

我遇到了.NET 3.5的ConcurrentDictionary实现(我很抱歉,我现在可以找到链接),它使用这种方法进行锁定: var current = Thread.CurrentThread.ManagedThreadId; while (Interlocked.CompareExchange(ref owner, current, 0) != current) { } // PROCESS SOMETHING HERE if (current != Interlocked.Exchange(ref owner, 0)) throw new UnauthorizedAccessException(“Thread had access to cache even though it shouldn’t have.”); 而不是传统的lock : lock(lockObject) { // PROCESS SOMETHING HERE } 问题是:有没有真正的理由这样做? 它更快还是有一些隐藏的好处? PS:我知道在一些最新版本的.NET中有一个ConcurrentDictionary但我不能用于遗留项目。 编辑 : 在我的具体情况下,我所做的只是以一种线程安全的方式操作内部Dictionary类。 例: public bool […]

单线公寓问题

从我的mainform我打电话给以下打开一个新表格 MyForm sth = new MyForm(); sth.show(); 一切都很好但是这个表单有一个combobox,当我将其AutoCompleteMode切换为建议和附加时,我在显示表单时遇到了这个exception: 在进行OLE调用之前,必须将当前线程设置为单线程单元(STA)模式。 确保您的Main函数标记了STAThreadAttribute。 我已根据exception的请求在我的main函数上设置了此属性: [STAThread] static void Main(string[] args) { … 我可以请一些帮助来了解可能出错的地方。 示例代码: private void mainFormButtonCLick (object sender, EventArgs e) { // System.Threading.Thread.CurrentThread.SetApartmentState(ApartmentState.STA); ? MyForm form = new MyForm(); form.show(); } 设计师: this.myCombo.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest; this.myCombo.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems; this.myCombo.FormattingEnabled = true; this.myCombo.Location = new System.Drawing.Point(20, 12); this.myCombo.Margin = […]

从工作线程调用MainThread上的事件

我在主线程中从辅助线程调用事件时遇到问题。 事件处理程序不在主线程上执行。 任何人都可以给我一些关于我做错的指示。 谢谢 namespace ThreadSyncExample { class Program { static void Main(string[] args) { Console.WriteLine(“MainThread: ” + System.Threading.Thread.CurrentThread.ManagedThreadId); Execute execThe = new Execute(); execThe.FinishedThread += (src, arg) => { //This shoould be executed on MainThread right? Console.WriteLine(“Thread Id: ” + System.Threading.Thread.CurrentThread.ManagedThreadId); }; execThe.Run(); Console.ReadKey(); } } class Execute { public void Run() { Thread […]

multithreading和锁定(线程安全操作)

所以我有一个类,有一些方法都使用锁定,以防止有人使用我的类的实例与多个线程访问它时发生奇怪的事情: public class SomeRandomClass { private object locker = new object(); public void MethodA() { lock (locker) { // Does something MethodB(); } } public void MethodB() { lock (locker) { // Does something else } } } 正如我们所看到的, MethodB()由MethodA()自动访问,但由于MethodA()当前已锁定了locker对象,因此无法使用。 我想公开访问MethodB() ,因此您可以在需要时手动调用它,但我不希望在MethodA()使用它(这就是我使用锁定器对象的原因)。 当然,当MethodB()正在做的事情时,我不希望MethodA()做事情。 我基本上只希望同时使用所有方法中的一个,但MethodA()需要以某种方式访问MethodB()而不删除锁(以便它始终保持完全线程安全)。 我真的希望我能提出的问题是可以理解的……如果对我的问题有任何疑问,请继续将其发布在下面。 答案/解决方案也非常感谢! 解决方案可能非常简单,我只是没有看到它。 顺便说一下,上面应该是C#-code。

繁忙等待线程

基本上,我需要忙着等到网页上出现一些html。 我已经创建了以下代码来忙着等我: public void ExecuteBusyWaitThreads() { foreach (Canidate canidate in allCanidates) { Thread newThread = new Thread(delegate() { BusyWait(canidate); }); newThread.Start(); } } public bool BusyWait(Canidate canidate) { //hit that url, and wait for the claim all button to appear string page = null; while (found == false) { HttpWebRequest request = Canidate.GetHTTPRequest(canidate.URL); //make sure […]

这个示例线程是否安全?

假设我有一个充当数据缓存的单例类。 多个线程将从缓存中读取,并且单个线程将定期刷新它。 它看起来像这样: public sealed class DataStore { public static DataStore Instance { get { return _instance; } } public Dictionary FooBar { get; private set; } static DataStore() { } private DataStore() { } public void Refresh() { FooBar = GetFooBarFromDB(); } private static readonly DataStore _instance = new DataStore(); } 我的问题基本上是,当其他线程可能正在访问FooBar时, Refresh()是否安全? 我需要使用锁,还是我的获取和设置操作是primefaces的? […]

在MultiThreading Service中听什么更好的方法?

我对MSMQ和.NET中的线程都相对较新。 我必须创建一个服务,通过TCP和SNMP,几个网络设备以及所有这些东西在专用线程中运行,以不同的线程监听,但是这里也需要从另一个应用程序监听MSMQ队列。 我正在分析另一个类似的项目,并使用下一个逻辑: private void MSMQRetrievalProc() { try { Message mes; WaitHandle[] handles = new WaitHandle[1] { exitEvent }; while (!exitEvent.WaitOne(0, false)) { try { mes = MyQueue.Receive(new TimeSpan(0, 0, 1)); HandleMessage(mes); } catch (MessageQueueException) { } } } catch (Exception Ex) { //Handle Ex } } MSMQRetrievalThread = new Thread(MSMQRetrievalProc); MSMQRetrievalThread.Start(); 但是在另一个服务(消息调度程序)中,我使用基于MSDN的异步消息读取示例 : public […]

在后台工作线程中使用C#全局变量是否安全

嗨,我正在研究一个简单的桌面应用程序,它需要处理一些操作,如加载可能阻塞主线程的网页,所以我将代码移动到后台工作者。 我的问题是有一个名为UCSProject的重类,它包含许多字符串和List字段,我需要将这个类的实例传递给后台worker,因为类有点重,我想减少重复的次数实例直接使用全局变量,而不是将其作为参数传递给后台工作者。 为了简短起见,我只想知道从C#中的后台工作线程访问全局变量是否安全