amibar / smartthreadpool Goto Github PK
View Code? Open in Web Editor NEWA .NET Thread Pool fully implemented in C# with many features
Home Page: http://www.codeproject.com/Articles/7933/Smart-Thread-Pool
License: Microsoft Public License
A .NET Thread Pool fully implemented in C# with many features
Home Page: http://www.codeproject.com/Articles/7933/Smart-Thread-Pool
License: Microsoft Public License
Hi,
We recently explored STP, and started using it in one of our asp.net core application.
and our implementation is like, during particular object's constructor we configure/instantiate the smart thread pool with work item groups and min, max threads etc.
There are two questions:
Thanks,
Matheswaran S
Awesome work on SmartThreadPool - it solves a lot of problems for me! However, my employer has a list of pre-approved licenses that I'm allowed to use, and MS-PL isn't on the list.
It appears to me that both BSD 2.0 and MIT licenses are basically the equivalent of MS-PL. Do you have any objections to dual-licensing SmartThreadPool under one of those licenses?
Is this project still maintained? When I use SmartThreadPool at Mono, it blocks the main thread.
Hi,
I'm using the STP but wrapped within the following code:
using (new Impersonator( "myUsername", "myDomainname", "myPassword" ))
{
[code that executes under the new context]
}
see code here:
http://www.codeproject.com/Articles/10090/A-small-C-Class-for-impersonating-a-User?msg=4023519#xx4023519xx
But when the threads execute I get an error thrown stating:
Invalid token for impersonation - it cannot be duplicated.
Do you know what is going on here?
I've tried to set the UseCurrentContext but to no avail. Do you know how I can kick off a thread so it carried using the Impersonatoer context it was kick off within?
I'm assuming that I need to pass through the UserToken for the user I'm logged in as I but can't find how to do this anywhere
Do you have any plans to support .net core 3.1?
Hello Ami,
A chance to thank you for your great job!
I'm using STP a lot and this is defiantly my favorite library!
Do you consider for future releases some of the new dotnet libraries and mechanisms?
Thanks!
Hi,
I found an issue in using groups and I would like to check if it is known issue or not.
steps to reproduce:
Here is what happens:
Thread#1 [STP Worker thread] timeouts, because it has empty queue for 60 seconds and starts terminating (there's still some work to do after the timeout check and before the thread is freed)
In meantime
Thread#2 [new thread] enqueues a task to the WorkItemGroup,
Thread#2 WorkItemGroup enqueues a task to SmartThreadPool expecting that STP withdraw the task,
Thread#2 STP enqueues a task and doesn't start a thread, because there's one already running (it doesn't know that it's terminating)
Thread#1 [STP Worker thread] finishes termination and removes itself from STP
Thread#3 [new thread] [Receiver] enqueues a task to Work Items Group
Thread#3 WorkItemGroup doesn't do anything, because it thinks that STP has already enough tasks in queue
since then the STP has no running thread and all new tasks are enqueued to the WorkItemGroup and the queue is growing and never processed
Thread was interrupted from a waiting state
problem:
问题签名:
问题事件名称: BEX64
应用程序名: Run.exe
应用程序版本: 2.0.0.0
应用程序时间戳: 5d6dfe32
故障模块名称: StackHash_b4ee
故障模块版本: 0.0.0.0
故障模块时间戳: 00000000
异常偏移: 000007fe00000000
异常代码: c0000005
异常数据: 0000000000000008
OS 版本: 6.1.7601.2.1.0.274.10
区域设置 ID: 2052
其他信息 1: b4ee
其他信息 2: b4ee5de6a2322745523997a782b35692
其他信息 3: 277e
其他信息 4: 277e19c30fbd5f6bb531ec9e027c37c3
program info:
// 全局类型:
// 入口点: run.Main
// 架构: x64
// 运行库: .NET Framework 4.5.1
// 时间戳: 5DAD0D73 (2019/10/21 1:44:19)
SmartThreadPool.
// SmartThreadPool.dll
// 全局类型:
// 架构: AnyCPU (64位优先)
// 运行库: .NET Framework 4.5
// 时间戳: <未知> (83155524)
what can i do to sovle this problem
thankyou
Hi @amibar
Would it be possible to add support for dotnet 4.7.x
(4.7.2
in particular) and dotnet 4.8
?
dotnet 4.7.2
is the default .net version that comes with Windows 10, 11, and server 2019 and dotnet 4.8
is the LTS
version of the dotnet framework. It will help a lot for maintaining old code and upgrading it to dotnet versions that are still supported (moving the code to dotnet core
and dotnet 5.0+
will take more time).
Thanks
Hello,
I'm using NetCore version: 3.1.302, but when i running, have error:
System.PlatformNotSupportedException: Thread abort is not supported on this platform. at System.Threading.Thread.Abort() at Amib.Threading.SmartThreadPool.Shutdown(Boolean forceAbort, Int32 millisecondsTimeout) at Amib.Threading.SmartThreadPool.Shutdown()
I saw admin talk it support in versionn 3.1, but why when i use, it's can't work?
Any can help me?
general info:
on Linux, compiling works :) some small warnings only.
See "SmartThreadPool_monodevelop.log" here: https://copy.com/xNFfGpHruCnU
WAKAKA
Hello,
I'm using this library for long-times but i recently had a problem :
Here the call stack (french sorry) :
System.InvalidOperationException: La collection a été modifiée; l'opération d'énumération peut ne pas s'exécuter.
à System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
à System.Collections.Generic.Dictionary`2.ValueCollection.Enumerator.MoveNext()
à Amib.Threading.SmartThreadPool.Cancel(Boolean abortExecution)
When watching the code of the method i think, it must create a copy of the values before looping on it. Replace :
foreach (ThreadEntry threadEntry in _workerThreads.Values)
by
var threadEntrys = _workerThreads.Values.ToList()
foreach (ThreadEntry threadEntry in threadEntrys )
Would you mind releasing the latest version (with InUseThreads for IWorkItemsGroup) as a new NuGet Version? Would be useful for me without having to fork it / Create another NuGet package
Hi Ami,
I'm having an issue and need your advice.
Code is very simple :
var pool = new SmartThreadPool(true);
items.ForEach(item => pool.QueueWorkItem(...));
try
{
pool.Start();
pool.WaitForIdle();
}
catch (ThreadAbortException)
{
Thread.ResetAbort();
pool.Cancel(true);
pool.WaitForIdle();
}
In each working items, the Thread.Abort
is handled to perform cleaning stuff.
What I want is, in case of cancellation, to wait for all threads in the pool to have this "cleaning stuff" done.
But the pool.WaitForIdle()
in the catch
doesn't seams to block, as there is some thread still alive doing their cleansing.
Is there a better/correct way to do this with your library ?
Thanks Ami !
I'm looking at the updates to SmartThreadPool.NetCore. Thread.Abort() is not supported in .NET Core, but I still see it in the .NET Core code?
Hi, I have 20-30 workgroups. Each workgroup will have arroud 3 to 4k workitems with different priorities. Sometime i face a problem. Few workgroup items are not being picked up even though those workitems are having high priority. Lower priority items are being picked from other workgroup. I tried to simulate the issue, but I couldn't. This issue doesn't happen always.
Have you come across similar issue?
I've ugraded from using an older version of STP to the newest one available on NuGet.
Now I get an error:
System.NullReferenceException
With the following stack trace (excerpt):
at Amib.Threading.SmartThreadPool.get_IsWorkItemCanceled() in e:\Dev\STP\STP.git\SmartThreadPool\SmartThreadPool.cs:Zeile 1420.
at ZetaTest.Runtime.Code.ThreadPooling.ThreadPoolManager.CheckWantCancel() in C:\P\Zeta Test\3.5\Zeta Test Main\Source\Runtime\Code\ThreadPooling\ThreadPoolManager.cs:Zeile 260.
at ...
It seems that the line number 140 is different from the code in this GitHub repository, since the function IsWorkItemCanceled
seems to be located here.
My question:
Am I doing anything wrong to get the null reference exception?
Do you any plans to support your library running on .NET Core in the near future?
I'm using the STP 2.2.1 in a server application.
Today I had a problem that a work item has been processed by two threads simultaneously.
In this a known issues?
When looking into the sourcecode I've notices the method
PriorityQueue.Dequeue ()
In this method there is a sequence
workItem = _queues[queueIndex].First.Value;
_queues[queueIndex].RemoveFirst();
For me it seems that these two actions are not atomic, and it might be that two threads got the same WorkItem.
Is that possible?
This repository has not been updated for a long time. Is there any further update in the next step, such as .net7 .net8...
Our MaxThreads settings is set to 20. However I see that the Thread Name [STP SmartThreadPool Thread#] increase over time and is currently at STP SmartThreadPool Thread #62 We've noticed this only in our test environments.
Can you please explain under what circumstances can the ThreadCounter number be greater than the MaxThreads settings?
when I try to use IWorkItemResult.Exception
, I am get a System.InvalidCastException
exception detail:
InvalidCastException: Cannot cast from source type to destination type.
Amib.Threading.Internal.WorkItemResultTWrapper`1[System.String].get_Exception ()
and i looking the source code:
public object Exception
{
get { return (TResult)_workItemResult.Exception; }
}
why the exception will be cast to TResult? is it a bug or feature
In .NET 2.0 sp0 we didn't have WaitOne/WaitAll/WaitAny that took waithandle(s) and an Int32. It was added in .NET 2.0 sp2. The overloads with exitContext = false provides identical functionality, but it's compatible with .NET 2.0 (yes I have machines that have 0 patches to them, nor can I affect them).
SmartThreadPool v2.2.3
This finding is very rare, I've encountered this problem only a couple of times so far. Therefore, It's very hard to reproduce.
On some occasions the WaitForIdle doesn't wait with callbacks on the queue. It seems like the ManualResetEvent was Set() or the underlying state changed somehow when it shouldn't have been.
To simulate this problem uncomment the call to StpChangeState().
The behavior with StpChangeState() uncommented is consistent with what I'm occasionally encountering.
Any idea what may be causing this? Has this problem been reported before?
using (var stp = new SmartThreadPool(new STPStartInfo
{ MinWorkerThreads = 0, MaxWorkerThreads = 2, StartSuspended = false }))
{
do
{
stp.QueueWorkItem(JobOne, stp);
stp.QueueWorkItem(JobTwo, stp);
Thread.Sleep(20);
// Uncomment this line to simulate the problem
// StpChangeState(stp);
stp.WaitForIdle();
} while (stp.InUseThreads > 0);
}
Console.WriteLine("Enter any key to close");
Console.ReadKey();
}
private static void JobOne(SmartThreadPool obj)
{
Thread.Sleep(TimeSpan.FromSeconds(10));
Console.WriteLine(MethodBase.GetCurrentMethod());
}
private static void JobTwo(SmartThreadPool obj)
{
Thread.Sleep(TimeSpan.FromSeconds(10));
Console.WriteLine(MethodBase.GetCurrentMethod());
}
private static void StpChangeState(SmartThreadPool stp)
{
FieldInfo field = stp.GetType().GetField("_isIdleWaitHandle", BindingFlags.NonPublic | BindingFlags.Instance);
var value = (ManualResetEvent)field?.GetValue(stp);
value?.Set();
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.