BomKhung.Com Đã Quay Trở Lại
Hoạt Động Dưới Tên Miền Hung.Pro.VN
00 Days
00 Hours
00 Minutes
00 Seconds
Hiện website đang cập nhật nội dung bài viết, nếu có lỗi gì mọi người có thể thông báo cho mình Tại đây!

[CSHARP] Detect App Running and Bring to Front - Single Intance App

Xin chào các bạn, bài viết hôm nay mình hướng dẫn các bạn cách kiểm tra ứng dụng đang chạy hay chưa, và nếu ứng dụng đang chạy thì Bring to Front App, cho người dùng biết app đang chạy, không mở thêm mới một Instance App khác.
[C#] Detect App Running and Bring to Front - Single Intance App

Source code C# chỉnh ở file program.cs:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DetectAppRunning
{
    internal static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            if (CheckForAnotherInstance())
            {
                Debug.Write("Detected another instance. Close.");
                return;
            }
            Application.Run(new Form1());
        }

        

        private static bool CheckForAnotherInstance()
        {
            try
            {
                Process currentProcess = Process.GetCurrentProcess();
                foreach (Process process in Process.GetProcessesByName(currentProcess.ProcessName))
                {
                    if (process.Id != currentProcess.Id)
                    {
                        SwitchToThisWindow(process.MainWindowHandle, true);
                        Application.Exit();
                        return true;
                    }
                }
            }
            catch (Exception ex)
            {
                return false;
            }
            return false;
        }

        // Token: 0x060001C3 RID: 451
        [DllImport("user32.dll")]
        public static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);
    }
}

Và code C# sau khi mình chuyển đổi sang Visual Basic
Imports System
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Linq
Imports System.Runtime.InteropServices
Imports System.Threading.Tasks
Imports System.Windows.Forms

Namespace DetectAppRunning
    Friend Module Program
        <STAThread>
        Private Sub Main()
            Application.EnableVisualStyles()
            Application.SetCompatibleTextRenderingDefault(False)

            If CheckForAnotherInstance() Then
                Debug.Write("Detected another instance. Close.")
                Return
            End If

            Application.Run(New Form1())
        End Sub

        Private Function CheckForAnotherInstance() As Boolean
            Try
                Dim currentProcess As Process = Process.GetCurrentProcess()

                For Each process As Process In Process.GetProcessesByName(currentProcess.ProcessName)

                    If process.Id <> currentProcess.Id Then
                        SwitchToThisWindow(process.MainWindowHandle, True)
                        Application.[Exit]()
                        Return True
                    End If
                Next

            Catch ex As Exception
                Return False
            End Try

            Return False
        End Function

        <DllImport("user32.dll")>
        Sub SwitchToThisWindow(ByVal hWnd As IntPtr, ByVal fAltTab As Boolean)
    End Module
End Namespace

Các bạn có thể download ứng dụng về để chạy test thử.

DOWNLOAD SOURCE CODE

Theo LapTrinhVB.Net

إرسال تعليق

Đồng ý sữ dụng cookie
Chúng tôi sử dụng cookie trên trang web này để phân tích lưu lượng truy cập, ghi nhớ tùy chọn của bạn và tối ưu hóa trải nghiệm của bạn.
Xem thêm
Oops!
Có vẻ như kết nối internet của bạn có vấn đề. Vui lòng kết nối lại và duyệt web.
AdBlock Detected!
Chúng tôi phát hiện bạn đang sử dụng plugin chặn quảng cáo trong trình duyệt của mình.
Doanh thu chúng tôi kiếm được từ quảng cáo được sử dụng để quản lý trang web này, chúng tôi yêu cầu bạn đưa trang web của chúng tôi vào danh sách trắng trong plugin chặn quảng cáo của bạn.
Site is Blocked
Sorry! This site is not available in your country.
Kỹ thuật số thế hệ tiếp theo Chào mừng bạn đến với trò chuyện WhatsApp
Xin chào! Chúng tôi có thể giúp gì cho bạn hôm nay?
Nhập vào đây...