How can i delay shutdown until virtualbox closed - c#

I try to shutdown vbox before local shutdown/restart/logoff etc.
i try something but dont work.
My code is:
private void VMStarter_Load(object sender, EventArgs e)
{
...
...
ShutDownHandle.StopShutdown(this.Handle, "Virtual Box is shutting down...");
}
private void tmr_doWork_tick(object sender, EventArgs e)
{
tmr_doWork.Enabled = false;
Controller.closeVM(); //for
while (!ShutDownHandle.ResetShutdown(this.Handle))
{
Thread.Sleep(10);
}
ShutDownHandle.Shutdown();
this.Close();
}
//[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
protected override void WndProc(ref Message m)
{
if (!systemShutdown)
if (m.Msg == (int)EnumClass.WindowsMessageCodes.SM_SHUTTINGDOWN ||
m.Msg == (int)EnumClass.WindowsMessageCodes.WM_ENDSESSION ||
m.Msg == (int)EnumClass.WindowsMessageCodes.WM_QUERYENDSESSION)
{
tmr_Check.Enabled = false;
//Message MyMsg = new Message();
//MyMsg.Msg = (int)EnumClass.WindowsMessageCodes.WM_CANCELMODE;
//base.WndProc(ref MyMsg);
systemShutdown = true;
tmr_doWork.Enabled = true;
return;
}
base.WndProc(ref m);
}
ShutDownHandle class: include StopShutdown and ResetShutdown methode. I use ShutdownBlockReasonCreate and ShutdownBlockReasonDestroy to stop closing window.
public static bool StopShutdown(IntPtr hWdn, string strMessage)
{
try
{
if (ShutdownBlockReasonCreate(hWdn, strMessage))
{
return true;
}
}
catch (Exception ex)
{
Writer.errorWrite(ex);
}
return false;
}
public static bool ResetShutdown(IntPtr hWdn)
{
try
{
return ShutdownBlockReasonDestroy(hWdn);
}
catch (Exception ex)
{
return false;
Writer.errorWrite(ex);
}
return false;
}
I click shutdown while app is running and get this
How can i do? what's wrong?

try this
in your method tmr_doWork_tick
put async like this
async void tmr_doWork_Tick(object sender, etc..)
{
// here is your thread.sleep();
// reemplace for await Task.delay(10);
}

Related

websocket-sharp - OnMessage callback is not running in the main thread

