1. 程序中开了6,7个线程,运行的时候CPU占用率达到60%-70%,太高了,影响性能,用线程池可否优化。线程池我还没用过,大概知道这个名词。
2. 在退出程序的时候在Form_closing()方法中,让线程都终止,用的是让标志变量为fasle,当然没用Abort方法,有没有必要等到线程的IsAlive状态为false的时候再退出,程序中我是加了循环等待到IsAlive的状态为false再退出程序,但是这样会造成退出很慢,有时候还会出现死循环,线程的IsAlive迟迟不为false。起初这样做的原因是怕不等到线程彻底结束,造成不安全因素。
还有我还试过,用标志变量让线程终止,等待线程的IsAlive状态为false后,线程就无法再开启,报ThreadStateException,IsAlive状态值始终为false。 if (threadJoyStickMaxon != null)
{
while (threadJoyStickMaxon.IsAlive)
{
Thread.Sleep(1);
}
threadJoyStickMaxon = null;
}
threadJoyStickMaxon = new Thread(new ThreadStart(ManageThreadJoystickMaxon));
threadJoyStickMaxon.Start();
2. 在退出程序的时候在Form_closing()方法中,让线程都终止,用的是让标志变量为fasle,当然没用Abort方法,有没有必要等到线程的IsAlive状态为false的时候再退出,程序中我是加了循环等待到IsAlive的状态为false再退出程序,但是这样会造成退出很慢,有时候还会出现死循环,线程的IsAlive迟迟不为false。起初这样做的原因是怕不等到线程彻底结束,造成不安全因素。
还有我还试过,用标志变量让线程终止,等待线程的IsAlive状态为false后,线程就无法再开启,报ThreadStateException,IsAlive状态值始终为false。 if (threadJoyStickMaxon != null)
{
while (threadJoyStickMaxon.IsAlive)
{
Thread.Sleep(1);
}
threadJoyStickMaxon = null;
}
threadJoyStickMaxon = new Thread(new ThreadStart(ManageThreadJoystickMaxon));
threadJoyStickMaxon.Start();
解决方案 »
- 请帮忙解释一下这个C#程序!
- C#已经达到基本写代码没有任何问题的地步了,再看什么书提高?。。只C#方面
- 更新数据时出错:子表包含相关记录,不能删除或改变该记录
- 急急急 treeview 的问题
- 问下怎么让一个动作执行1秒 比如更改的图标要给用户看到1秒
- 一个要用C#写的特别有意思的图形
- 困扰了我一年的一个关于SCSF的问题,希望各路见多识广的大侠进来解答。先给40,回复正确就加到100分。
- 用c# 写 activeX控件?
- 一个复杂的省份验证的问题,涉及到SQLServer, myProgram and Windows验证
- 不返回任何键列信息的SelectCommand不支持UpdateCommand的动态SQL生成
- 解决方案,怎么改名啊?
- wpf 如何实现menuitem的自定义command?
2. threadJoyStickMaxon.IsBackground = true;
while (threadJoyStickMaxon.IsAlive)
{
Thread.Sleep(1);
}
如何谈优化
MemoryManagement.cs
namespace AliwwJC
{
/// <summary>
/// Author:king
/// This class provide methods to automatic do the Memory Management,
/// free some resources and collect garbage
/// </summary>
public sealed class MemoryManagement : IDisposable
{
#region Properties
/// <summary>
/// Current app ticks
/// </summary>
private long _ticks; /// <summary>
/// Gets a value indicating if the Memory Management is stopped or running.
/// </summary>
private bool _hasStarted; /// <summary>
/// Gets a value indicating if the Memory Management is stopped or running.
/// </summary>
public bool HasStarted
{
get { return _hasStarted; }
} #endregion #region Constructors /// <summary>
/// Initializes a new instance of the <see cref="T:ApplicationManagement.MemoryManagement"/> class.
/// Note: This class is only for Win32NT Operative Systems, static methods still can be used in both.
/// </summary>
/// <param name="start">If true initializes the class and starts the memory management,
/// otherwise only initializes the class</param>
/// <res>If that class is used in a unsupported Operative System, nothing will happen.</res>
public MemoryManagement(bool start)
{
if (start)
Start();
} /// <summary>
/// Initializes a new instance of the <see cref="T:ApplicationManagement.MemoryManagement"/> class.
/// Note: This class is only for Win32NT Operative Systems, static methods still can be used in both.
/// </summary>
/// <res>If that class is used in a unsupported Operative System, nothing will happen.</res>
public MemoryManagement()
{
} #endregion #region Public Methods /// <summary>
/// Starts the automatic memory management.
/// </summary>
/// <returns>Returns true if function starts successful, otherwise false.</returns>
public bool Start()
{
if (_hasStarted || Environment.OSVersion.Platform != PlatformID.Win32NT)
return false;
_ticks = DateTime.Now.Ticks;
Application.Idle += Application_Idle;
Free();
_hasStarted = true;
return true;
} /// <summary>
/// Stops the automatic memory management.
/// </summary>
/// <returns>Returns true if function stops successful, otherwise false.</returns>
public bool Stop()
{
if (!_hasStarted)
return false;
Application.Idle -= Application_Idle;
_ticks = 0;
_hasStarted = false;
return true;
} #endregion #region Static Methods /// <summary>
/// Free resources.
/// </summary>
private static void Free()
{
try
{
using (Process proc = Process.GetCurrentProcess())
{
Kernel32.SetProcessWorkingSetSize(proc.Handle, -1, -1);
}
}
catch (Exception)
{
} } /// <summary>
/// Gets if this class <see cref="MemoryManagement"/> can be used.
/// </summary>
/// <returns>Returns true if this class can be used under current Operative System, otherwise false.</returns>
public static bool CanUseClass()
{
return Environment.OSVersion.Platform == PlatformID.Win32NT;
} #endregion
/// Collect Garbage and free memory.
/// </summary>
public static void CollectGarbage()
{
GC.Collect(0);
GC.GetTotalMemory(false);
GC.Collect(0);
} #endregion #region Event Methods /// <summary>
/// When application is idle, this event is fired.
/// </summary>
/// <param name="sender">Sender object.</param>
/// <param name="e">Event aruments.</param>
private void Application_Idle(object sender, EventArgs e)
{
try
{
long ticks = DateTime.Now.Ticks;
if ((ticks - _ticks) > 0x989680L)
{
_ticks = ticks;
Free();
}
}
catch
{
}
} #endregion #region Implementation of IDisposable /// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
/// <filterpriority>2</filterpriority>
public void Dispose()
{
Stop();
} #endregion
}
}