Trong dự án của tôi, tôi cần khóa ứng dụng (giống như khóa cửa sổ). Nếu ứng dụng không hoạt động trong một khoảng thời gian, ứng dụng sẽ bị khóa, cửa sổ đăng nhập cho ứng dụng sẽ xuất hiện. Làm thế nào tôi có thể làm điều đó trong một ứng dụng WPF C#?Kiểm tra xem ứng dụng có rảnh trong một khoảng thời gian không và khóa nó
Trả lời
Bạn có thể sử dụng các chức năng này
thấy mã này, bạn phải thêm một bộ đếm thời gian để hình của bạn, và thiết lập this.timer1.Enabled = true;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication9
{
internal struct LASTINPUTINFO
{
public uint cbSize;
public uint dwTime;
}
public partial class Form1 : Form
{
[DllImport("User32.dll")]
public static extern bool LockWorkStation();
[DllImport("User32.dll")]
private static extern bool GetLastInputInfo(ref LASTINPUTINFO Dummy);
[DllImport("Kernel32.dll")]
private static extern uint GetLastError();
public static uint GetIdleTime()
{
LASTINPUTINFO LastUserAction = new LASTINPUTINFO();
LastUserAction.cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(LastUserAction);
GetLastInputInfo(ref LastUserAction);
return ((uint)Environment.TickCount - LastUserAction.dwTime);
}
public static long GetTickCount()
{
return Environment.TickCount;
}
public static long GetLastInputTime()
{
LASTINPUTINFO LastUserAction = new LASTINPUTINFO();
LastUserAction.cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(LastUserAction);
if (!GetLastInputInfo(ref LastUserAction))
{
throw new Exception(GetLastError().ToString());
}
return LastUserAction.dwTime;
}
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (GetIdleTime() > 10000) //10 secs, Time to wait before locking
LockWorkStation();
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Start();
}
}
}
Set một thời gian chờ trên tải, và mỗi khi "hoạt động" hành động xảy ra (bạn sẽ cần phải treo lên cho họ), thiết lập lại bộ đếm thời gian trở lại để bắt đầu.
Có ứng dụng mẫu nào không? – Sauron
IMO câu trả lời được chấp nhận là không tốt như phương pháp này:
http://www.codeproject.com/Articles/30345/Application-Idle
Các bài viết CodeProject sử dụng thông điệp của Windows mà sẽ gây ra các thành phần để xem xét việc áp dụng không nhàn rỗi, ví dụ:
public enum ActivityMessages : int
{
/// <summary>
/// Cursor moved while within the nonclient area.
/// </summary>
WM_NCMOUSEMOVE = 0x00A0,
/// <summary>
/// Mouse left button pressed while the cursor was within the nonclient area.
/// </summary>
WM_NCLBUTTONDOWN = 0x00A1,
/// <summary>
/// Mouse left button released while the cursor was within the nonclient area.
/// </summary>
WM_NCLBUTTONUP = 0x00A2,
/// <summary>
Bạn có biết cách đạt được ứng dụng MVC không? – alice7
Điều này phát hiện khi toàn bộ hệ thống không hoạt động, không phải khi một ứng dụng cụ thể không hoạt động. –