class WakeUP
{
[DllImport("kernel32.dll")]
public static extern SafeWaitHandle CreateWaitableTimer(IntPtr lpTimerAttributes,
bool bManualReset,
string lpTimerName);
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetWaitableTimer(SafeWaitHandle hTimer,
[In] ref long pDueTime,
int lPeriod,
IntPtr pfnCompletionRoutine,
IntPtr lpArgToCompletionRoutine,
bool fResume);
public event EventHandler Woken;
private BackgroundWorker bgWorker = new BackgroundWorker();
public WakeUP()
{
bgWorker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
bgWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgWorker_RunWorkerCompleted);
}
public void SetWakeUpTime(DateTime time, String tName)
{
// Create a 7-tuple.
var wutargs = new Tuple<string, string>(time.ToFileTime().ToString(), tName.ToString());
bgWorker.RunWorkerAsync(wutargs);
}
void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (Woken != null)
{
Woken(this, new EventArgs());
}
}
private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
var thetuple = e.Argument;
long wakeuptime = (long)thetuple.Item1;
using (SafeWaitHandle handle = CreateWaitableTimer(IntPtr.Zero, true, "Timer"))
{
//if (SetWaitableTimer(handle, ref "12:00 AM", 0, IntPtr.Zero, IntPtr.Zero, true))
//{
//using (EventWaitHandle wh = new EventWaitHandle(false, EventResetMode.AutoReset))
//{
//wh.SafeWaitHandle = handle;
//wh.WaitOne();
//}
//}
//else
//{
//throw new Win32Exception(Marshal.GetLastWin32Error());
//}
}
}
Visual Studio告诉我:
long wakeuptime = (long)thetuple.Item1;
错误1'对象'不包含'Item1'的定义,也没有扩展方法'Item1'接受类型'object'的第一个参数(你是否缺少using指令或汇编引用?)C :\ Users \ esepich \ Documents \ Visual Studio 2013 \ Projects \ SepysAlarmV1ecs \ SepysAlarmV1 \ WakeUP.cs 57 46 SepysAlarmV1
我应该如何访问元组的元素?
感谢您发帖...
答案 0 :(得分:5)
参数可以是任何类型。它作为object
传递,因此将其转换回原始类型:
var thetuple = (Tuple<string, string>)e.Argument;
你也会在下一行得到编译错误:
long wakeuptime = (long)thetuple.Item1;