我想显示我的多线程应用程序的CPU使用率(在多核处理器上工作).我想收到任务经理附近的数字.但我得到的数字超过100%.甚至超过500%.是的,我知道,对于类别"进程"的计数器"%Processor Time",我需要分为Environment.ProcessorCount或"NumberOfLogicalProcessors"(我的配置相同).此操作后结果为500%.我在具有不同硬件(i7,i5,Core2)和软件配置(带有所有更新的Windows 7 SP1,带有所有更新的Windows 2008 R2 SP1)的不同计算机上测试了此示例,并遇到了同样的问题.
public static class SystemInfo
{
private static Process _thisProc;
private static bool HasData = false;
private static PerformanceCounter _processTimeCounter;
private static void Init()
{
if (HasData)
return;
if (CheckForPerformanceCounterCategoryExist("Process"))
{
_processTimeCounter = new PerformanceCounter();
_processTimeCounter.CategoryName = "Process";
_processTimeCounter.CounterName = "% Processor Time";
_processTimeCounter.InstanceName = FindInstanceName("Process");
_processTimeCounter.NextValue();
}
MaximumCpuUsageForCurrentProcess = 0;
HasData = true;
}
private static bool CheckForPerformanceCounterCategoryExist(string categoryName)
{
return PerformanceCounterCategory.Exists(categoryName);
} …Run Code Online (Sandbox Code Playgroud) 我正在为.NET多线程应用程序寻找无头浏览器.它必须具有下一个功能:
我找到了这个解决方案
但不知道什么是最好的.如果您建议我最好的解决方案并给出一些使用它的.NET示例,将会很高兴.
我可以Autofac使用这样的行逐个注册所有模块(从Autofac.Module派生的类)
builder.RegisterModule(new LoggingInjectionModule());
但是,如果我有10个或更多模块,我只想指定程序集,哪里Autofac可以找到所需的模块,需要注册.有没有办法通过单行或两行注册特定程序集,命名空间中的所有模块?
有一个多线程应用程序,可以处理较大的DB文件(> 600 Mb)。当我添加Blob数据时开始出现“数据库已锁定”问题,并且每个请求开始使用大于30 Kb的BLOB数据进行操作。我认为问题与小硬盘速度有关。看起来SQLite删除了-journal文件,我的应用程序的一个线程失锁了(因为-journal文件已被应用并删除了),而我的其他线程想对数据库进行处理,但是SQLite仍在更新DB文件...当然,我可以在每次DB调用后延迟一分钟,但这不是解决方案,因为我需要更快的速度。
现在,我使用每个会话(每个线程)实现的会话。因此,每个应用程序对象有一个ISessionFactory,而许多ISession对象是。
有我的帮助程序类(如您所见,我使用IsolationLevel.Serializable和CurrentSessionContext = ThreadStaticSessionContext):
public abstract class nHibernateHelper
{
private static FluentConfiguration _configuration;
private static IPersistenceContext _persistenceContext;
static nHibernateHelper() {}
private static FluentConfiguration ConfigurePersistenceLayer()
{
return Fluently.Configure().Database(FluentNHibernate.Cfg.Db.SQLiteConfiguration.Standard.ShowSql().UsingFile(_fileName).IsolationLevel(IsolationLevel.Serializable).MaxFetchDepth(2)).
Mappings(m => m.FluentMappings.AddFromAssemblyOf<Foo>()).CurrentSessionContext(typeof(ThreadStaticSessionContext).FullName);
}
public static ISession CurrentSession
{
get { return _persistenceContext.CurrentSession; }
}
public static IDisposable OpenConnection()
{
return new DbSession(_persistenceContext);
}
}
public class PersistenceContext : IPersistenceContext, IDisposable
{
private readonly FluentConfiguration _configuration;
private readonly ISessionFactory _sessionFactory;
public PersistenceContext(FluentConfiguration configuration)
{
_configuration = configuration;
_sessionFactory = …Run Code Online (Sandbox Code Playgroud) 让我们编写简单的控制台应用程序(调试模式):
static void Main(string[] args)
{
Process p = Process.GetCurrentProcess();
IList<Thread> threads = new List<Thread>();
Console.WriteLine(p.Threads.Count);
for(int i=0;i<30;i++)
{
Thread t = new Thread(Test);
Console.WriteLine("Before start: {0}", p.Threads.Count);
t.Start();
Console.WriteLine("After start: {0}", p.Threads.Count);
}
Console.WriteLine(Process.GetCurrentProcess().Threads.Count);
Console.ReadKey();
}
static void Test()
{
for(int i=0;i<100;i++)Thread.Sleep(1);
}
Run Code Online (Sandbox Code Playgroud)
你认为你会在结果中看到什么?
[Q1] 为什么 p.Threads.Count 与 Process.GetCurrentProcess().Threads.Count 不同?
.net ×3
c# ×2
autofac ×1
automation ×1
browser ×1
cpu-usage ×1
headless ×1
locking ×1
nhibernate ×1
process ×1
sqlite ×1
web-scraping ×1