在背景工作者运行时特定间隔后自动重定向问题。 [英] Auto redirection issue after specific interval while background worker running.
问题描述
我正在重定向特定的视图或页面,以防机器是理想的。我已经成功实现了代码。我的代码是
[DllImport(" user32.dll")]
  [StructLayout(LayoutKind.Sequential)]
static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
        struct LASTINPUTINFO
      {
            public static readonly int SizeOf = Marshal.SizeOf(typeof(LASTINPUTINFO));
          [的MarshalAs(UnmanagedType.U4)]
            public Int32 cbSize;
          [的MarshalAs(UnmanagedType.U4)]
            public Int32 dwTime;
      }
void timer_Tick(object sender,EventArgs e)
{
//如果用户空闲300秒(5分钟),则禁用当前的MainWindow
if(GetLastInputTime()> = 30)
{
var frame = App.Current.MainWindow.FindName(" mainContentPlaceHolder")as Frame;
if(frame!= null&&!Convert.ToString(frame.Source).Contains(" Views / Home.xaml")){
frame.Source = new Uri(" /Views/Home.xaml" ;, UriKind.Relative);
}
}
}
//以秒为单位返回用户空闲的时间段
static int GetLastInputTime()
{
int idleTime = 0;
LASTINPUTINFO lastInputInfo = new LASTINPUTINFO();
lastInputInfo.cbSize = Marshal.SizeOf(lastInputInfo);
lastInputInfo.dwTime = 0;
int envTicks =(int)Environment.TickCount;
if(GetLastInputInfo(ref lastInputInfo))
{
int lastInputTick = lastInputInfo.dwTime;
idleTime = envTicks - lastInputTick;
}
return((idleTime> 0)?(idleTime / 1000):0);
}
问题 - 我在后台执行API但是它无法检查后台进程并重定向到另一个视图过程的中间。我可以解决这个问题吗?
我可以通过获取另一个全局变量来做,但我需要在所有页面上保持此变量的值。我不喜欢这个。这是一个长度过程。
请建议更改给定代码。
谢谢。
你的意思是你不希望这个在其他进程运行的时候采取行动吗?
添加一个带有静态bool的静态类你可以用作IsBusy标志。
最初设置为false。
当你运行后台进程时设置为true。
当您的后台进程(或所有后台进程)完成后,再将其设置为false。
然后您可以修改if:
< pre class ="prettyprint"> if(GetLastInputTime()> = 30&&!SomeStatic.IsBusy)
I am redirecting on specific view or page in case of Machine is ideal. I have successfully implemented code. My code is
[DllImport("user32.dll")] static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
[StructLayout(LayoutKind.Sequential)]
struct LASTINPUTINFO
{
public static readonly int SizeOf = Marshal.SizeOf(typeof(LASTINPUTINFO));
[MarshalAs(UnmanagedType.U4)]
public Int32 cbSize;
[MarshalAs(UnmanagedType.U4)]
public Int32 dwTime;
}
void timer_Tick(object sender, EventArgs e) { //If the user is idle for 300 seconds (5 minutes) disable the current MainWindow if (GetLastInputTime() >= 30) { var frame = App.Current.MainWindow.FindName("mainContentPlaceHolder") as Frame; if (frame!=null && !Convert.ToString(frame.Source).Contains("Views/Home.xaml")){ frame.Source = new Uri("/Views/Home.xaml", UriKind.Relative); } } } //Returns the period of time the user is idle in seconds static int GetLastInputTime() { int idleTime = 0; LASTINPUTINFO lastInputInfo = new LASTINPUTINFO(); lastInputInfo.cbSize = Marshal.SizeOf(lastInputInfo); lastInputInfo.dwTime = 0; int envTicks = (int)Environment.TickCount; if (GetLastInputInfo(ref lastInputInfo)) { int lastInputTick = lastInputInfo.dwTime; idleTime = envTicks - lastInputTick; } return ((idleTime > 0) ? (idleTime / 1000) : 0); }
Issue - I am executing API in the background but its failed to check background process and redirecting on the another view in the middle of process. can i resolve this?
I can do by taking another global variable but I need maintain the value of this variable on all the pages. I do not like this. This is a length process.
please suggest changes in the given code.
Thanks.
You mean you don't want this to act whilst some other process is running?
Add a static class with a static bool you can use as an IsBusy flag.
Set it false initially.
When you're running a background process set it true.
When your background process ( or all background processes ) are complete set it false again.
You can then modify your if:
if (GetLastInputTime() >= 30 && !SomeStatic.IsBusy)
这篇关于在背景工作者运行时特定间隔后自动重定向问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!