Tag: multithreading

.NET中的multithreading和闭包

如果我有这个: public string DoSomething(string arg) { string someVar = arg; DoStuffThatMightTakeAWhile(); return SomeControl.Invoke(new Func(() => someVar)); } 并且这个方法可以从多个线程同时调用,并且一个线程停留在DoStuffThatMightTakeAWhile ,然后第二个线程调用具有不同arg DoSomething ,这将改变所有线程的someVar的值,因此, DoSomething返回第二个版本的两个调用中的someArg ,或者每个线程someVar存在一个someVar吗? 编辑我认为我的Action应该是一个Func所以编辑它。

如何在C#中启动UI线程

我知道我可以用.NET开始一个新的工作线程。 但是如何启动新的UI线程(如在MFC中)? 我不介意解决方案是否仅限于Windows机箱; 我也希望解决方案纯粹是.NET – 没有p /调用CreateThread等。 任何输入赞赏。

使用WPF中的Backgroundworker为主应用程序弹出MessageBox

在WPF应用程序中,我使用BackgroundWorker定期检查服务器上的条件。 虽然工作正常,但我希望弹出一个MessageBox,如果在检查过程中出现故障,请通知用户。 这就是我所拥有的: public static void StartWorker() { worker = new BackgroundWorker(); worker.DoWork += DoSomeWork; worker.RunWorkerAsync(); } private static void DoSomeWork(object sender, DoWorkEventArgs e) { while (!worker.CancellationPending) { Thread.Sleep(5000); var isOkay = CheckCondition(); if(!isOkay) MessageBox.Show(“I should block the main window”); } } 但是这个MessageBox不会阻止主窗口。 我仍然可以点击我的WPF应用程序并使用MessageBox更改我喜欢的任何内容。 我该如何解决这个问题? 谢谢, 编辑: 作为参考,这是我最终做的事情: public static void StartWorker() { worker = […]

从另一个线程和类更新C#GUI中的进度条

可能重复: 从另一个线程更新进度条 在我的程序中,我想将非GUI函数分离到另一个类,并在主类中保留与GUI相关的内容。 但是,当工作者类中的一个工作方法正在执行其工作时,我遇到了更新进度条的问题。 我知道我将不得不在这里使用multithreading,但我不明白如何。 我可能只是缺少简单的东西,但是当我查找有关它的信息时,似乎大多数教程都会讨论细节,但不能很好地解释大局。 我部分理解调用和委托命令是什么,但我真的不明白它们是如何交互的。 下面是我要做的事情的精简版。 如何修改此更新进度条,但保持窗口响应和重新绘制? 主要表格类: public partial class Form1 : Form { time_waster thing = new time_waster(); public Form1() { InitializeComponent(); progressBar1.Minimum = 0; progressBar1.Maximum = 100; } private void button1_Click(object sender, EventArgs e) { thing.something_that_takes_a_while(); } } 单独的工人类:class time_waster {public time_waster(){} public void something_that_takes_a_while() { int delay = 200; […]

避免从multithreadingc#MVVM应用程序中的ViewModel对象调用BeginInvoke()

我的C#应用​​程序有一个数据提供程序组件,它在自己的线程中异步更新。 ViewModel类都inheritance自实现INotifyPropertyChanged的基类。 为了让异步数据提供程序使用PropertyChanged事件更新View中的属性,我发现我的ViewModel与视图紧密耦合,因为只需要从GUI线程中引发事件! #region INotifyPropertyChanged /// /// Raised when a property on this object has a new value. /// public event PropertyChangedEventHandler PropertyChanged; /// /// Raises this object’s PropertyChanged event. /// /// The property that has a new value. protected void OnPropertyChanged(String propertyName) { PropertyChangedEventHandler RaisePropertyChangedEvent = PropertyChanged; if (RaisePropertyChangedEvent!= null) { var propertyChangedEventArgs = […]

任务LongRunning副作用?

如果使用LongRunning选项创建任务,则会产生任何副作用,因为它们不使用ThreadPool

如何等待BackgroundWorker完成然后退出控制台应用程序

我已经编写了一个示例控制台应用程序来使用Stackoverflow中发布的示例之一来测试backgroundworker。 我有一个backgroundworker,它以main方法开头,但是如果我按下回车键,它会在操作过程中结束,因为我在main方法中写了一个console.readkey。 但我希望它等到后台工作者完成工作然后退出应用程序。 这是我的代码。 class Program { private static BackgroundWorker worker = new BackgroundWorker(); private event EventHandler BackgroundWorkFinished; static void Main(string[] args) { worker.DoWork += worker_DoWork; worker.RunWorkerCompleted += worker_RunWorkerCompleted; worker.ProgressChanged += worker_ProgressChanged; worker.WorkerReportsProgress = true; worker.WorkerSupportsCancellation = true; Console.WriteLine(“Starting Application…”); worker.RunWorkerAsync(); Console.ReadKey(); } static void worker_ProgressChanged(object sender, ProgressChangedEventArgs e) { Console.WriteLine(e.ProgressPercentage.ToString()); } static void worker_DoWork(object […]

了解WPF Dispatcher.BeginInvoke

我的印象是dispatcher将遵循排队的操作的优先级,并根据优先级或操作添加到队列的顺序(如果相同的优先级)执行操作,直到我被告知这不是WPF UI dispatcher 。 有人告诉我,如果UI线程上的操作持续时间较长,则表示数据库读取UI调度程序,则尝试执行队列中的下一组操作。 我无法接受它,所以决定编写一个示例WPF应用程序,其中包含一个按钮和三个矩形,点击按钮,矩形填充不同的颜色。 并在代码隐藏 private void OnFillColorsClick(object sender, RoutedEventArgs e) { var dispatcher = Application.Current.MainWindow.Dispatcher; dispatcher.BeginInvoke(new Action(() => { //dispatcher.BeginInvoke(new Action(SetBrushOneColor), (DispatcherPriority)4); //dispatcher.BeginInvoke(new Action(SetBrushTwoColor), (DispatcherPriority)5); //dispatcher.BeginInvoke(new Action(SetBrushThreeColor), (DispatcherPriority)6); dispatcher.BeginInvoke(new Action(SetBrushOneColor)); dispatcher.BeginInvoke(new Action(SetBrushTwoColor)); dispatcher.BeginInvoke(new Action(SetBrushThreeColor)); }), (DispatcherPriority)10); } private void SetBrushOneColor() { Thread.Sleep(10 * 1000); Order = “One”; //MessageBox.Show(“One”); BrushOne = Brushes.Red; } […]

如何同步对ASP.NET中使用的List 的访问?

我在具有并发访问列表的站点上遇到了一些问题。 此列表保留一个项目的购物车,并且多次删除都会导致网站崩溃。 哪个是同步它们的最佳方法? 锁够了吗? 锁定选项似乎很难看,因为代码遍布整个地方并且非常混乱。 更新:这是一个如下所示的列表:public class MyList:List {} 这是一个遗留站点,因此不允许进行太多修改。 我应该如何重构这个以便在迭代时安全锁定? 任何的想法!

如何调试死锁?

除此之外我不知道我现在是否可以重现它(我已经使用这个特定的应用程序一两个星期没有问题),假设我在VS调试器中运行我的应用程序,如何它应该在它发生后调试死锁吗? 如果我暂停程序并因此看到不同的线程发生在哪里,我认为我可能能够进入调用堆栈,但是单击暂停只会使Visual Studio陷入死锁,直到我杀死我的应用程序。 除了浏览我的源代码树以寻找潜在问题之外,还有其他方法吗? 一旦出现问题,是否有办法获得调用堆栈以查看问题所在? 任何其他可能有用的工具/提示/技巧?