首页 > 学院 > 开发设计 > 正文

辅助功能性代码,研究和记录代码。

2019-11-17 02:25:49
字体:
来源:转载
供稿:网友

辅助功能性代码,研究和记录代码。

C#下面能产生每秒65535个我无重复ID,但是毫无规则可言。

 1 PRivate static int id = 0; 2  3         private static int serverID = 1; 4  5         /// <summary> 6         /// 下面这段代码在一秒内,只能生产 65535 个操过了生产是会重复ID 的 7         /// </summary> 8         /// <returns></returns> 9         public static long getId()10         {11             lock (typeof(string))12             {13                 id += 1;14                 return (serverID & 0xFFFF) << 48 | (DateTime.Now.CurrentTimeMillis() / 1000L & 0xFFFFFFFF) << 16 | id & 0xFFFF;15             }16         }

======================

C#程序单例模式。某些非web程序,需要验证单例模式,只能开启一个客户端。

目前我晓得的方式调用win32API,

static Mutex mutex;    /// <summary>    /// 单例模式    /// </summary>    /// <param name="strMutex">系统互斥名,任意设置标识就行</param>    static public void Singleton(string strMutex = "SzExtensions")    {        bool createdNew;        //系统能够识别有名称的互斥,因此可以使用它禁止应用程序启动两次        //第二个参数可以设置为产品的名称:application.ProductName        //每次启动应用程序,都会验证名称为SingletonWinAppMutex的互斥是否存在        mutex = new Mutex(true, strMutex, out createdNew);        //如果已运行,则在前端显示        //createdNew == false,说明程序已运行        if (!createdNew)        {            Process instance = GetExistProcess();            //如果程序已经启动,设置为前端显示            if (instance != null) { SetForegroud(instance); }            //退出当前程序            System.Environment.Exit(0);        }    }    /// <summary>    /// 查看程序是否已经运行    /// </summary>    /// <returns></returns>    static Process GetExistProcess()    {        Process currentProcess = Process.GetCurrentProcess();        foreach (Process process in Process.GetProcessesByName(currentProcess.ProcessName))        {            if ((process.Id != currentProcess.Id) &&                (Assembly.GetExecutingAssembly().Location == currentProcess.MainModule.FileName))            {                return process;            }        }        return null;    }    /// <summary>    /// 使程序前端显示    /// </summary>    /// <param name="instance"></param>    static void SetForegroud(Process instance)    {        IntPtr mainFormHandle = instance.MainWindowHandle;        if (mainFormHandle != IntPtr.Zero)        {            ShowWindowAsync(mainFormHandle, 1);            SetForegroundWindow(mainFormHandle);        }    }

程序启动的时候创建一个系统互斥量,设置一个互斥量又好名称就可以。防止程序第二次启动。

在开发WPF或者WF程序的时候,为了方便测试需要打印一些临时数据。但是打印是一个非常麻烦的事情,不像控制台程序那样可以直接输出。非常便利。那么我们WPF或者WF程序可不可以以打开控制台输出呢?

网上查询了一下可以调用win32API实现打开控制台。其实也就是打开程序的一个输入和输出流而已。

