国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

輔助功能性代碼,研究和記錄代碼。

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://m.survivalescaperooms.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
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 奈曼旗| 兖州市| 吐鲁番市| 咸阳市| 化隆| 永平县| 蓝田县| 平舆县| 昭通市| 新竹县| 屯昌县| 隆化县| 樟树市| 铅山县| 长顺县| 如皋市| 鹤庆县| 九江县| 商都县| 区。| 沐川县| 永川市| 南川市| 城市| 天门市| 任丘市| 霍邱县| 康定县| 邯郸市| 罗定市| 蒙山县| 岳阳县| 托里县| 余干县| 大庆市| 郓城县| 禄劝| 兴文县| 景泰县| 宜丰县| 丹凤县|