I'm stuck in TAPI programming. I've created a program to monitor activity of phone call. Everything is working fine, but now I want to implement a functionality to accept and reject a call from web directly.
what I've done is below:
namespace Shared
{
public partial class Site : System.Web.UI.MasterPage
{
public static ITAddress ln;
static int index = -1;
static int line;
static ITAddress[] ia;
protected void Page_Load(object sender, EventArgs e)
{
#region TAPI
TAPIClass tobj;
int[] registertoken;
tobj = new TAPI3Lib.TAPIClass();
tobj.Initialize();
IEnumAddress ea = tobj.EnumerateAddresses();
uint lines;
uint arg3 = 0;
int TotalLines = 0;
lines = 0;
foreach (TAPI3Lib.ITAddress ad in (tobj.Addresses as TAPI3Lib.ITCollection))
{
TotalLines++;
}
callnotification cn = new callnotification();
tobj.ITTAPIEventNotification_Event_Event += new TAPI3Lib.ITTAPIEventNotification_EventEventHandler(cn.Event);
tobj.EventFilter = (int)(TAPI_EVENT.TE_CALLNOTIFICATION |
TAPI_EVENT.TE_DIGITEVENT |
TAPI_EVENT.TE_PHONEEVENT |
TAPI_EVENT.TE_CALLSTATE |
TAPI_EVENT.TE_GENERATEEVENT |
TAPI_EVENT.TE_GATHERDIGITS |
TAPI_EVENT.TE_REQUEST);
registertoken = new int[TotalLines];
ia = new TAPI3Lib.ITAddress[TotalLines];
for (int i = 0; i = 0)
{
ln = ia[line];
}
IEnumCall ec = ln.EnumerateCalls();
uint arg = 0;
ITCallInfo ici;
try
{
ec.Next(1, out ici, ref arg);
ITBasicCallControl2 bc = (TAPI3Lib.ITBasicCallControl2)ici;
if (ici != null && ici.CallState == CALL_STATE.CS_OFFERING)
{
if (bc != null)
{
bc.Answer();
}
}
}
catch (Exception ex)
{
COMException comEx = ex as COMException;
if (comEx != null)
comEx.ErrorCode.ToString();
else
{
string aa = ex.Message;
}
}
//addtolist("Call Offering from " + callernumber + " to Ext " + viaextension + " via DID " + DIDNumber);
break;
case TAPI3Lib.CALL_STATE.CS_IDLE:
//addtolist("Call is created!");
break;
}
break;
}
}
catch (Exception ex)
{
//MessageBox.Show(ex.StackTrace.ToString());
}
}
}
#endregion
}
}
I'm always getting ITBasicCallControl2 bc NULL and when I press the Accept button, nothing happens.
Okay, so the first thing you have to do in order to successfully answer calls, is to register your line with owner rights.
tobj.RegisterCallNotifications(ln, true, true, TapiConstants.TAPIMEDIATYPE_AUDIO, 2);
After that, you can either iterate through every call using EnumerateCalls(), or you can implement the ITTAPIEventNotification interface to get notified if there is a change in callstate (for example).
One way or the other, at some point, you have found the call you want to answer. Now you need to make sure, that the call is in an alerting state (CS_OFFERING for inbound calls), before you can finally call the answer method.
try
{
ec.Next(1, out ici, ref arg);
if (ici != null && ici.CallState == CALL_STATE.CS_OFFERING)
{
ITBasicCallControl2 bc = (TAPI3Lib.ITBasicCallControl2)ici;
if (bc != null)
{
bc.Answer();
}
}
}
catch (Exception exp)
{
COMException comEx = exp as COMException;
if (comEx != null)
MessageBox.Show(comEx.ErrorCode.ToString());
else
MessageBox.Show(exp.Message);
}
If the call you want to answer is not in the callstate CS_CONNECTED, the method will throw a COMException with an error code of 0x800040010.
For further information on the ITTAPIEventNotification interface, see https://msdn.microsoft.com/en-us/library/windows/desktop/ms732506(v=vs.85).aspx
EDIT:
If you want to detect new incoming calls, I'd recommend to use the TE_CALLNOTIFICATION-Event, because it is triggered only once per new incoming call.
The TE_CALLSTATE-Event will be triggered every time the callstate changes.
Now, i've updated the callnotification class:
public class callnotification : TAPI3Lib.ITTAPIEventNotification
{
public InboundCall OnNewIncomingCall;
public void Event(TAPI_EVENT TapiEvent, object pEvent)
{
switch (TapiEvent)
{
case TAPI_EVENT.TE_CALLNOTIFICATION:
this.OnCallNotification((ITCallNotificationEvent)pEvent);
break;
}
}
private void OnCallNotification(ITCallNotificationEvent callNotification)
{
ITCallInfo ici = callNotification.Call;
if (ici != null && ici.CallState == CALL_STATE.CS_OFFERING)
this.OnNewIncomingCall(ici);
}
}
I've also declared a delegate Method to use if there is a new inbound call:
public delegate void InboundCall(ITCallInfo ici);
So your initialization of the callnotification event could look like this:
callnotification cn = new callnotification();
cn.OnNewIncomingCall += this.OnNewIncomingCall;
And finally, in the OnNewIncomingCallMethod, you can answer the call:
private void OnNewIncomingCall(ITCallInfo ici)
{
ITBasicCallControl bcc = (ITBasicCallControl)ici;
if (bcc != null)
{
string caller = ici.get_CallInfoString(CALLINFO_STRING.CIS_CALLERIDNUMBER);
DialogResult dlg = MessageBox.Show(string.Format("New incoming call from {0}\r\nDo you wish to answer the call now?", caller), "New incoming call", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dlg == System.Windows.Forms.DialogResult.Yes)
bcc.Answer();
}
}
I've tested your code with the additions of mine and it worked fine. Should you be having any exceptions when answering or during the initialization, let me know.
Related
I scaling the app from single user to multiple users.
each user have a group of IP cameras, when he click on a button the APP connecting to his cameras
and snapping images. the SDK has a delegate for image callback and you have to wait for all the images and show it Immediately in gallery.
for one user i was used AutoResetEvent wait= new AutoResetEvent(false);.
but now we have more than one user and in parallel mode the first user's job done reset the event
and the second user stuck.
Any ideas how I can to separate the waiting ????
Button Event
protected void btn_capture_Click(object sender, EventArgs e)
{
//db job for each user
.
.
AutoResetEvent wait4all = new AutoResetEvent(false);
Cams cams = new Cams( cameraList, businessPath, cropsList);
cams.SetAllCallBack();
cams.CaptureAll(ref wait4all, ref imgUrl, ref plate, ref plateDetails);
//wait here when callback triggered as a number of snapping
wait4all.WaitOne();
}
Init func
public void SetAllCallBack()
{
//set disconnect callback.
disConnect = new fDisConnectCallBack(DisConnect);
try
{
bool res = NETClient.Init(disConnect, IntPtr.Zero, null); //init NetClient.
if (res == false)
{
// LogsWriter.LogWriter("exception", new string[] { "", String.Format("{0:d/M/yyyy HH:mm:ss}", DateTime.Now.ToUniversalTime()), " NETClient init failed!" });
}
}
catch (Exception ex)
{
// LogsWriter.LogWriter("exception", new string[] { "", String.Format("{0:d/M/yyyy HH:mm:ss}", DateTime.Now.ToUniversalTime()), "Reconnect callback function - " + ((NETClientExcetion)ex).Message.ToString() });
}
m_RealDataCallBack = new fRealDataCallBackEx(RealDataCallBack); //instance realdata callback.
m_RealPlayDisConnectCallBack = new fRealPlayDisConnectCallBack(RealPlayDisConnectCallBack); //instance realplay disconnect callback.
m_ReConnectCallBack = new fHaveReConnectCallBack(ReConnectCallBack); //instance reconnect callback.
m_SnapRevCallBack = new fSnapRevCallBack(SnapRevCallBack); // instance capture callback.
NETClient.SetAutoReconnect(m_ReConnectCallBack, IntPtr.Zero); //set reconnect callback.
}
Cam capture function
[MethodImpl(MethodImplOptions.Synchronized)]
// Capture all cameras except back
public void CaptureAll(string user, AutoResetEvent wait4allv, SortedDictionary<string, string> imgUrl, PlateDetails plateDetails)
{
log.Info("step3 all-" + user);
this.wait4all = wait4all;// pass the event object to continue work
this.imgesUrls = imgUrl;
plateDetails = this.plateDetails;
try
{
Device dev = null;
foreach (var item in m_DeviceList1)
{
// dev = item.Value;
if (dev == null)
{
// return; //if the stuck is empty
}
if (m_IsSetCaptureCallBack == false)
{
log.Info("call back");
NETClient.SetSnapRevCallBack(m_SnapRevCallBack, IntPtr.Zero);
m_IsSetCaptureCallBack = true;
}
NET_SNAP_PARAMS snap = new NET_SNAP_PARAMS();
snap.Channel = (uint)item.ChannelNum;
snap.Quality = 6;
snap.ImageSize = 2;
snap.mode = 0;
snap.InterSnap = 0;
snap.CmdSerial = m_SnapSerialNum;
bool ret = NETClient.SnapPictureEx(item.LoginID, snap, IntPtr.Zero); //call capture function.
log.Info("snap All - " + user + " " + ret);
if (ret)
{
m_SnapSerialNum++;
}
}
}
catch (Exception ex)
{
if (ex is NETClientExcetion)
{
// LogsWriter.LogWriter("exception", new string[] { "", String.Format("{0:d/M/yyyy HH:mm:ss}", DateTime.Now.ToUniversalTime()), "Capture all function - " + ((NETClientExcetion)ex).Message.ToString() });
}
}
}
Callback func
private void SnapRevCallBack(IntPtr lLoginID, IntPtr pBuf, uint RevLen, uint encodeType, uint CmdSerial, IntPtr dwUser)
{
//save image
.
.
countSnap++;
//check if all images saved
if (countSnap == countInit)
{
log.Info("if count " + businessId + "countSnap " + countSnap + " countInit");
wait4all.Set();
}
}
I have a weird problem with my Connection from C# to my Simatic S7-1200.
I am using LibNoDave to connect and i want to set bits of my SPS with the C#-program.
This is working just fine but it only works 9 times and then the writeBytes(..) function returns -1025 and not 0 as it should and it is not setting the byte anymore.
I then have to restart the application and it will work 9 times again.
This is my LibNoDave class.
static class LibNoDave
{
static libnodave.daveConnection dc;
static bool connection = false;
public static bool CreateConnection()
{
const string IP = "192.168.1.250";
const int Rack = 0;
const int Slot = 0;
libnodave.daveOSserialType fds;
libnodave.daveInterface di;
try
{
fds.rfd = libnodave.openSocket(102, IP);
fds.wfd = fds.rfd;
if (fds.rfd > 0)
{
di = new libnodave.daveInterface(fds, "IF1",
0, libnodave.daveProtoISOTCP,
libnodave.daveSpeed187k);
di.setTimeout(1000000);
int res = di.initAdapter();
if (res == 0)
{
dc = new libnodave.daveConnection(di, 0,
Rack, Slot);
res = dc.connectPLC();
if (res == 0)
{
connection = true;
return true;
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return false;
}
public static void DisconnectPLC()
{
if (connection)
{
dc.disconnectPLC();
libnodave.closeSocket(102);
connection = false;
}
}
public static void WritePLC(int anfangswert, byte wert)
{
if (connection)
{
byte[] buf = new Byte[1];
buf[0] = wert;
int res = dc.writeBytes(libnodave.daveFlags, 0, anfangswert, 1, buf);
MessageBox.Show(res.ToString());
}
}
}
Here I am calling the functions:
private void frmAuslagerung_Load(object sender, EventArgs e)
{
if (!LibNoDave.CreateConnection())
{
finished = true;
this.Close();
}
LibNoDave.WritePLC(1, Byte.Parse(auto.Position.Lade.ToString()));
}
private void frmAuslagerung_FormClosing(object sender, FormClosingEventArgs e)
{
if (!finished)
{
//e.Cancel = true; //Not in code for testing
LibNoDave.DisconnectPLC(); //Only for testing
}
else
LibNoDave.DisconnectPLC();
}
I don´t see any problem in code and I don´t know neither your device nor LibNoDave, but the problem may be related to:
1- Is some receiver an "One single digit" (like 0 to 9) address or container ?
2- The position is being truly reseted as I see in WritePLC function?
3- The "LibNoDave.WritePLC" is incrementing its pointer?
Good luck.
it can be problem closing the connection try with:
public static void DisconnectPLC()
{
if (connection)
{
dc.disconnectPLC();
di.disconnectAdapter();
libnodave.closePort(fds.rfd);
connection = false;
}
}
It's old, but for others to find: in my case it was the declaration of fds and di: they had to be class members and not local to the connect method, or the timeout value was being lost later on.
I have been tasked with writing a custom application in C#.NET to move messages from one queue to another, while keeping all of the context and identity information the same as the original message.
I have tried to decipher the online docs, and have tried every combination of MQC.MQOO_PASS_ALL_CONTEXT and MQOO_SAVE_ALL_CONTEXT in opening queues, and MQC.MQPMO_PASS_ALL_CONTEXT in put calls, to no avail. I have tried setting the MQPutMessageOptions.ContextReference to both the source and the
destination queues, but I continue to get exceptions, either MQRC_CONTEXT_HANDLE_ERROR or MQRC_OPTIONS_ERROR, and I cannot find enough information in the online docs to determine the problem.
I am using runtime version v2.0.50727, Version 7.5.0.1 of the amqmdnet.dll. If anyone knows the correct open and/or put message option settings I need to use, I would appreciate the leg up. Thanks.
***** SECOND UPDATE *****
Here is a very simple gui program to test the classes, and I get the same results:
public partial class frmMain : Form
{
// Simple test - all data hard-coded
string sourceQStr = "PETE.TQ1";
string targetQStr = "PETE.TQ2";
string targetMsgIdStr = "414D5120514D4942584330352020202028ED0155202EB302";
string connName = "localhost";
string connPort = "1414";
MQQueueManager sourceManager;
MQQueueManager targetManager;
MQQueue sourceQueue;
MQQueue targetQueue;
MQMessage msg = new MQMessage();
MQGetMessageOptions gmo = new MQGetMessageOptions();
MQPutMessageOptions pmo = new MQPutMessageOptions();
public frmMain()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void txtPassword_Validating(object sender, CancelEventArgs e)
{
if (txtPassword.Text.Trim() != string.Empty)
{
if (LoginUser())
btnTest.Focus();
else
txtUserId.Focus();
}
}
public void LogoutUser()
{
txtUserId.Text = "";
txtPassword.Text = "";
btnTest.Enabled = false;
}
public bool LoginUser()
{
bool OK = ValidateUserAndPassword();
if (OK)
{
btnTest.Enabled = true;
}
else LogoutUser();
return OK;
}
private bool ValidateUserAndPassword()
{
if ((txtUserId.Text.Trim() == string.Empty) || (txtPassword.Text.Trim() == string.Empty))
return false;
try
{
bool _passwordValid = false;
ContextOptions options = ContextOptions.SimpleBind | ContextOptions.SecureSocketLayer;
using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, "UP"))
{
_passwordValid = pc.ValidateCredentials
(txtUserId.Text.Trim(), txtPassword.Text.Trim(), options);
}
if (!_passwordValid)
MessageBox.Show("Invalid username / password", "Invalid",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return _passwordValid;
}
catch (PrincipalServerDownException pex)
{
string msg = pex.Message.Insert(pex.Message.IndexOf("server"), "Authentication ");
MessageBox.Show(msg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
private void btnTest_Click(object sender, EventArgs e)
{
try
{
SetupObjects();
sourceQueue.Get(msg, gmo);
targetQueue.Put(msg, pmo);
sourceManager.Commit();
targetManager.Commit();
MessageBox.Show("Test appears successful - verify with MQ Explorer","OK",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
catch (Exception ex) // MQRC_CONTEXT_HANDLE_ERROR is always thrown
{
MessageBox.Show("Error: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
sourceManager.Backout();
targetManager.Backout();
}
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
/************************************** Utiility methods *****************************************/
private void SetupObjects()
{
// set up objects
string connectName = connName + "(" + connPort + ")";
int ConnOptions = MQC.MQCNO_HANDLE_SHARE_BLOCK;
sourceManager = new MQQueueManager("", ConnOptions, "CHANNEL1", connectName);
targetManager = new MQQueueManager("", ConnOptions, "CHANNEL1", connectName);
MQEnvironment.UserId = txtUserId.Text.Trim();
MQEnvironment.Password = txtPassword.Text.Trim();
int options =
MQC.MQOO_INPUT_SHARED + MQC.MQOO_FAIL_IF_QUIESCING +
MQC.MQOO_SAVE_ALL_CONTEXT + MQC.MQOO_INQUIRE;
sourceQueue = sourceManager.AccessQueue
(sourceQStr, options, sourceManager.Name, null, txtUserId.Text.Trim());
options =
MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING +
MQC.MQOO_PASS_ALL_CONTEXT + MQC.MQOO_INQUIRE;
targetQueue = targetManager.AccessQueue
(targetQStr, options, targetManager.Name, null, txtUserId.Text.Trim());
int i = 0;
try
{
i = sourceQueue.CurrentDepth;
}
catch (Exception ex)
{
MessageBox.Show("Exception: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
gmo.Options = MQC.MQGMO_COMPLETE_MSG + MQC.MQGMO_FAIL_IF_QUIESCING +
MQC.MQGMO_NO_WAIT + MQC.MQGMO_SYNCPOINT;
pmo.Options = MQC.MQPMO_PASS_ALL_CONTEXT +
MQC.MQPMO_SYNCPOINT + MQC.MQPMO_SYNC_RESPONSE;
pmo.ContextReference = sourceQueue;
msg.MessageId = StringToByteArray(targetMsgIdStr);
}
public byte[] StringToByteArray(String hex)
{
hex = hex.Trim();
int NumberChars = hex.Length;
if ((NumberChars % 2) != 0)
{
hex = "0" + hex;
NumberChars++;
}
byte[] bytes = new byte[NumberChars / 2];
for (int i = 0; i < NumberChars; i += 2)
bytes[i / 2] = StringToByte(hex.Substring(i, 2));
return bytes;
}
public byte StringToByte(string hexDigits)
{
if (hexDigits.Length != 2)
return 0;
int high = hexValue(hexDigits[0]);
int low = hexValue(hexDigits[1]);
return (byte)(((high << 4) & 240) | (low & 15));
}
public int hexValue(char c)
{
int retval = 0;
if (c > '9')
retval = ((int)c) - 55;
else
retval = ((int)c) - 48;
return retval;
}
}
The GUI simply prompts for a username and password (which are validated with an LDAP call) and then enables a "test" button which runs the btnTest_Click method. I still get the same exception (MQRC_CONTEXT_HANDLE_ERROR) even thought I have used all the open and get message options specified.
Adding to Morag's answer, here is snippet for moving messages with context. I am also using a SYNC_POINT to ensure the message is removed from source queue after the message is successfully put to the destination queue.
/// <summary>
/// Moves messages from one queue to another with context
/// </summary>
public void moveMessagesWithContext()
{
MQQueueManager qmSource = null;
MQQueueManager qmDestination = null;
MQQueue qSource = null;
MQQueue qDestination = null;
Hashtable htSource = null;
Hashtable htDestination = null;
try
{
htSource = new Hashtable();
htDestination = new Hashtable();
htSource.Add(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_MANAGED);
htSource.Add(MQC.HOST_NAME_PROPERTY, "localhost");
htSource.Add(MQC.PORT_PROPERTY, 2020);
htSource.Add(MQC.CHANNEL_PROPERTY, "A_QM_SVRCONN");
qmSource = new MQQueueManager("A_QM", htSource);
qSource = qmSource.AccessQueue("Q_SOURCE", MQC.MQOO_INPUT_AS_Q_DEF | MQC.MQOO_FAIL_IF_QUIESCING | MQC.MQOO_SAVE_ALL_CONTEXT);
htDestination.Add(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_MANAGED);
htDestination.Add(MQC.HOST_NAME_PROPERTY, "localhost");
htDestination.Add(MQC.PORT_PROPERTY, 3030);
htDestination.Add(MQC.CHANNEL_PROPERTY, "B_QM_SVRCONN");
qmDestination = new MQQueueManager("B_QM", htDestination);
qDestination = qmDestination.AccessQueue("Q_DESTINATION", MQC.MQOO_OUTPUT | MQC.MQOO_FAIL_IF_QUIESCING | MQC.MQOO_PASS_ALL_CONTEXT);
MQMessage msgSource = new MQMessage();
MQGetMessageOptions gmo = new MQGetMessageOptions();
gmo.Options |= MQC.MQGMO_SYNCPOINT;
qSource.Get(msgSource,gmo);
if (msgSource != null)
{
MQMessage msgDestination = new MQMessage();
MQPutMessageOptions pmo = new MQPutMessageOptions();
pmo.ContextReference = qSource;
qDestination.Put(msgSource, pmo);
qmSource.Commit();
}
}
catch (MQException mqEx)
{
qmSource.Backout();
Console.WriteLine(mqEx);
}
catch (Exception otherEx)
{
Console.WriteLine(otherEx);
}
}
The correct options to use to move messages whilst retaining all the context information within them are as follows.
When opening the queue to get messages from, use MQOO_SAVE_ALL_CONTEXT. The context will be saved at get time in the object handle that represents this opened queue.
When opening the queue to put messages to, use MQOO_PASS_ALL_CONTEXT. Then when putting the message, use MQPMO_PASS_ALL_CONTEXT and provide the object handle that contains the context.
Receiving return code MQRC_CONTEXT_HANDLE_ERROR means that you didn't use MQOO_SAVE_ALL_CONTEXT on the first open but then tried to use the object handle on the put. It could also mean that it's just not a correct reference to the first queue. In the MQ API it's an object handle (MQHOBJ) whereas in .Net it's the MQQueue (see MQPutMessageOptions .NET class).
Receiving return code MQRC_OPTIONS_ERROR means that you mixed and matched the options too much.
I have successfully integrated IMAPI2 using Interop.cs into my application. I can burn CD/DVDs without any problem. However, the event handler for MsftDiscFormat2Data update does not work, so I can't get my progress bar moving.
Here is the code I found at codeproject website:
private void backgroundBurnWorker_DoWork(object sender, DoWorkEventArgs e)
{
MsftDiscRecorder2 discRecorder = null;
MsftDiscFormat2Data discFormatData = null;
try
{
//
// Create and initialize the IDiscRecorder2 object
//
discRecorder = new MsftDiscRecorder2();
var burnData = (BurnData)e.Argument;
discRecorder.InitializeDiscRecorder(burnData.uniqueRecorderId);
//
// Create and initialize the IDiscFormat2Data
//
discFormatData = new MsftDiscFormat2Data
{
Recorder = discRecorder,
ClientName = ClientName,
ForceMediaToBeClosed = _closeMedia
};
//
// Set the verification level
//
var burnVerification = (IBurnVerification)discFormatData;
burnVerification.BurnVerificationLevel = _verificationLevel;
//
// Check if media is blank, (for RW media)
//
object[] multisessionInterfaces = null;
if (!discFormatData.MediaHeuristicallyBlank)
{
multisessionInterfaces = discFormatData.MultisessionInterfaces;
}
//
// Create the file system
//
IStream fileSystem;
if (!CreateMediaFileSystem(discRecorder, multisessionInterfaces, out fileSystem))
{
e.Result = -1;
return;
}
//
// add the Update event handler
//
discFormatData.Update += discFormatData_Update;
//
// Write the data here
//
try
{
discFormatData.Write(fileSystem);
e.Result = 0;
}
catch (COMException ex)
{
e.Result = ex.ErrorCode;
MessageBox.Show(ex.Message, "IDiscFormat2Data.Write failed",
MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
finally
{
if (fileSystem != null)
{
Marshal.FinalReleaseComObject(fileSystem);
}
}
//
// remove the Update event handler
//
discFormatData.Update -= discFormatData_Update;
if (_ejectMedia)
{
discRecorder.EjectMedia();
}
}
catch (COMException exception)
{
//
// If anything happens during the format, show the message
//
MessageBox.Show(exception.Message);
e.Result = exception.ErrorCode;
}
finally
{
if (discRecorder != null)
{
Marshal.ReleaseComObject(discRecorder);
}
if (discFormatData != null)
{
Marshal.ReleaseComObject(discFormatData);
}
}
}
void discFormatData_Update([In, MarshalAs(UnmanagedType.IDispatch)] object sender, [In, MarshalAs(UnmanagedType.IDispatch)] object progress)
{
//
// Check if we've cancelled
//
if (backgroundBurnWorker.CancellationPending)
{
var format2Data = (IDiscFormat2Data)sender;
format2Data.CancelWrite();
return;
}
var eventArgs = (IDiscFormat2DataEventArgs)progress;
_burnData.task = BURN_MEDIA_TASK.BURN_MEDIA_TASK_WRITING;
// IDiscFormat2DataEventArgs Interface
_burnData.elapsedTime = eventArgs.ElapsedTime;
_burnData.remainingTime = eventArgs.RemainingTime;
_burnData.totalTime = eventArgs.TotalTime;
// IWriteEngine2EventArgs Interface
_burnData.currentAction = eventArgs.CurrentAction;
_burnData.startLba = eventArgs.StartLba;
_burnData.sectorCount = eventArgs.SectorCount;
_burnData.lastReadLba = eventArgs.LastReadLba;
_burnData.lastWrittenLba = eventArgs.LastWrittenLba;
_burnData.totalSystemBuffer = eventArgs.TotalSystemBuffer;
_burnData.usedSystemBuffer = eventArgs.UsedSystemBuffer;
_burnData.freeSystemBuffer = eventArgs.FreeSystemBuffer;
//
// Report back to the UI
//
backgroundBurnWorker.ReportProgress(0, _burnData);
}
Unfortunately, the update handler gets never called. I looked up everywhere on internet but could not find a solution. I saw some people with same problem but no one was able to answer.
The original code from Eric, http://www.codeproject.com/KB/miscctrl/imapi2.aspx?msg=2695517,
does work for some reason.
Can someone please help me?
I know, it's too late, but I've got the same problem with update callbacks.
Open Project->Properties->Application->Assembly Information and tick "Make Assembly COM-visible".
For my network based project I need to lock some codes to prevent simultaneous access.
This is my code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utility;
using DataBaseConnection;
using System.Net.Sockets;
using System.Data;
using System.IO;
namespace SunHavenClasses
{
public delegate void CtatReceiveDelegate(string message);
public class ServerHandlerClass
{
public event CtatReceiveDelegate OnChatDataReceive;
private Settings settings;
private DBCon con;
private Utility.Network.Server server;
private Dictionary<string, Socket> UsersOnline;
private Dictionary<string, int> unAuthenticatedIps;
private string pass = "logisoftlogicielbarundipankar";
public ServerHandlerClass(Settings s)
{
settings = s;
con = s.GetConnection();
server = new Utility.Network.Server(7777);
server.ClientConnectEventArise += OnUserConnect;//.OnClientConnect(OnUserConnect);
server.ClientDataReceiveEventArise += OnUsersDataReceive;
server.ClientDataSendEventArise += OnDataSendToUser;
server.ClientDisconnectEventArise += OnUserDisconnect;
server.OnBlockUser += OnUserBlocked;
UsersOnline = new Dictionary<string, Socket>();
unAuthenticatedIps = new Dictionary<string, int>();
}
private void OnUserConnect(Utility.Network.ServerEventArguments e)
{
Stream data = Utility.Serializing.Serialize(settings);
data = ZipNEncrypt.Zip(new string[] { "settings" }, new Stream[] { data }, pass);
server.Send(data, e.ClientSocket);
//MessageBox.Show(e.ClientSocket.RemoteEndPoint.ToString() + " is Connected!!");
}
private void OnUsersDataReceive(Utility.Network.ServerEventArguments e)
{
Dictionary<string, System.IO.Stream> data = ZipNEncrypt.Unzip(e.Data, pass);
User user;
try
{
user = (User)Serializing.Deserialize(data["user"]);
if (!UsersOnline.ContainsKey(user.GetUserId()))
{
server.BlockIp(e.ClientSocket);
return;
}
data.Remove("user");
}
catch (Exception)
{
bool passed = true;
foreach (string key in data.Keys)
{
if (key.Equals("LoggedIn")) break;
string[] str = key.Split('_');
if (str[0].Equals("GetData"))
{
string strr = (string)Serializing.Deserialize(data[key]);
if (strr.Contains("Users"))
{
string ip = e.ClientSocket.RemoteEndPoint.ToString().Split(':')[0];
/*CHANGE 1.2.10 00:14*/
lock (unAuthenticatedIps)
{
if (!unAuthenticatedIps.ContainsKey(ip))
{
unAuthenticatedIps.Add(ip, 1);
}
else unAuthenticatedIps[ip] += 1;
if (unAuthenticatedIps[ip] >= 11) passed = false;
}
/*CHANGE 1.2.10 00:14*/
break;
}
else passed = false;//server.AddBlockedIp(ip);
}
else passed = false;
}
if (!passed)
{
server.BlockIp(e.ClientSocket);
}
}
foreach (string key in data.Keys)
{
if (key.Equals("LoggedIn"))
{
try
{
User u = (User)Serializing.Deserialize(data["LoggedIn"]);
if (!UsersOnline.ContainsKey(u.GetUserId()))
{
if (User.ValidateUser(u.GetUserId(), u.GetPassword(), con))
{
/*CHANGE 1.2.10 00:14*/
lock (UsersOnline)
{
UsersOnline.Add(u.GetUserId(), e.ClientSocket);
string ip = e.ClientSocket.RemoteEndPoint.ToString().Split(':')[0];
Utility.Log.Write("UserLog.log", u.GetUserId() +
" Logged In From Ip " + ip);
}
/*CHANGE 1.2.10 00:14*/
}
else
{
server.BlockIp(e.ClientSocket);
return;
}
}
else
{
Stream tmpStream = Serializing.Serialize("Same User");
tmpStream = ZipNEncrypt.Zip(new string[] { key + "ERROR_SameUser" },
new Stream[] { tmpStream }, pass);
server.Send(tmpStream, e.ClientSocket);
return;
}
}
catch (Exception) { }
return;
}
else if (key.Equals("chat"))
{
string ip = e.ClientSocket.RemoteEndPoint.ToString().Split(':')[0];
string message = ip + " : "+ (string)Serializing.Deserialize(data[key]);
OnChatDataReceive(message);
return;
}
string[] str = key.Split('_');
Stream dataStream = null;
object obj = null;
try
{
if (str[0].StartsWith("Get"))
{
if (str[0].Equals("GetData"))
{
string query = (string)Serializing.Deserialize(data[key]);
obj = con.GetData(query);
}
else if (str[0].Equals("GetColumn"))
{
string query = (string)Serializing.Deserialize(data[key]);
string[] tmp = query.Split('%');
obj = con.GetColumn(tmp[0], tmp[1]);
}
else if (str[0].Equals("GetColumnDistrinctValue"))
{
string query = (string)Serializing.Deserialize(data[key]);
string[] tmp = query.Split('%');
obj = con.GetColumnDistrinctValue(tmp[0], tmp[1]);
}
}
else
{
lock (this)
{
if (str[0].Equals("ExecuteUpdate"))
{
if (str[1].Equals("Query"))
{
Query query = (Query)Serializing.Deserialize(data[key]);
obj = con.ExecuteUpdate(query);
}
else if (str[1].Equals("String"))
{
string query = (string)Serializing.Deserialize(data[key]);
obj = con.ExecuteUpdate(query);
}
}
else if (str[0].Equals("ExecuteBatchUpdate"))
{
if (str[1].Equals("Query"))
{
Query[] query = (Query[])Serializing.Deserialize(data[key]);
obj = con.ExecuteBatchUpdate(query);
}
else if (str[1].Equals("String"))
{
string[] query = (string[])Serializing.Deserialize(data[key]);
obj = con.ExecuteBatchUpdate(query);
}
}
else if (str[0].Equals("ExecutrInsert"))
{
Query query = (Query)Serializing.Deserialize(data[key]);
obj = con.ExecutrInsert(query);
}
}
}
dataStream = Serializing.Serialize(obj);
dataStream = ZipNEncrypt.Zip(new string[] { key },
new Stream[] { dataStream }, pass);
}
catch (Exception ex)
{
dataStream = Serializing.Serialize(ex.Message);
dataStream = ZipNEncrypt.Zip(new string[] { key + "_ERROR" },
new Stream[] { dataStream }, pass);
}
server.Send(dataStream, e.ClientSocket);
}
}
private void OnDataSendToUser(Utility.Network.ServerEventArguments e)
{
}
private void OnUserDisconnect(Utility.Network.ServerEventArguments e)
{
//System.Windows.Forms.MessageBox.Show("Disconnected");
string ip = e.ClientSocket.RemoteEndPoint.ToString().Split(':')[0];
/*CHANGE 1.2.10 00:14*/
lock (unAuthenticatedIps)
{
if (unAuthenticatedIps.ContainsKey(ip))
unAuthenticatedIps.Remove(ip);
}
lock (UsersOnline)
{
foreach (string key in UsersOnline.Keys)
if (UsersOnline[key].Equals(e.ClientSocket))
{
Utility.Log.Write("UserLog.log", key + " Logged Out From Ip " + ip);
UsersOnline.Remove(key);
break;
}
}
/*CHANGE 1.2.10 00:14*/
}
private void OnUserBlocked(Utility.Network.ServerEventArguments e)
{
string ip = e.ClientSocket.RemoteEndPoint.ToString().Split(':')[0];
Utility.Log.Write("UserLog.log", "Blocked For Illegal Access From Ip " + ip);
}
public void Send(Stream dataStream)
{
foreach (string key in UsersOnline.Keys)
{
try
{
server.Send(dataStream, UsersOnline[key]);
}
catch (Exception) { }
}
}
public void Send(Stream dataStream, Socket client)
{
try
{
server.Send(dataStream, client);
}
catch (Exception) { }
}
/*changed*/
public bool AddUser(string userId, Socket socket)
{
if (UsersOnline.ContainsKey(userId)) return false;
UsersOnline.Add(userId, null);
return true;
}
public void RemoveUser(string userId)
{
if (!UsersOnline.ContainsKey(userId) || UsersOnline[userId] != null) return;
UsersOnline.Remove(userId);
}
}
}
Now I am not sure that I am using lock correctly.Please Give me some advice.
Thanks.
I'm guessing you read a lot more than you write? If so, a ReaderWriterLockSlim may be more appropriate to reduce blocking (take read when you just want to check for the key, and write to manipulate the data).
By that I mean you could do double-checked locking with a read at first, then if it fails take a write lock, check again and add if necessary.
Also - the lock(this) is generally frowned upon; having a separate lock object is preferred.
Note that to be effective, all access must respect the lock; there are some places where UsersOnline is locked, and some places where it is accessed without a lock, for example; those second cases may explode in a gooey mess.
For example:
if (!UsersOnline.ContainsKey(u.GetUserId()))
{
if (User.ValidateUser(u.GetUserId(), u.GetPassword(), con))
{
/*CHANGE 1.2.10 00:14*/
lock (UsersOnline)
{
UsersOnline.Add(u.GetUserId(), e.ClientSocket);
In the above, if it is possible that two threads are looking at UsersOnline, then you've already failed by attempting ContainsKey without the lock. If another thread is mutating the state when you do this.... boom.
First of all, you code isn't thread safe at all. In your code you locks only modifying operations (Remove, Add), but also you should lock all access to shared fields. Actually this code not thread safe at all. I think that in this case ReaderWriterLockSlim - would be the best choise.
Second. lock(this) - very bed idea. You should use special objects for this.
Finally, I think your code is messy and hard to understand. Maybe your class solve many different tasks. Maybe you should extract some logic to separate classes (for example create guarded dictionaries as separate classes) or something else.
Sample of using ReaderWriterLockSlim:
someSharedResource;
someSharedResourceRWLock = new ReaderWriterLockSlim();
Some reading code:
try
{
someSharedResourceRWLock.EnterReadLock();
//access to someSharedResource for reading
}
finally
{
someSharedResourceRWLock.ExitReadLock();
}
Some writing code:
try
{
someSharedResourceRWLock.EnterWriteLock();
//access to someSharedResource for modifications
}
finally
{
someSharedResourceRWLock.ExitWriteLock();
}
You are using it correctly some of the time. unAuthenticatedIps is being protected correctly, but UsersOnline is not. Let's consider two parallel threads passing through your code:
Thread A Thread B
-------- --------
if (!UsersOnline.ContainsKey(u.GetUserId()) Statement's true Statement's true
{
lock (UsersOnline) Get lock Block
{
UsersOnline.Add UsersOnline.Add
} Release Lock
} Get lock
UsersOnline.Add
Release lock
Notice that both threads A and B modify the UsersOnline dictionary. This structure would properly protect that object:
if (!UsersOnline.ContainsKey(u.GetUserId()))
{
if (User.ValidateUser(u.GetUserId(), u.GetPassword(), con))
{
lock (UsersOnline)
{
// Note the additional check in case another thread
// added this already
if (!UsersOnline.ContainsKey(u.GetUserId())
{
UsersOnline.Add(u.GetUserId(), e.ClientSocket);
// ...
}
}
}
else
{
server.BlockIp(e.ClientSocket);
return;
}
}
As far as the last lock goes (lock (this)), I don't yet see why you would need it. str and obj are both local variables, so you shouldn't need to worry about them being modified by separate threads. And, as other have stated, locking this is not recommended.
The advice given so far has been great, but I have a design suggestion you may want to consider. Replace this:
private Dictionary<string, Socket> UsersOnline;
with a custom Thread Safe Dictionary
private ThreadSafeDictionary<string, Socket> UsersOnline
If its a fit for your needs, it would be a nice way to separate the business logic from the threading logic.