I'm trying to detect an incoming call in the Lync client . This is done by subscribing to ConversationManager.ConversationAdded event in the Lync client as described in this post
However, by using this method I'm not able to detect incoming calls if a conversation window with the caller is already open before the caller is calling.
For instance if I'm chatting with a friend and therefore have a open conversation windows and this friend decides to call me, the ConversationAdded event is not triggered.
How would I detect incoming calls when I already have an active conversation with the caller?
Thanks,
Nicklas
You need to monitor the states of the modalities on the conversation. The two avaiable modalities are IM and AV, so you'll need to watch for state changes on these, like so:
void ConversationManager_ConversationAdded(object sender, Microsoft.Lync.Model.Conversation.ConversationManagerEventArgs e)
{
e.Conversation.Modalities[ModalityTypes.InstantMessage].ModalityStateChanged += IMModalityStateChanged;
e.Conversation.Modalities[ModalityTypes.AudioVideo].ModalityStateChanged += AVModalityStateChanged;
}
void IMModalityStateChanged(object sender, ModalityStateChangedEventArgs e)
{
if (e.NewState == ModalityState.Connected)
MessageBox.Show("IM Modality Connected");
}
void AVModalityStateChanged(object sender, ModalityStateChangedEventArgs e)
{
if (e.NewState == ModalityState.Connected)
MessageBox.Show("AV Modality Connected");
}
This sample is using the ConversationAdded event to wire up the event handlers for modality changes, so this will only work for conversations that are started while your application is running. To do the same for conversations that are already active before your application starts, you could add this code to your application's startup routine:
foreach (var conv in _lync.ConversationManager.Conversations)
{
conv.Modalities[ModalityTypes.InstantMessage].ModalityStateChanged += new EventHandler<ModalityStateChangedEventArgs>(IMModalityStateChanged);
conv.Modalities[ModalityTypes.AudioVideo].ModalityStateChanged += new EventHandler<ModalityStateChangedEventArgs>(AVModalityStateChanged);
}
You should subscribe to the ModalityStateChanged event on Conversation.Modalities[ModalityTypes.AudioVideo], this will give you events when the AV modality is created or changes state.
Related
I have 2 projects acting as a webserver and a game server. The game server has the webserver as a dependency so I can access its files in the code and I then want to subscribe to an event in the web server.
I have a Discord bot sending information to the webserver that in turn needs to send data to the game server. But as I can't access the game servers file from the webserver I created a static event in the webserver that the game server, in turn, subscribes to. But the event is always null, no matter what I do. Is it even possible to subscribe to an event cross-project like this?
Here is the event:
public static event EventHandler<CommandEventArgs> RecievedCommand = delegate{};
Here is the invoking method:
public static void InvokeEvent(string exector, string command, string channel)
{
if (!ulong.TryParse(exector, out var user))
{
Console.WriteLine("Not a user");
return;
}
if (!ulong.TryParse(channel, out var from))
{
Console.WriteLine("not a channel");
return;
}
Console.WriteLine("Full invoke");
CommandEventArgs cmd = new CommandEventArgs();
cmd.Channel = channel;
cmd.Command = command;
cmd.Executor = exector;
RecievedCommand.Invoke(typeof(BotBridge), cmd);
}
Here you can see how I invoke it:
if (query["execute"] != null)
{
Console.WriteLine("Invoking");
BotBridge.InvokeEvent(query["user"], query["command"], query["channel"]);
Write(context, "Success!", false);
return;
}
And here is a picture from the game server where I subscribe to the event. The event is registered in the creation of this class so I know for sure the code reaches that part:
BotBridge.RecievedCommand += HandleBotCommand;
}
public void HandleBotCommand(object sender, CommandEventArgs e)
{
The webserver reaches Console.Writeline("Full invoke") in the invoking method but nothing else happens after that. Any ideas?
It's not possible to subscribe to an event from a separate process via a project reference.
Even though your game server is referring the "correct web server code/project", it's not pointing to the correct instance.
Here's what's happening:
Your game server code is subscribing to an event from the web server assembly, but in its own process. It's not subscribing to the actually deployed web server.
In red: what you were expecting.
In green: what is actually happening.
For your situation, you would need another communication mechanism such as HTTP, TCP, etc.
I am trying to have some logic when Windows user is being changed while my UWP app is running and for that in my OnLaunchApplicationAsync method I have:
var userWatcher = Windows.System.User.CreateWatcher();
userWatcher.AuthenticationStatusChanged += AuthenticationStatusChanged;
and I also have
private void AuthenticationStatusChanged(Windows.System.UserWatcher sender, Windows.System.UserChangedEventArgs args)
{
// Some logic
}
But the problem is that when I log off with my current user and sign in with another one, the expected event is not being raised.
I have also tried out userWatcher.Updated for updating user data as well as userWatcher.AuthenticationStatusChanging with the same result.
Am I doing this in a wrong way?
You need to start the watcher, otherwise these events are not fired.
I am trying to get an event when a file is added on the "camera roll" album by a usb connection.
I thought in create a task to verify if the "camera roll" album is increasing or decreasing their size. But this will be very cost in the performance. So i am trying to find an event that tells me when the system file be altered.
Anyone can help me?
WinRT provides files and folders monitoring based on queries, which is an alternative to the .net FileSystemWatcher class, your app also needs to be subscribed on the query's ContentsChanged event to get notified
private void MainPage_OnLoaded(object sender, RoutedEventArgs e)
{
var query = KnownFolders.CameraRoll.CreateFileQuery();
query.ContentsChanged += QueryContentsChanged;
await query.GetFilesAsync();
}
void QueryContentsChanged(Windows.Storage.Search.IStorageQueryResultBase sender, object args)
{
// your handler code
}
Update
the GetFilesAsync is necessary to trigger the ContentsChanged event.
I'm starting out in C#, coded a lot in Java but having some trouble here. I'm trying to learn how to use MouseKeyHook for an application I'm developing. I cannot get the actual listener to fire off an event. Here's my listener code:
using System;
using System.Windows.Forms;
using Gma.System.MouseKeyHook;
namespace ChromaHeatmap
{
class keyListener
{
private IKeyboardMouseEvents m_GlobalHook;
public void Subscribe()
{
// Note: for the application hook, use the Hook.AppEvents() instead
m_GlobalHook = Hook.GlobalEvents();
m_GlobalHook.KeyPress += GlobalHookKeyPress;
}
private void GlobalHookKeyPress(object sender, KeyPressEventArgs e)
{
Console.WriteLine("blah");
}
public void Unsubscribe()
{
m_GlobalHook.KeyPress -= GlobalHookKeyPress;
//It is recommened to dispose it
m_GlobalHook.Dispose();
}
}
}
And here's the part of my application code where I attempt to do something with the listener. If anyone can let me know what the best way is to loop here and wait for events, I'd appreciate it.
//Listen for key presses
keyListener heyListen = new keyListener();
heyListen.Subscribe();
while(true)
{
}
while(true) {}
This is a hold-and-catch-fire statement, the thread will burn 100% core and cannot execute the hook callback. You'll notice that the machine goes dead for 5 seconds when you press a key, the operating system is waiting for an opportunity to invoke the callback. But it won't wait forever and unceremoniously will destroy the hook so you regain control over the machine. Also the kind of mishap that will occur when you try to debug your event handler.
Windows needs an opportunity to safely call the hook callback. That requires your program to be "idle", not executing any code. The technical term for this is "pumping the message loop", your program must wait for a notification from the operating system that something interesting happened.
A very simple way is to use the Winforms project template as-is, you'll also get a window. Note how the Main() method in the project makes the call that you need instead of the while() loop. You must call Application.Run().
Check this post for code that avoids displaying a window.
On client side, I handle the proxy state so that when its State==CommunicationState.Faulted, it will automatically call Abort() and gracefully transition to CommunicationState.Closed.
On server side, I have 2 events hooked up to callback channel
OperationContext.Current.Channel.Faulted += Channel_Faulted;
OperationContext.Current.Channel.Closed += Channel_Closed;
Here are my events code
private void Channel_Closed(object sender, EventArgs e)
{
var callback = sender as IPtiCommunicationCallback;
PtiClient client;
lock (syncObj)
{
client = clientsList.FirstOrDefault(x => x.Value == callback).Key;
}
if (client != null)
{
//Code to remove client from the list
Disconnect(client);
}
}
private void Channel_Faulted(object sender, EventArgs e)
{
(sender as ICommunicationObject).Abort();
}
Now the question: Will the duplex channel's (the callback channel) state automatically transition accordingly to client's or I have to handle the Faulted State as I did? I'm using NetTcpBinding by the way.
The Callback channel's state will generally mimic the client's state, but this is not guaranteed. For example, if the client is trying to reach the server to close the connection, its state might be Closing while the state on the server side could be Opened. You are handling it correctly in assuming that each side must handle Closed and Faulted states individually.
I have done bit research about what kind of binding should we use and finally decided to go ahead with nettcp binding. See my blog for detail explanation.
http://maheshde.blogspot.com.au/2013/06/duplex-communication-over-internet.html
I have no idea why those events not fired. But triggering WCF call from client every 10 minutes our problem solved.