Tag: multithreading

C#Windows服务:该服务没有响应开始

这是我第一次尝试创建一个调用多个线程的Windows服务。 我做的是创建一个控制台应用程序,然后添加一个Windows服务和安装程序。 我的代码如下。 partial class Service1 : ServiceBase { public Service1() { InitializeComponent(); } protected override void OnStart(string[] args) { System.Timers.Timer timer1 = new System.Timers.Timer(); timer1.Interval = 10000; timer1.Start(); timer1.Elapsed += new ElapsedEventHandler(timer1_Elapsed); } public static void timer1_Elapsed(object sender, EventArgs e) { Program.ThreadController(); } protected override void OnStop() { // TODO: Add code here to […]

在另一个线程的eventhandler上调用主线程方法

我必须根据另一个对象引发的事件更新另一个GUI。 public void PLCMessage_Received(object sender, PLCMessageEventArgs e) { string tempstr = (String)e.Message; UpdateGUI(tempstr); } public void UpdateGUI(string temp) { if (temp == “A1Y101”) { lbll2heartbeatack.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate() { lbll2heartbeatack.Content = “Ack-OK”; lbll2heartbeatack.Foreground = Brushes.Green; })); } else if (temp == “A1Y102”) { lbll2chargeingscheduleack.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate() { lbll2chargeingscheduleack.Content = “Ack-OK”; lbll2chargeingscheduleack.Foreground = Brushes.Green; })); } else […]

ASP.NET中的全局变量

我希望阻止一个线程输入代码,而另一个线程仍在执行该位代码: 我目前正在进行以下操作: global.asax.cs private static bool _isProcessingNewIllustrationRequest; public static bool IsProcessingNewIllustrationRequest { get { return _isProcessingNewIllustrationRequest; } set { _isProcessingNewIllustrationRequest = value; } } 然后在MVC中: public ActionResult CreateNewApplication() { if (!Global.IsProcessingNewIllustrationRequest) { Global.IsProcessingNewIllustrationRequest = true; // DO WORK… RUN CODE Global.IsProcessingNewIllustrationRequest = false; return View(“Index”, model); } else { // DISPLAY A MESSAGE THAT ANOTHER […]

如何在超时后取消线程

我有一个multithreading应用程序,我用它来控制no.of进程(2)。 我想只在指定的持续时间内处理文件。 以下是我的方法。 我得到了CancellationTokenSource已被处理。 错误。 如果我不是在发布cts.Dispose(); 然后在10秒后该过程不会弯腰。 它一直处理到1000.任何人都可以帮助我。 注意 :我有1000个文件。 要求是在给定时间(10秒)内通过控制进程(2)和睡眠之间(某些x ms)的进程文件。 以下是我的代码 class Program { static void Main(string[] args) { try { LimitedConcurrencyLevelTaskScheduler lcts = new LimitedConcurrencyLevelTaskScheduler(2); List tasks = new List(); TaskFactory factory = new TaskFactory(lcts); CancellationTokenSource cts = new CancellationTokenSource(); for (int i = 0; i { if (cts != null) Console.WriteLine(“{0} […]

线程和套接字

我有以下内容: ThreadStart startThread = delegate { mySocket.StartListen(); }; mySocket现在循环在Listen()上,当我: new Thread(startThread).Start(); 这是StartListen: public void StartListen() { Object locker = new Object(); // Lock resources lock (locker) { S = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp); S.Blocking = false; try { S.Bind(serverEP); S.Listen(1000); isListening = true; /* * While statement not required here because AcceptConnection() * method instructs […]

如何摆脱烧制锅炉板代码的事件?

我正在开发一个事件驱动的应用程序并运行multithreading,所以我有很多事件被解雇并以“保存方式”触发它们我按照以下方式执行此操作: public static void OnEVENT(EventArgs args) { var theEvent = EVENT; if (theEvent != null) theEvent(this, args); } 这个模式在我的应用程序中重复多次。 有没有办法摆脱这种模式的重复并以通用的方式实现这一点?

我想要进行字符串搜索的4000个文件

在多个文件中搜索字符串的最佳方法是什么? 目前我正在对每个文件进行foreach循环,但是已经注意到需要4-5分钟来浏览所有4000多个文件 有没有某种平行的方法来做到这一点?

在后台线程WPF中创建UI?

我试过以下: var task = new Task(() => { for (int i=0; i { label.Content = printerStatus(); // will return “Out of Paper”, “printing”, “Paper jam”, etc. }, TaskScheduler.FromCurrentSynchronizationContext()); label.Content = “Sending to printer”; 它返回以下错误:调用线程必须是STA,因为许多UI组件需要这个.. 尝试创建新的UI对象Grid时发生错误。 我怎样才能解决这个问题? 如果还有其他任何方式让我知道!

串行任务执行器; 这个线程安全吗?

我使用ThreadPool作为执行手段,创建了一个允许异步顺序执行任务的类。 我的想法是,我将在后台运行串行任务的多个实例,但我不希望每个实例都有一个单独的专用线程。 我想检查的是这个类是否真的是线程安全的。 这是相当简短的,所以我想我会由专家在这里运行它,以防我遗漏了一些明显的东西。 我省略了一些针对不同Action类型的方便重载。 /// /// This class wraps ThreadPool.QueueUserWorkItem, but providing guaranteed ordering of queued tasks for this instance. /// Only one task in the queue will execute at a time, with the order of execution matching the order of addition. /// This is designed as a lighter-weight alternative to using a dedicated […]

如何在单独的线程中加载多个3D几何图形而不会遇到线程所有权问题?

我有几个MeshGeometry3D元素存储在单独的文件中。 例如, somemodel.xml可能包含 。 如果我在主UI线程中加载它们,它们会在加载时锁定UI。 所以我尝试在一个单独的线程中加载它们: ThreadStart threadStart = delegate { var geometry = ConvertXmlFileToMeshGeometry3D(filename); viewport2DVisual3D.Dispatcher.BeginInvoke( DispatcherPriority.Normal, new Action(delegate { viewport2DVisual3D.Geometry = geometry; })); }; threadStart.BeginInvoke(delegate(IAsyncResult aysncResult) { threadStart.EndInvoke(aysncResult); }, null); 但是,这在行viewportVisual.Geometry = geometry;上给出了一个例外viewportVisual.Geometry = geometry; : The calling thread cannot access this object because a different thread owns it. 换句话说, MeshGeometry3D是在不同的线程上创建的,所以我不能把它变成Viewport2DVisual3D的Geometry。 我无法想出一种方法来异步加载MeshGeometry3D而不会被错误的线程拥有。 这只是不可能的事情,还是有办法做到的? 编辑:分析表明加载MeshGeometry3D大约13%的时间用于从文件中加载xml元素( […]