[DllImport("User32.dll")]    private static extern bool SetForegroundWindow(IntPtr hWnd);    [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]    public static extern IntPtr GetForegroundWindow();    [MethodImpl(MethodImplOptions.ForwardRef), DllImport("user32.dll")]    private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);    [MethodImpl(MethodImplOptions.ForwardRef), DllImport("kernel32.dll")]    private static extern bool FreeConsole();    [MethodImpl(MethodImplOptions.ForwardRef), DllImport("Kernel32.dll")]    private static extern bool AllocConsole();    [MethodImpl(MethodImplOptions.ForwardRef), DllImport("user32.dll")]    private static extern IntPtr GetSystemMenu(IntPtr hWnd, IntPtr bRevert);    [MethodImpl(MethodImplOptions.ForwardRef), DllImport("user32.dll")]    private static extern bool ShowWindowAsync(IntPtr hwind, int cmdShow);    [MethodImpl(MethodImplOptions.ForwardRef), DllImport("user32.dll")]    private static extern IntPtr RemoveMenu(IntPtr hMenu, uint uPosition, uint uFlags);    private static string ConsoleTitle = string.Empty;    private static bool flag_console = false;    private static System.Timers.Timer Console_Timer;    /// <summary>    /// 关闭/隐藏控制台 窗体    /// </summary>    static public void Console_Hide()    {        if (Console_Timer != null)        {            Console_Timer.Stop();        }        flag_console = false;        FreeConsole();    }    /// <summary>    /// 显示控制台,一旦显示就不会再调用第二次    /// </summary>    /// <param name="title">显示的标题</param>    /// <param name="withCloseBox">是否移除关闭按钮,默认值:false 显示</param>    /// <param name="isShowThreadCount">在控制台显示线程数量,默认值:true 显示</param>    static public void Console_Show(string title, [MarshalAs(UnmanagedType.U1)] bool withCloseBox, [MarshalAs(UnmanagedType.U1)] bool isShowThreadCount)    {        //检测flag_console,防止调用两次        //直接用win32api        //顺便移除窗口        if (!flag_console)        {            flag_console = true;            AllocConsole();            Console.Title = title;            if (withCloseBox)            {                RemoveMenu();            }            if (isShowThreadCount) { ShowThreadCount(title); }            else { Console.Title = title; }        }    }    /// <summary>    /// 移除关闭按钮    /// </summary>    static void RemoveMenu()    {        IntPtr mainFormHandle = GetForegroundWindow();        RemoveMenu(GetSystemMenu(mainFormHandle, IntPtr.Zero), 0xf060, 0);    }    /// <summary>    /// 在控制台title显示线程量    /// </summary>    /// <param name="title"></param>    public static void ShowThreadCount(string title)    {        ConsoleTitle = title;        Console_Timer = new System.Timers.Timer(200);        Console_Timer.Elapsed += (obj, e) =>        {            int thrads = Process.GetCurrentProcess().Threads.Count;            int runthreads = 0;            foreach (ProcessThread item in Process.GetCurrentProcess().Threads) if (item.ThreadState == System.Diagnostics.ThreadState.Running) runthreads++;            long mem = System.Environment.WorkingSet / 1024 / 1024;            Console.Title = string.Format("{0} -> 当前内存占用 -> {3} MB -> 当前线程量 -> {1} ->  当前活动线程: -> {2}", ConsoleTitle, thrads, runthreads, mem);        };        Console_Timer.Start();    }

在此加入了,控制台的打开和关闭功能性辅助代码。并且加入了控制台标题查看内存和线程使用情况。

还有一个非常方便的方式打开WPF和WF控制台输入输出流的方式。只能临时使用的办法。那就是右键项目属性,选择更改项目类型,把windows应用程序改为控制台程序。可以实现,WPF和WF程序打开控制输入输出流。

日志辅助========================================

日志辅助线程

 1 /** 2  *  3  * @author 失足程序员 4  * @Blog http://www.VEVb.com/ty408/ 5  * @mail 492794628@QQ.com 6  * @phone 13882122019 7  *  8  */ 9 namespace Sz10 {11     /// <summary>12     /// 线程模型13     /// </summary>    14     internal class ThreadModel15     {16         public bool IsStop = false;17         /// <summary>18         /// ID19         /// </summary>20         public int ID;21 22         static int StaticID = 0;23 24         public ThreadModel(string name)25         {26             lock (typeof(ThreadModel))27             {28                 StaticID++;29             }30             ID = StaticID;31             System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(Run));32             thread.Name = name;33             thread.IsBackground = true;34             thread.Start();35         }36 37         /// <summary>38         /// 任务队列39         /// </summary>40         protected System.Collections.Concurrent.ConcurrentQueue<TaskBase> taskQueue = new System.Collections.Concurrent.ConcurrentQueue<TaskBase>();41 42         /// <summary>43         /// 加入任务44         /// </summary>45         /// <param name="t"></param>46         public virtual void AddTask(TaskBase t)47         {48             taskQueue.Enqueue(t);49             ///防止线程正在阻塞时添加进入了新任务50             are.Set();51         }52 53         //通知一个或多个正在等待的线程已发生事件54         protected ManualResetEvent are = new ManualResetEvent(false);55 56         protected virtual void Run()57         {58             while (true)59             {60                 while (!taskQueue.IsEmpty)61                 {62                     TaskBase t = null;63                     if (!taskQueue.IsEmpty && taskQueue.TryDequeue(out
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表