Attach Watin to an existing browser always times out - watin

I am trying to attach Watin to an existing instance of Internet Explorer 9. The trouble is no matter what I try it seems to just timeout. The code I am using is:
Settings.AttachToBrowserTimeOut = 240;
Settings.WaitUntilExistsTimeOut = 240;
Settings.WaitForCompleteTimeOut = 240;
Browser.AttachTo<IE>(Find.ByTitle("Google"), 240);
This code is what is in the Watin guide here minus the timeout adjustments I have tried. I have also tried setting the code to run x86 not any cpu but still not working. Any ideas what I am doing wrong.

The method you write in is missing the single threaded ApartmentState. The easiest way to fix this is to add [STAThread] above the method where you're trying to use that code.
For example this:
//[STAThread]
static void Main(string[] args)
{
IE.AttachTo<IE>(Find.ByTitle("Google"), 5);
}
acts like you described. When you uncomment the [STAThread]
[STAThread]
static void Main(string[] args)
{
IE.AttachTo<IE>(Find.ByTitle("Google"), 5);
}
it works as intented.

Related

Trying to automate a process in console application

I am trying to get my console application to simulate dragging and dropping a file, so far I have had no luck.
The system throws a win32 exception stating it cannot find the file, since I know that is not really the problem I was hoping someone could shed some light on what might be causing this behavior.
I suspect it might be DEP. I can drag and drop the file and the process runs as expected, but I need to automate this.
I have created a filewatcher and am right now trying to figure out how to get the code to work, prior to making it a windows service.
But right now I am really stuck on this win32 error.
public class Watcher
{
public static void Main(string[] args)
{
Run();
}
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
public static void Run()
{
string[] args = System.Environment.GetCommandLineArgs();
// If a directory is not specified, exit program.
if (args.Length != 2)
{
// Display the proper way to call the program.
Console.WriteLine("Usage: Watcher.exe (directory)");
return;
}
// Create a new FileSystemWatcher and set its properties.
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = args[1];
/* Watch for changes in LastAccess and LastWrite times, and
the renaming of files or directories. */
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
// Only watch jpg files.
watcher.Filter = "*.jpg";
// Add event handlers.
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
// Begin watching.
watcher.EnableRaisingEvents = true;
// Wait for the user to quit the program.
Console.WriteLine("Press \'q\' to quit the sample.");
while (Console.Read() != 'q') ;
}
// Define the event handlers.
private static void OnChanged(object source, FileSystemEventArgs e)
{
// Specify what is done when a file is changed, created, or deleted.
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
Process.Start(e.FullPath + "c:\\demo\\kr-pano\\mpr.bat");
}
Visit the link to see the full win32 exception details.
Win32Exception
Apparently the issue is being caused because the filesystemwatcher is single threaded, and when I try to launch my new process it stops the old one from running. All I needed to do was stub out a new runpano function and call it from my code, its working now.

Call to String.Concat causing high-cpu not showing in CPU Usage tab

My sample app looks as follows.
class Program
{
static List<XmlNode> memList = new List<XmlNode>();
static void Main(string[] args)
{
Console.WriteLine("Press any key to start");
Console.ReadKey();
CauseHighCPU();
}
static public void CauseHighCPU()
{
string str = string.Empty;
for (int i = 0; i < 100000; i++)
{
str += " Hello World";
}
}
}
I expect string concatenation to cause high cpu. When I profile the application using PerfView, this is very loud and clear.
I am trying to do similar analysis using Visual Studio 2017 Diagnostics Hub. Below is what its CPU usage tab shows.
Its Call-tree view not showing any call to Concat, although, there are some External Code here
This makes me think that it may related to something missing in my configration. As you can see here, Enable Just My Code is unchecked.
Also not sure if its related but here is symbols settings.
Any thouhts what could be wrong that is causing VS not showing root cause of high-cpu usage.
You should not look in the options of Debugging but in the options of Performance Tools and then disable "Just my code":

How watch values doesn't affect the environment

When debugging in IDE, how does the IDE know how to calculate the watch value without changing the environment (writing to file, writing result to DB)?
Your observation cannot be generalized. An IDE typically makes changes during debugging, especially if a property has a side effect.
Visual Studio
The following C# code:
using System;
namespace EvaluateChangesValue
{
class Program
{
static void Main()
{
var program = new Program();
Console.WriteLine(program.Value);
Console.ReadLine();
Console.WriteLine(program.Value);
Console.ReadLine();
}
private int member;
private int Value => member++;
}
}
Set a breakpoint at the first ReadLine(), then add program.Value to the watch window and see how the value gets increased due to the member++ statement.
Eclipse
In Java and Eclipse, it's a bit harder to make the same proof because for these reasons:
In Java it's more clear whether you call a method or access a field.
You need the "Expressions" window, which is not available by default
Re-evaluation needs user interaction
The code is similar to C#:
public class Program {
public static void main(String[] args)
{
Program p = new Program();
System.out.println(p.member);
System.console().readLine();
System.out.println(p.member);
System.console().readLine();
}
private int member;
public int getMember()
{
return member++;
}
}
And the screenshot:

ArgumentOutOfRangeException running HtmlAgilityPack

I'm at my wits end. I keep getting this exception with HtmlAgilityPack whenever I attempt to select nodes using XPath. The problem remains even if I start a brand new solution with just this example (so it's not a problem with my application code interfering somehow. There's nothing wrong with the webpage, or my internet connection or anything like that. I know I've had this working before. I even suspected a corrupted dll somehow and re-downloaded it, but to no avail. Any ideas??
using System;
using HtmlAgilityPack;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var web = new HtmlWeb();
var doc = web.Load("http://www.google.com");
var root = doc.DocumentNode;
var links = root.SelectNodes("//a");
// Error! ArgumentOutOfRangeException: Index was out of range.
// Must be non-negative and less than the size of the collection.
}
}
}
/*
System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
at System.ThrowHelper.ThrowArgumentOutOfRangeException()
at HtmlAgilityPack.HtmlNodeNavigator.MoveToFirstChild()
at MS.Internal.Xml.XPath.XPathDescendantIterator.MoveNext()
at MS.Internal.Xml.XPath.DescendantQuery.Advance()
at MS.Internal.Xml.XPath.XPathSelectionIterator.MoveNext()
at HtmlAgilityPack.HtmlNode.SelectNodes(String xpath)
at ConsoleApplication1.Program.Main(String[] args)
*/
edit:
Well...I'm not sure how it happened or what it did, but I found the .dll installed in a GAC_MSIL folder. Removing that immediately resolved the issue. So, nevermind!

