Tag: multithreading

等待系统删除文件

删除文件后刷新文件列表时遇到问题。 当我命令删除文件时,抛出了exception,因为刷新方法试图访问应该删除的文件。 经过一番思考和调试后,我得出结论,问题在于系统需要一些时间来删除文件。 我这样解决它: //Deleting file System.Threading.Thread.Sleep(2000); //Refreshing list 它工作得很好。 我的问题是 是否有更优雅的方式等待系统删除文件然后继续代码…?

具有静态类的竞争条件?

假设我有一个带静态方法的静态类。 多个线程可以同时调用此静态方法。 在这些情况下是否存在竞争条件: a – if the method depends only on local variables b – if the method depends on local variables and member fields

为什么我的Winforms应用程序中的SynchronizationContext.Current为null?

我刚写了这段代码: System.Threading.SynchronizationContext.Current.Post( state => DoUpdateInUIThread((Abc)state), abc); 但System.Threading.SynchronizationContext.Current为null

如何设置定时器在c#中的特定时间执行

我有一个要求,我需要在每天00:01:00 AM执行计时器…但我没有得到如何实现这一点..如果我正在采取系统时间,它可以是不同的格式..这里是我的计时器代码.. static System.Timers.Timer timer; timer = new System.Timers.Timer(); timer.Interval = 1000 * 60 * 60 * 24;//set interval of one day timer.Elapsed += new ElapsedEventHandler(timer_Elapsed); start_timer(); static void timer_Elapsed(object sender, ElapsedEventArgs e) { // Add timer code here } private static void start_timer() { timer.Start(); }

System.Threading.Timer vs System.Threading.Thread.Sleep resolution – .NET Timer不使用系统时钟分辨率

问题:尽管OS时钟分辨率更精确,为什么System.Threading.Timer保持15ms的分辨率? 在没有繁忙的CPU等待的情况下,实现1ms定时事件解析的可行方法是什么? 再次强调: 在我的情况下,系统计时器的分辨率为1ms (与建议重复的问题相反)。 所以这不是系统计时器分辨率的问题。 因此,在所谓的重复问题中没有有用的信息。 背景:似乎.NET System.Threading.Timer 没有使用系统时钟分辨率 – 它保持了~15ms的分辨率。 尽管OS时钟(例如Sleep分辨率)更加精确。 在我的盒子上(当几乎空闲并且有4个核心可用时): >Clockres.exe ClockRes v2.0 – View the system clock resolution Copyright (C) 2009 Mark Russinovich SysInternals – www.sysinternals.com Maximum timer interval: 15.625 ms Minimum timer interval: 0.500 ms Current timer interval: 1.001 ms 输出我的快速测试: Sleep test: Average time delta: 2[ms] (from 993 […]

在C#内部使用TPL内部服务的基本设计模式

我正在尝试构建Windows服务,它需要某种并行性来汇集来自不同ftp源的文件。 为了启动多个ftp下载,我正在寻找TPL库来轻松地进行foreach循环并使并行性变得非常容易。 但是当我搜索如何启动或停止我的服务时,我最好的资金是在OnStart()方法中创建新线程,如下所述https://stackoverflow.com/a/4865893/69433 阅读有关TPL的信息,请注意TPL比手动线程和手动停止线程更先进。 我没有找到任何描述如何在WindowsService中进行TPL循环的示例post? 我的代码: protected override void OnStart(string[] args) { _thread = new Thread(WorkerThreadFunc); _thread.Name = “My Worker Thread”; _thread.IsBackground = true; _thread.Start(); } 而在WorkerThreadFunc里面做了那种TPL private void WorkerThreadFunc() { foreach (string path in paths) { string pathCopy = path; var task = Task.Factory.StartNew(() => { Boolean taskResult = ProcessPicture(pathCopy); return taskResult; }); task.ContinueWith(t […]

C#从另一个线程调用form.show()

如果我从另一个线程调用WinForms对象上的form.show() ,表单将抛出exception。 我可以通过何种方式向主应用程序线程添加新的可见表单? 否则,如何在不停止当前正在执行的线程的情况下打开表单? 这是我的示例代码。 我试图启动一个线程,然后在该线程中执行一些工作。 随着工作的进展,我将展示表格。 public void Main() { new Thread(new ThreadStart(showForm)).Start(); // Rest of main thread goes here… } public void showForm() { // Do some work here. myForm form = new myForm(); form.Text = “my text”; form.Show(); // Do some more work here }

从给定的线程获取SynchronizationContext

我似乎没有找到如何获取给定Thread的SynchronizationContext : Thread uiThread = UIConfiguration.UIThread; SynchronizationContext context = uiThread.Huh?; 我为什么需要那个? 因为我需要从前端应用程序的不同位置发布到UIThread。 所以我在名为UIConfiguration的类中定义了一个静态属性。 我在Program.Main方法中设置了这个属性: UIConfiguration.UIThread = Thread.CurrentThread; 在那一刻我可以肯定我有正确的线程,但是我不能设置像这样的静态属性 UIConfiguration.SynchronizationContext = SynchronizationContext.Current 因为尚未安装该类的WinForms实现。 由于每个线程都有自己的SynchronizationContext,因此必须可以从给定的Thread对象中检索它,否则我完全错了?

启动画面显示方法最佳实践C#

我通过在运行主表单之前立即启动新线程来显示启动表单。 在这个线程运行的方法中,我正在使用Application.Run,​​如下面的选项1所示。 这是一个正确的方法,或者是否有问题等我,因为我已经两次调用Application.Run? 另一种选择是选项2,也在下面显示,我调用.ShowDialog()来显示表单。 启动表单本身在指定时间后关闭,在表单本身内控制,并且两个选项似乎都运行良好。 所以我的问题是:哪个更受欢迎 – 选项1还是选项2? 如果你能给出一个或那个很好的具体原因。 谢谢。 主要片段: // Run splash screen thread. Thread splash = new Thread(new ThreadStart(ShowSplash)); splash.Start(); // Run main application. Application.Run(new MainForm()); 显示启动表单选项1: static void ShowSplash() { Application.Run(new SplashForm()); } 显示启动表单选项2: static void ShowSplash() { using (SplashForm splash = new SplashForm()) { splash.ShowDialog(); } }

List 线程安全上的Parallel.ForEach

就线程安全问题而言,可以做或者我需要使用不同的集合吗? List fileInfo = getList(); Parallel.ForEach(fileInfo, fileMember => { //Modify each fileMember }