I have a WPF (.NET Framework 4.6) application that uses websocket-sharp (version 3.0.0) to create a websocket server.
I have a WebsocketServer and using EventHandler to tranfer event to MainWindow.xaml.cs but it not working. The MainWindow.xaml.cs listened to a RaiseOnScanDevice event but not any event invoked here.
I think this issue is relative to different thread. I try using Dispatcher.Invoke but it still not working.
System.Windows.Application.Current.Dispatcher.Invoke(new System.Action(() =>
{
RaiseOnScanDevice(this, new EventArgs());
}));
I found an issue (https://github.com/sta/websocket-sharp/issues/350) but the answers do not resolve my issue.
Please help me a solution for this issue.
WebsocketServer.cs file
public class WebsocketServer : WebSocketBehavior
{
private static readonly Lazy<WebsocketServer> lazyInstance = new Lazy<WebsocketServer>(() => new WebsocketServer());
public static WebsocketServer Instance
{
get
{
return lazyInstance.Value;
}
}
private const string TAG = "WebsocketServer";
private const string HOST_IP_ADDRESS = "127.0.0.2"; // localhost
private const int PORT = 38001;
public WebSocketServer socket;
private PacketHandler packetHandler = new PacketHandler();
public event EventHandler<EventArgs> RaiseOnScanDevice = new EventHandler<EventArgs>((a, e) => { });
public WebsocketServer()
{
Initialize();
}
public void Initialize()
{
socket = new WebSocketServer(IPAddress.Parse(HOST_IP_ADDRESS), PORT);
socket.AddWebSocketService<WebsocketServer>("/");
StartServer();
}
public void StartServer()
{
socket.Start();
}
public void StopServer()
{
socket.Stop();
}
protected override Task OnOpen()
{
return base.OnOpen();
}
protected override Task OnClose(CloseEventArgs e)
{
return base.OnClose(e);
}
protected override Task OnError(ErrorEventArgs e)
{
return base.OnError(e);
}
protected override Task OnMessage(MessageEventArgs e)
{
System.IO.StreamReader reader = new System.IO.StreamReader(e.Data);
string message = reader.ReadToEnd();
//Converting the event back to 'eventName' and 'JsonPayload'
PacketModel packet = packetHandler.OpenPacket(message);
HandleMessageFromClient(packet);
return base.OnMessage(e);
}
private void HandleMessageFromClient(PacketModel packet) {
var eventName = packet.EventName;
var data = packet.Data;
if (eventName == null || eventName.Equals(""))
{
return;
}
switch (eventName)
{
case SocketEvent.Hello:
Send("OK");
break;
case SocketEvent.ScanDevice:
ScanDevice();
break;
default:
break;
}
}
private void ScanDevice()
{
try
{
RaiseOnScanDevice(this, new EventArgs());
// or dispatch to Main Thread
System.Windows.Application.Current.Dispatcher.Invoke(new System.Action(() =>
{
RaiseOnScanDevice(this, new EventArgs());
}));
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
}
}
MainWindow.xaml.cs file
public partial class MainWindow : Window
{
public WebsocketServer WebsocketConnection
{
get { return WebsocketServer.Instance; }
}
public MainWindow()
{
InitializeComponent();
WebsocketConnection.RaiseOnScanDevice += SocketConnection_RaiseOnScanDevice;
}
private void SocketConnection_RaiseOnScanDevice(object sender, EventArgs e)
{
Console.WriteLine("SocketConnection_RaiseOnScanDevice");
}
The queue of messages is a good idea but you may want to use a lock to guard access to it. Most likely it won't be an issue but if you don't, you leave yourself open to the possibility of an error if the coroutine is reading from the queue as the websocket is writing to it. For example you could do something like this:
var queueLock = new object();
var queue = new Queue<MyMessageType>();
// use this to read from the queue
MyMessageType GetNextMessage()
{
lock (queueLock) {
if (queue.Count > 0) return queue.Dequeue();
else return null;
}
}
// use this to write to the queue
void QueueMessage(MyMessageType msg)
{
lock(queueLock) {
queue.Enqueue(msg);
}
}

How can I activate the previously opened microsoft access instance of an app using C#?

I'm using this code to open an access database :
public partial class Start_Baseet : System.Windows.Forms.Form
{
string MyFile = Environment.CurrentDirectory + "\\Baseet.accde";
Microsoft.Office.Interop.Access.Application AccApp = new Microsoft.Office.Interop.Access.Application();
public Start_Baseet()
{
InitializeComponent();
}
public void OpenDb()
{
AccApp.Visible = true;
AccApp.OpenCurrentDatabase(MyFile, false, "017014a");
AccApp.RunCommand(AcCommand.acCmdAppMaximize);
// AccApp.Activate();
}
}
private void Start_Basset_Load(object sender, EventArgs e)
{
try
{
OpenDb();
}
catch
{
AccApp.Quit();
MessageBox.Show("Something is missing", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
finally
{
this.Close();
System.Windows.Forms.Application.Exit();
System.Windows.Forms.Application.ExitThread();
// Process.GetCurrentProcess().CloseMainWindow();
}
The problem is the MSACCESS process is piling up in the running processes so I tried this :
//var prc = Process.GetProcessesByName("MSACCESS.EXE*32");
var prc = Process.GetProcessesByName("Microsoft Access");
if (prc.Length > 0)
{
MessageBox.Show("Access Found");
SetForegroundWindow(prc[0].MainWindowHandle);
}
else
{
AccApp.Visible = true;
AccApp.OpenCurrentDatabase(MyFile, false, "017014a");
AccApp.RunCommand(AcCommand.acCmdAppMaximize);
// AccApp.Activate();
}
}
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
But still with every time I use the code another MSACCESS process starts.
How can I fix this ?
Other point if I ran my app second time it will open a new instance of the same database can I activate the database if it is opened otherwise open a new instance of it ?
Thanks
Try this. This should catch users closing out Access as well through error catching. I'm sure it can be improved on but, I don't get a bunch of MSAccess.exe in the background from this code I wrote a while ago.
public partial class Form1 : Form
{
Microsoft.Office.Interop.Access.Application accApp = new Microsoft.Office.Interop.Access.Application();
private bool isFormClosed = false;
public Form1()
{
InitializeComponent();
OpenMicrosoftAccessFile(#"FileName");
Thread t = new Thread(new ThreadStart(CheckIfMSAccessExeIsRunning));
t.Start();
}
/// <summary>
/// The User Closed Out Access Cleanup.
/// </summary>
public void CheckIfMSAccessExeIsRunning()
{
int secondsToWait = 5*1000;
while(!isFormClosed)
{
if (accApp != null &&
accApp.Visible == false)
CloseMicrosoftAccessFile();
Thread.Sleep(secondsToWait);
}
CloseMicrosoftAccessFile();
}
private bool OpenMicrosoftAccessFile(string accessFileName)
{
try
{
if (accApp != null &&
!accApp.Visible)
{
CloseMicrosoftAccessFile();
}
if (accApp == null)
{
accApp = new Microsoft.Office.Interop.Access.Application();
accApp.OpenCurrentDatabase(accessFileName);
accApp.Visible = true;
}
return true;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine((ex.InnerException != null) ? ex.InnerException : "");
CloseMicrosoftAccessFile();
return false;
}
}
private void CloseMicrosoftAccessFile()
{
try
{
if (accApp != null)
{
accApp.CloseCurrentDatabase();
accApp.Quit();
}
}
catch (Exception ex)
{
//Good chance there never was an Access exe.
Console.WriteLine(ex.Message);
Console.WriteLine((ex.InnerException != null) ? ex.InnerException : "");
}
finally
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(accApp);
accApp = null;
}
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
isFormClosed = true;
}
}
Also, if you still have processes running the Debugger or Visual Studio is probably holding onto it still. It would be good to test this from the release exe. If OpenMicrosoftAccessFile returns false you can try opening it up again but, this way ignores the error it caught.

Restart InActivity Monitor after timer is stopped

I have WinForms App where I am using the code in this following Post to check the InActivity Status of my app (Please see the accepted answer in the post). InActivity In WinForms. Once the app reaches inactivity its stopping the inactivity monitor. But then I want to restart the time once the user logs in.
So I have a notification mechanism when the user logs in and I am calling the start timer method again. I get the Started Monitor Message but the app never tracks inactivity and I don't get Timer reporting app is InACTIVE message at all. Please help.
public static System.Windows.Forms.Timer IdleTimer =null;
static int MilliSeconds = 60000;
static void Main(string[] args)
{
f = new GeneStudyForm(true, arguments.SystemTimeOutFolder, arguments.SystemTimeOutFile, StartInActivityMonitor);
int x = StartInActivityMonitor();
}
public static void StartInActivityMonitor()
{
IdleTimer = new Timer();
LeaveIdleMessageFilter limf = new LeaveIdleMessageFilter();
Application.AddMessageFilter(limf);
IdleTimer.Interval = MilliSeconds; //One minute; change as needed
Application.Idle += new EventHandler(Application_Idle);
if (IdleTimer != null)
{
MessageBox.Show(IdleTimer.Interval.ToString());
}
IdleTimer.Tick += TimeDone;
IdleTimer.Tag = InActivityTimer.Started;
MessageBox.Show("starting");
IdleTimer.Start();
}
static private void Application_Idle(object sender, EventArgs e)
{
if (!IdleTimer.Enabled) // not yet idling?
IdleTimer.Start();
}
static private void TimeDone(object sender, EventArgs e)
{
try
{
MessageBox.Show("Stopped");
IdleTimer.Stop(); // not really necessary
f.MonitorDirectory();
f.UpdateInActivityStatus();
IdleTimer.Tick -= TimeDone;
Application.Idle -= new EventHandler(Application_Idle);
}
catch(Exception ex)
{
MessageBox.Show(ex.InnerException + ex.Data.ToString());
}
}
Here is my GeneStudyForm
public partial class GeneStudyForm
{
GeneStudySystemTimeOutIO GeneStudyIO;
Func<int> StartTimer;
//Passing the StartInActivityMonitor Method as Func Delegate
public GeneStudyForm(bool isStandalone, string TimeOutFolder, string TimeOutFile, System.Func<int> MyMethod)
{
GeneStudyIO = GeneStudySystemTimeOutIO.GetInstance(TimeOutFolder, TimeOutFile);
UpdateActivityStatus(AppName.GeneStudyStatus, ActivityStatus.Active);
this.StartTimer = MyMethod;
}
public void UpdateActivityStatus(AppName name, ActivityStatus status)
{
if (GeneStudyIO != null)
{
GeneStudyIO.WriteToFile(name, status);
}
}
public void MonitorDirectory()
{
FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(GeneStudyIO.GetDriectory());
fileSystemWatcher.NotifyFilter = NotifyFilters.LastWrite;
fileSystemWatcher.Filter = "*.json";
fileSystemWatcher.Changed += FileSystemWatcher_Changed;
fileSystemWatcher.EnableRaisingEvents = true;
}
public void UnRegister(FileSystemWatcher fileSystemWatcher)
{
fileSystemWatcher.Changed -= FileSystemWatcher_Changed;
}
// I am writing the inactive status to a file. So this event will fill
private void FileSystemWatcher_Changed(object sender, FileSystemEventArgs e)
{
try
{
var root = GeneStudyIO.GetDesrializedJson();
if (root != null && root.AllApplications != null)
{
var item = root.AllApplications.Any(x => x.Status == ActivityStatus.Active.ToString());
if (!item)
{
if (InActivecount == 0)
{
GeneStudyAndApplicationCommon.TimeStatus = InActivityTimer.Ended;
MessageBox.Show("I am hiding");
this.Hide();
InActivecount++;
}
}
else
{
if (GeneStudyAndApplicationCommon.TimeStatus == InActivityTimer.Ended)
{
MessageBox.Show("I am showing");
this.Show();
UnRegister(sender as FileSystemWatcher);
UpdateActivityStatus(AppName.GeneStudyStatus, ActivityStatus.Active);
MessageBox.Show("Updated Status");
if (StartTimer != null)
{
MessageBox.Show("Starting Timer again");
if (StartTimer() == -1)
{
MessageBox.Show("Couldn't start timer");
}
}
}
}
}
}
catch (Exception ex)
{
SystemDebugLogLogger.LogException(ex);
}
}
}
This soulution is quite different from what I have posted. But I could solve my problem with this. But I want to post it if it helps someone. Here is the post I am following Last User Input
I created a class called IdleCheck where I am getting LastUserInput as follows
public static class IdleCheck
{
[StructLayout(LayoutKind.Sequential)]
private struct LASTINPUTINFO
{
[MarshalAs(UnmanagedType.U4)]
public int cbSize;
[MarshalAs(UnmanagedType.U4)]
public int dwTime;
}
[DllImport("user32.dll")]
private static extern bool GetLastInputInfo(ref LASTINPUTINFO x);
public static int GetLastInputTime()
{
var inf = new LASTINPUTINFO();
inf.cbSize = Marshal.SizeOf(inf);
inf.dwTime = 0;
return (GetLastInputInfo(ref inf)) ? Environment.TickCount - inf.dwTime : 0;
}
}
Next in the actual Form this is my code. I am using a simple yes no message box to see if the timer can be stopped and recalled again when needed. You can apply your own locking mechanism.
I want the app to time out if it is InActive for 20 seconds. Change it as needed.
public partial class Form1 : Form
{
Timer timer;
const int TIMEOUT_DONE = 20000;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Reset();
}
void timer_Tick(object sender, EventArgs e)
{
//var ms = TIMEOUT_DONE - IdleCheck.GetLastInputTime();
if (IdleCheck.GetLastInputTime() > TIMEOUT_DONE)
{
DialogResult dialogResult = MessageBox.Show("Sure", "Some Title", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
Stop();
Reset();
}
}
}
public void Reset()
{
timer = new Timer();
timer.Interval = 10000;
timer.Tick += timer_Tick;
timer.Start();
}
public void Stop()
{
timer.Tick -= timer_Tick;
timer.Stop();
}
}

CALL_STATE.CS_OFFERING not works at TAPI programming

I am using tapi programming in order to communicate
with a device and send-receive calls. At this moment i am able
to make external calls, and "see" who is calling me when
i pick up the phone. For some reason i can't see the number
at the event < CALL_STATE.CS_OFFERING > (When your phone rings).
I post my code below (it is similar to one i found on the internet).
Any help will be appreciated!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace TapiSample
{
public partial class Form1 : Form
{
static public IAsyncResult result;
public Form1()
{
InitializeComponent();
tapi = new TAPI3Lib.TAPIClass();
tapi.Initialize();
foreach (TAPI3Lib.ITAddress ad in (tapi.Addresses as TAPI3Lib.ITCollection))
{
cbLines.Items.Add(ad.AddressName);
}
tapi.EventFilter = (int)(TAPI3Lib.TAPI_EVENT.TE_CALLNOTIFICATION |
TAPI3Lib.TAPI_EVENT.TE_CALLINFOCHANGE |
TAPI3Lib.TAPI_EVENT.TE_DIGITEVENT |
TAPI3Lib.TAPI_EVENT.TE_PHONEEVENT |
TAPI3Lib.TAPI_EVENT.TE_CALLSTATE |
TAPI3Lib.TAPI_EVENT.TE_GENERATEEVENT |
TAPI3Lib.TAPI_EVENT.TE_GATHERDIGITS |
TAPI3Lib.TAPI_EVENT.TE_REQUEST);
tapi.ITTAPIEventNotification_Event_Event += new TAPI3Lib.ITTAPIEventNotification_EventEventHandler(tapi_ITTAPIEventNotification_Event_Event);
}
TAPI3Lib.TAPIClass tapi = null;
TAPI3Lib.ITAddress line = null;
int cn = 0;
private void button1_Click(object sender, EventArgs e)
{
if (line != null)
{
line = null;
if (cn != 0) tapi.UnregisterNotifications(cn);
}
foreach (TAPI3Lib.ITAddress ad in (tapi.Addresses as TAPI3Lib.ITCollection))
{
if (ad.AddressName == cbLines.Text)
{
line = ad;
break;
}
}
if (line != null)
{
cn = tapi.RegisterCallNotifications(line, true, true, TAPI3Lib.TapiConstants.TAPIMEDIATYPE_AUDIO, 2);
}
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
if (cn != 0) tapi.UnregisterNotifications(cn);
}
delegate void AddLogDelegate(string text);
private void AddLog(string text)
{
if (this.InvokeRequired)
{
result = this.BeginInvoke(new AddLogDelegate(AddLog), new object[] { text });
}
listBox1.Items.Insert(0, text);
}
private void tapi_ITTAPIEventNotification_Event_Event(TAPI3Lib.TAPI_EVENT TapiEvent, object pEvent)
{
try
{
switch (TapiEvent)
{
case TAPI3Lib.TAPI_EVENT.TE_CALLNOTIFICATION:
AddLog("call notification event has occured");
break;
case TAPI3Lib.TAPI_EVENT.TE_CALLSTATE:
TAPI3Lib.ITCallStateEvent tcallStateEvent = (TAPI3Lib.ITCallStateEvent)pEvent;
TAPI3Lib.ITCallInfo b = tcallStateEvent.Call;
switch (b.CallState)
{
case TAPI3Lib.CALL_STATE.CS_OFFERING:
string str2 = b.get_CallInfoString(TAPI3Lib.CALLINFO_STRING.CIS_CALLERIDNUMBER);
AddLog("Number Calling:" + str2); //Doesn't work
return;
case TAPI3Lib.CALL_STATE.CS_CONNECTED:
string str = b.get_CallInfoString(TAPI3Lib.CALLINFO_STRING.CIS_CALLERIDNUMBER);
AddLog("Communicating with: " + str);
return;
case TAPI3Lib.CALL_STATE.CS_DISCONNECTED:
this.EndInvoke(result);
AddLog("Call Disconnected");
return;
}
break;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private void button2_Click(object sender, EventArgs e)
{
if (line == null) return;
TAPI3Lib.ITBasicCallControl bc = line.CreateCall(teNumber.Text, TAPI3Lib.TapiConstants.LINEADDRESSTYPE_PHONENUMBER, TAPI3Lib.TapiConstants.TAPIMEDIATYPE_AUDIO);
bc.Connect(false);
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
For same kind of problem I used a managed C# wrapper for Tapi written by Julmar , You can download its dll,
By using this Sample you can also record incoming call in .wav format
TPhone tphone;
TTapi tobj;
TTerminal recordTerminal;
TCall CurrCall;
void InitializeTapi()
{
tobj = new TTapi();
tobj.Initialize();
tobj.TE_CALLNOTIFICATION += new System.EventHandler<JulMar.Tapi3.TapiCallNotificationEventArgs>(this.OnNewCall);
tobj.TE_CALLSTATE += new System.EventHandler<JulMar.Tapi3.TapiCallStateEventArgs>(this.OnCallState);
tobj.TE_CALLINFOCHANGE += tobj_TE_CALLINFOCHANGE;
foreach (TPhone tp in tobj.Phones)
{
tphone = tp;
tphone.Open(PHONE_PRIVILEGE.PP_OWNER);
}
foreach (TAddress addr in tobj.Addresses)
{
if (addr.QueryMediaType(TAPIMEDIATYPES.AUDIO))
{
try
{
addr.Open(TAPIMEDIATYPES.AUDIO);
}
catch (TapiException ex)
{
if (ex.ErrorCode == unchecked((int)0x80040004))
{
try
{
addr.Open(TAPIMEDIATYPES.DATAMODEM);
}
catch (Exception ex2)
{
}
}
}
}
}
}
void tobj_TE_CALLINFOCHANGE(object sender, TapiCallInfoChangeEventArgs e)
{
try
{
CurrCall = e.Call;
txtCallerId.Text = e.Call.get_CallInfo(CALLINFO_STRING.CIS_CALLERIDNUMBER).ToString();
objCallLog.CallerID = txtCallerId.Text;
Task.Factory.StartNew(() => AnswerCall());
}
catch (Exception ex)
{
}
}
void OnNewCall(object sender, TapiCallNotificationEventArgs e)
{
CurrCall = e.Call;
}
void OnCallState(object sender, EventArgs E)
{
try
{
TapiCallStateEventArgs e = E as TapiCallStateEventArgs;
CurrCall = e.Call;
TapiPhoneEventArgs ev = E as TapiPhoneEventArgs;
switch (e.State)
{
case CALL_STATE.CS_OFFERING:
break;
case CALL_STATE.CS_CONNECTED:
break;
case CALL_STATE.CS_DISCONNECTED:
try
{
if (recordTerminal != null)
recordTerminal.Stop();
recordTerminal = null;
CurrCall.Dispose();
}
catch (Exception ex)
{
}
finally
{
CurrCall = null;
}
break;
}
}
catch (Exception ex)
{
}
}
void OnCallChangeEvent(object sender, TapiCallInfoChangeEventArgs e)
{
CurrCall = e.Call;
}
private void AnswerCall()
{
try
{
lock (lockAnswer)
{
if (CallStat == CallState.Offering)
{
CurrCall.Answer();
RecordConversation();
}
else
{
}
}
}
catch (Exception ex)
{
}
}
void RecordConversation()
{
if (CurrCall != null)
{
try
{
recordTerminal = CurrCall.RequestTerminal(
TTerminal.FileRecordingTerminal,
TAPIMEDIATYPES.MULTITRACK, TERMINAL_DIRECTION.TD_RENDER);
if (recordTerminal != null)
{
recordTerminal.RecordFileName = "FileName.wav";
CurrCall.SelectTerminalOnCall(recordTerminal);
recordTerminal.Start();
}
else
{
MessageBox.Show("Error in recording file.");
}
}
catch (TapiException ex)
{
MessageBox.Show(ex.ToString());
}
}
}
Internally TAPI handles state and call information separately. So the calling number (a.k.a. CLIP) can be sent before or after the offering state itself. So you are not guaranteed to have the CLIP at the time of the offering state. It can come later in a a call info change event.
You are already requesting TAPI3Lib.TAPI_EVENT.TE_CALLINFOCHANGE in your filter but you are not handling it in your TapiEvent switch statement. So you will need to implement this.
Side note: it is possible for something calling you to not have a CLIP

Best way to do a task looping in Windows Service

I have a method that send some SMS to our customers that look like below:
public void ProccessSmsQueue()
{
SmsDbContext context = new SmsDbContext();
ISmsProvider provider = new ZenviaProvider();
SmsManager manager = new SmsManager(context, provider);
try
{
manager.ProcessQueue();
}
catch (Exception ex)
{
EventLog.WriteEntry(ex.Message, EventLogEntryType.Error);
}
finally
{
context.Dispose();
}
}
protected override void OnStart(string[] args)
{
Task.Factory.StartNew(DoWork).ContinueWith( ??? )
}
So, I have some issues:
I donĀ“t know how long it takes for the method run;
The method can throw exceptions, that I want to write on EventLog
I want to run this method in loop, every 10 min, but only after last execution finish.
How I can achieve this? I thought about using ContinueWith(), but I still have questions on how to build the entire logic.
You should have an async method that accepts a CancellationToken so it knows when to stop, calls ProccessSmsQueue in a try-catch block and uses Task.Delay to asynchronously wait until the next time it needs to run:
public async Task DoWorkAsync(CancellationToken token)
{
while (true)
{
try
{
ProccessSmsQueue();
}
catch (Exception e)
{
// Handle exception
}
await Task.Delay(TimeSpan.FromMinutes(10), token);
}
}
You can call this method when your application starts and Task.Wait the returned task before existing so you know it completes and has no exceptions:
private Task _proccessSmsQueueTask;
private CancellationTokenSource _cancellationTokenSource;
protected override void OnStart(string[] args)
{
_cancellationTokenSource = new CancellationTokenSource();
_proccessSmsQueueTask = Task.Run(() => DoWorkAsync(_cancellationTokenSource.Token));
}
protected override void OnStop()
{
_cancellationTokenSource.Cancel();
try
{
_proccessSmsQueueTask.Wait();
}
catch (Exception e)
{
// handle exeption
}
}
Sample Worker Class that I have used in Windows Services. It supports stopping in a 'clean' way by using a lock.
You just have to add your code in DoWork, set your timer in the StartTimerAndWork method (in milliseconds), and use this class in your service.
public class TempWorker
{
private System.Timers.Timer _timer = new System.Timers.Timer();
private Thread _thread = null;
private object _workerStopRequestedLock = new object();
private bool _workerStopRequested = false;
private object _loopInProgressLock = new object();
private bool _loopInProgress = false;
bool LoopInProgress
{
get
{
bool rez = true;
lock (_loopInProgressLock)
rez = _loopInProgress;
return rez;
}
set
{
lock (_loopInProgressLock)
_loopInProgress = value;
}
}
#region constructors
public TempWorker()
{
}
#endregion
#region public methods
public void StartWorker()
{
lock (_workerStopRequestedLock)
{
this._workerStopRequested = false;
}
_thread = new Thread(new ThreadStart(StartTimerAndWork));
_thread.Start();
}
public void StopWorker()
{
if (this._thread == null)
return;
lock (_workerStopRequestedLock)
this._workerStopRequested = true;
int iter = 0;
while (LoopInProgress)
{
Thread.Sleep(100);
iter++;
if (iter == 60)
{
_thread.Abort();
}
}
//if (!_thread.Join(60000))
// _thread.Abort();
}
#endregion
#region private methods
private void StartTimerAndWork()
{
this._timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
this._timer.Interval = 10000;//milliseconds
this._timer.Enabled = true;
this._timer.Start();
}
#endregion
#region event handlers
private void timer_Elapsed(object sender, ElapsedEventArgs e)
{
if (!LoopInProgress)
{
lock (_workerStopRequestedLock)
{
if (this._workerStopRequested)
{
this._timer.Stop();
return;
}
}
DoWork();
}
}
private void DoWork()
{
try
{
this.LoopInProgress = true;
//DO WORK HERE
}
catch (Exception ex)
{
//LOG EXCEPTION HERE
}
finally
{
this.LoopInProgress = false;
}
}
#endregion
}

Categories

Resources