.NETLink Graphics producing PNG instead of EMF

The C# code below should produce an EMF, but viewing the output (in Vim) shows it to be a PNG. Perhaps someone on S.O. knows a good work-around or solution.
MathKernel k = new MathKernel();
k.CaptureGraphics = true;
k.GraphicsFormat = "Metafile";
k.Compute("Show[Graphics[{Thick, Blue, Circle[{#, 0}] & /# Range[4], Black, Dashed, Line[{{0, 0}, {5, 0}}]}]]");
k.Graphics[0].Save("C:\\Temp\\file.emf", System.Drawing.Imaging.ImageFormat.Emf);
So far I'm considering wrapping Show[Graphics...] in ExportString[... , "EMF"] and collecting the result using the MathKernel Result property.
Addendum
The MathKernel.Graphics property[1] is apparently a .Net Graphics method which only handles image files such as bitmaps, not vector graphic based enhanced metafiles.
http://reference.wolfram.com/legacy/v7/NETLink/ref/net/Wolfram.NETLink.MathKernel.Graphics.html
Enhanced metafiles can be transferred through .NETLink one at a time though, in the following manner:
using System;
using System.IO;
using Wolfram.NETLink;
public class Example
{
public static void Main(String[] args)
{
MathKernel k = new MathKernel();
k.Compute("ExportString[Graphics[{Disk[]}], {\"Base64\", \"EMF\"}]");
byte[] decodedBytes = Convert.FromBase64String(k.Result.ToString());
// The transferred EMF can be used or simply written out to file.
File.WriteAllBytes("C:\\Temp\\file.emf", decodedBytes);
}
}
Here is a working solution:
using System;
using Wolfram.NETLink;
public class Example {
public static void Main(String[] args) {
MathKernel k = new MathKernel();
k.Compute("Export[\"c:/users/arnoudb/out.emf\", Graphics[{Disk[]}], \"EMF\"]");
}
}
I am not sure why you consider this part:
k.Graphics[0].Save("C:\\Temp\\file.emf", System.Drawing.Imaging.ImageFormat.Emf);
a Mathematica bug, since k.Graphics[0] is a pure C# System.Drawing.Image class. Perhaps you can clarify this part a bit?

Resources