Good afternoon. I have a source program that monitors the status of the printer (start printing, stop, etc.). Here is the code that displays information about the print:
MethodInvoker invoker = () =>
{
lbSpoolChanges.Items.Add(e.JobID + " - " + e.JobName + " - " + e.JobStatus);
};
if (lbSpoolChanges.InvokeRequired)
{
Invoke(invoker);
}
else
{
invoker();
}`
You can also call the property e.JobInfo.NumberOfPagesPrinted and line will be a
lbSpoolChanges.Items.Add(e.JobID + " - " + e.JobName + " - " + e.JobStatus + " - " + e.JobInfo.NumberOfPagesPrinted);
but in debug error pops up "The calling thread can not get access to this object as the owner of this object is another thread.." Tell me where you want to call this property. Source included. And can somebody tell how to do so automatically control all the printers (eg 4), and not set in the program. Thanks in advance.
Does it work if you write the invoker as an Action like this, and pass the delegate parameters using BeginInvoke?
Action<string> invoker = (x) =>
{
lbSpoolChanges.Items.Add(x);
};
if (this.InvokeRequired)
{
this.BeginInvoke(invoker, e.JobID + " - " + e.JobName + " - " + e.JobStatus);
}
else
{
invoker(e.JobID + " - " + e.JobName + " - " + e.JobStatus);
}
Related
I have to do if user's browser compatibility is on then need to show message to user that your browser's compatibility is on.
I have searched this a lot on google but yet not found a proper answer.
I have tried below code but HttpContext.Current.Request.UserAgent always contains MSIE 7.0
string isOn = string.Empty;
if (HttpContext.Current.Request.UserAgent.IndexOf("MSIE 7.0") > -1)
{
isOn = "IE8 Compatibility View";`
}
else
{
isOn = "IE8";
}
}
You may try like this
if (Request.Browser.Type.ToUpper().Contains("IE"))
{
if (Request.Browser.MajorVersion < 7)
{
//Show the message here
}
...
}
else if (Request.Browser.Type.Contains("Firefox"))
{
//code to show message
}
else if (Request.Browser.Type.Contains("Chrome"))
{
//code to show message
}
Also check this MSDN which has its own way of detecting the browser
Query the Browser property, which contains an HttpBrowserCapabilities
object. This object gets information from the browser or client device
during an HTTP request, telling your application the type and level of
support the browser or client device offers. The object in turn
exposes information about browser capabilities using strongly typed
properties and a generic name-value dictionary.
private void Button1_Click(object sender, System.EventArgs e)
{
System.Web.HttpBrowserCapabilities browser = Request.Browser;
string s = "Browser Capabilities\n"
+ "Type = " + browser.Type + "\n"
+ "Name = " + browser.Browser + "\n"
+ "Version = " + browser.Version + "\n"
+ "Major Version = " + browser.MajorVersion + "\n"
+ "Minor Version = " + browser.MinorVersion + "\n"
+ "Platform = " + browser.Platform + "\n"
+ "Is Beta = " + browser.Beta + "\n"
+ "Is Crawler = " + browser.Crawler + "\n"
+ "Is AOL = " + browser.AOL + "\n"
+ "Is Win16 = " + browser.Win16 + "\n"
+ "Is Win32 = " + browser.Win32 + "\n"
+ "Supports Frames = " + browser.Frames + "\n"
+ "Supports Tables = " + browser.Tables + "\n"
+ "Supports Cookies = " + browser.Cookies + "\n"
+ "Supports VBScript = " + browser.VBScript + "\n"
+ "Supports JavaScript = " +
browser.EcmaScriptVersion.ToString() + "\n"
+ "Supports Java Applets = " + browser.JavaApplets + "\n"
+ "Supports ActiveX Controls = " + browser.ActiveXControls
+ "\n"
+ "Supports JavaScript Version = " +
browser["JavaScriptVersion"] + "\n";
TextBox1.Text = s;
}
I am using Selenium to manipulate TestRail test cases. I am using this because the TestRail API is sparse and doesn't allow you to upload your log results.
I have everything set up and it runs with out problems until I reach the very end. When selenium hits enter on the Add Test Result (submit button) it tells me an error has occurred. The very odd thing is that if I step through it will submit with no errors.
To try and fix this I added a sleep for x seconds. I have tried sleeping for 2-20 seconds.
Here is a snippet of code that I am running.
try
{
OpenQA.Selenium.IWebElement element = driver.FindElement(By.LinkText(testName));
element.Click();
System.Threading.Thread.Sleep(2000);
driver.FindElement(By.Id("addResult")).Click();
System.Threading.Thread.Sleep(2000);
driver.FindElement(By.LinkText("Add Attachment")).Click();
System.Threading.Thread.Sleep(2000);
driver.FindElement(By.Id("attachment")).SendKeys(testCase.LogLocation);
System.Threading.Thread.Sleep(250);
driver.FindElement(By.XPath("//*[#id=" + "\"" + "attachmentForm" + "\"" + "]/div[2]/button/span[1]")).Click();
System.Threading.Thread.Sleep(2000);
driver.FindElement(By.XPath("//*[#id=" + "\"" + "addResultVersion" + "\"" + "]")).SendKeys(testCase.Version);
System.Threading.Thread.Sleep(250);
driver.FindElement(By.XPath("//*[#id=" + "\"" + "addResultStatus" + "\"" + "]")).SendKeys(testCase.Status.ToString());
System.Threading.Thread.Sleep(250);
driver.FindElement(By.XPath("//*[#id=" + "\"" + "addResultComment" + "\"" + "]")).SendKeys(testCase.Comment);
System.Threading.Thread.Sleep(250);
driver.FindElement(By.XPath("//*[#id=" + "\"" + "addResultElapsed" + "\"" + "]")).SendKeys(testCase.ElapsedTime.ToString());
System.Threading.Thread.Sleep(250);
element = driver.FindElement(By.XPath("//*[#id=" + "\"" + "addResultAssignTo" + "\"" + "]"));
SelectElement clickThis = new SelectElement(element);
clickThis.SelectByText("Shawn Rioux");
driver.FindElement(By.XPath("//*[#id=" + "\"" + "addResultDefects" + "\"" + "]")).SendKeys(testCase.Defects);
System.Threading.Thread.Sleep(10000);
element = driver.FindElement(By.XPath("//*[#id=" + "\"" + "addResultSubmit" + "\"" + "]"));
System.Threading.Thread.Sleep(1000);
element.SendKeys(Keys.Enter);
//If i set a break point on this line and remove the sleep it works some how :(
Console.WriteLine("Page submitted");
}
catch (Exception e)
{
Console.WriteLine("Failed at an element " + e.Message);
}
The weirdest part is if I set a break point on the line after element.SendKeys and remove the sleep everything works like a charm and adds the result perfect. At this point I am fairly certain it isn't a race condition since if I remove the sleep and set a break point AFTER the send keys it works, but I could be wrong. Any insight would be helpful
I have also looked at C# and breakpoints - do we have any magician in here? and the WebDriverWait method in there didn't work for me unless I set a breakpoint.
It's a continue of
I've narrow down the problems and now when I'm running on debug mode I can see that for some reason not all the Foreach iterations are completed, I have a debug every iteration, when the program stops I can see that I'm iterating on a 4 items list, but the loop is executed only 3 times which causing the program to throw a NullrefrenceException - because not all objects has been created.
This is my Loop
foreach (var system in allSystems.ToList())
{
Debug.WriteLine("inside Foreach in system: " + system.Name + " interface " + system.Interfaces.Count + " structs " + system.Structs.Count);
if (nodes == null)
Debug.WriteLine("Node is Null");
try
{
Debug.WriteLine("Before add node");
lock (networkLocker)
{
Debug.WriteLine("trying to add: " + system.Name + " " + system.InputNum + " " + system.OutputNum + " " + system.Interfaces.Count + " " + system.Enums.Count + " " + system.Structs.Count);
nodes.Add(CreateNode(system.Name, new Point(width, height), false, system.InputNum, system.OutputNum, system.Interfaces, system.Enums, system.Structs, update));
}
Debug.WriteLine("Success adding new node to list");
}
catch (Exception ex)
{
logger.addMessage("Error in adding new node to list " + ex.Message + " Inner: " + ex.InnerException.Message);
Debug.WriteLine("Error in adding new node to list " + ex.Message + " Inner: " + ex.InnerException.Message);
}
width += 150;
if (width >= 700)
{
width = 0;
height += 100;
}
}
The allSystems contains 4 elemnts (I can see that on debug) but the Debug.WriteLine("inside Foreach in system: " + system.Name + " interface " + system.Interfaces.Count + " structs " + system.Structs.Count); is only printed 3 times, and the list nodes contains indeed only 3 elements.
Any thoughts?
So to explain what i'm expiriancing i added 2 pictures from the debugger:
this is the list that i'm iterating on:
this is the nodes list that should add node every iteration:
I think your Debug.WriteLine("inside ...") throws an exception, cause either system.Interfaces or system.Structs is null.
You should go within VS to Debug - Exception and enable all types of exception to find the one that throws the exception. To make it easier to enable or disable all exceptions at once you should maybe take a look at the Exception Breaker, who simply puts this behaviour into one button.
I tried to update a page in OneNote with the Microsoft reference :
http://msdn.microsoft.com/en-us/library/office/jj680118.aspx
Here's my problem. When i tried to update my page with the correct ID, it throwed me an error saying : Exception from HRESULT: 0x80042000.
Here is my code :
static void UpdatePageContent()
{
ApplicationClass onApplication = new ApplicationClass();
String strImportXML;
strImportXML = #"<?xml version="+"1.0"+" encoding="+"utf-16"+"?>" +
" <one:Page xmlns:one="+"http://schemas.microsoft.com/office/onenote/12/2004/onenote\""+"" +
"ID=\"{5BE09697-903A-45DD-88D4-8AD301A3D91F}{1}{B0}\">" +
" <one:PageSettings RTL=\"false\" color=\"automatic\">" +
" <one:PageSize>" +
" <one:Automatic/>" +
" </one:PageSize>" +
" <one:RuleLines visible=\"false\"/>" +
" </one:PageSettings>" +
" <one:Title style=\"font-family:Calibri;" +
" font-size:17.0pt\" lang=\"en-US\">" +
" <one:OE alignment=\"left\">" +
" <one:T>" +
" <![CDATA[My Sample Page]]>" +
" </one:T>" +
" </one:OE>" +
" </one:Title>" +
" <one:Outline >" +
" <one:Position x=\"120\" y=\"160\"/>" +
" <one:Size width=\"120\" height=\"15\"/>" +
" <one:OEChildren>" +
" <one:OE alignment=\"left\">" +
" <one:T>" +
" <![CDATA[Sample Text]]>" +
" </one:T>" +
" </one:OE>" +
" </one:OEChildren>" +
" </one:Outline>" +
" </one:Page>";
// Update page content
try
{
onApplication.UpdatePageContent(strImportXML, System.DateTime.MinValue);
}
catch (COMException e)
{
Console.WriteLine("Error Message : " + e.Message);
}
}
I really don't know how to solve this.
Your XML is not OneNote friendly.
Here's a list of error codes:
http://msdn.microsoft.com/en-us/library/office/jj680117.aspx
You can get rid of the first line, as #Sebastian has stated it's malformed anyway and my experience is that OneNote doesn't need it.
Also, remember that you don't need to send the entire page. You just need to send the page's objId and any updated objects. So one outline needs adding then this should also work:
"<one:Page xmlns:one=\"http://schemas.microsoft.com/office/onenote/12/2004/onenote\" +
"ID=\"{5BE09697-903A-45DD-88D4-8AD301A3D91F}{1}{B0}\">" +
" <one:Outline >" +
" <one:Position x=\"120\" y=\"160\"/>" +
" <one:Size width=\"120\" height=\"15\"/>" +
" <one:OEChildren>" +
" <one:OE alignment=\"left\">" +
" <one:T>" +
" <![CDATA[New Text]]>" +
" </one:T>" +
" </one:OE>" +
" </one:OEChildren>" +
" </one:Outline>";
Just this new outline will get added.
If you still get problems (it doesn't complain but the content doesn't update) then check the extra parameters for UpdatePageContent, certainly in the 2013 API one can send a last modified date to check and also there's a parameter to force a local over-write.
There are some issues in the strImportXML string, which causes the update to fail.
Adjust
#"<?xml version="+"1.0"+" encoding="+"utf-16"+"?>" to "<?xml version=\"" + "1.0" + "\" encoding=\"" + "utf-16" + "\"?>"
Add an empty space before the page ID attribute ( + " " + "ID...
instead of + "" + "ID)
Make sure that the page ID is found/ present in your onApplication
hierarchy
Make sure that you reference the COM library with the matching
namespace defined in the one:Page element (e.g. Office 2013/ 15.0
Object Library has another namespace)
I am trying to create a simple appplication that can read from a text file and write to the text file. I have a main form which is shown below, also i have a secondary form also shown below; there are also getter and setter classes for Customers, accounts and Transactions. What i would like to do is search for a Customer based on any data held, but mostly there account number. How would i get it to return the correct customer with ALL of there information. Linked to this it would be good to use the search as a point for inserting say a new account.
Below is the main form for pritning out the customer and getting the information from the file.
//create account if all ok
if (allInputOK)
{
//create Account
Account temp = new Account(tempAccSortCode, tempAccNumber, tempAccNickName, tempAccDate, tempAccCurBal, tempAccOverDraft, tempNumTrans);
//add to array
Form1.accDetails.Add(temp);
//finish up
MessageBox.Show("Success Account added ");
resetForm();
}
}
foreach (Customer c in bankDetails)
{
lstOutput.Items.Add(" ");
lstOutput.Items.Add(c.getCustomerNumber() + " " + c.getCustomerTitle() + " " + c.getFirstName()
+ " " + c.getInitials() + " " + c.getSurname() + " " + c.getDateOfBirth()
+ " " + c.getHouseNameNumber() + " " + c.getStreetName() + " " + c.getArea()
+ " " + c.getCityTown() + " " + c.getCounty() + " " + c.getPostcode()
+ " " + c.getPassword() + " " + c.getNumberAccounts());
foreach (Account a in c.Accounts)
{
lstOutput.Items.Add("\t" + a.getAccSort() + " " + a.getAccNumber() + " " + a.getAccNick() + " " + a.getAccDate()
+ " " + a.getAccCurBal() + " " + a.getAccOverDraft() + " " + a.getAccNumTrans());
foreach (Transaction t in a.Transactions)
{
lstOutput.Items.Add("\t \t" + t.getDate() + " " + t.getType() + " " + t.getDescription() + " " + t.getAmount()
+ " " + t.getBalAfter());
}
}
}
In the above code for adding an account, where is adds it to the class Account and the arraylist i think is incorrect with the rest of the program on the main form which uses an underlying list to store the customer information and there accounts/transactions.
EDIT: the above code snippet shows the adding of a new account, however it doesnt seem to be working as i need to find the correct customer by searching the customer array, and then inserting it into the correct place. The second snippet of code shows the arraylist with the underlying lists connected to each customer.
Where to begin?
Generally, for querying objects you can use Linq to Objects.
Also, I would recommend against using text files to keep track of things. They are fine at first, but quickly grow unwieldy and slow. On top of that, what if your app crashes before you saved? Look into a "light database" like SQLite (yes, that's one 'L').