Gecko WebBrowser.ReadyState - browser

I'm creating an application that contains "geckoWebBrowser" in c #. But I have to wait the complete loading a web page, and then continue to execute other instructions. there is something similar to WebBrowser.ReadyState? thank you very much

Hi GeckoWebBrowser has a DocumentCompleted event you could use it.
Edit:
for example you have a button to show url
private void ShowBtnClick(object sender, EventArgs e)
{
geckoWebBrowser1.Size = new Size(int.Parse(ViewportWidth.Text), int.Parse(ViewportHeight.Text));
geckoWebBrowser1.DocumentCompleted += LoadingFinished;
geckoWebBrowser1.Navigate(PageUrl.Text);
}
private void LoadingFinished(object sender, EventArgs args)
{
GeckoElement body = geckoWebBrowser1.Document.GetElementsByTagName("body")[0];
body.SetAttribute("style", "margin-top:-700px");
}

Try this :
private void WaitBrowser(Gecko.GeckoWebBrowser wb)
{
while (wb.IsBusy)
{
Application.DoEvents();
}
}

Related

How can I override or act on KeyDown: Space or Enter for ListView in UWP?

I've attached the KeyDown event to a ListView in my Win 10 UWP app. I want to make VirtualKey.Enter have a special effect, but the event is not firing for this particular key. Neither does it for Space, Arrow up or down. This I guess because the listview already has defined a special behaviour for those keys.
I'd like to override some of those keys though, or at least trigger additional actions. Even attaching events to those key with modifiers (e.g. Shift+ArrowDown) would not work because the events still are not firing.
I read that for WPF that there is a PreviewKeyDown-event which one can attach to. I can't find that event for UWP though. Are there any other options?
Stephanie's answer is a good one and it works in the general case. However, as Nilzor observed it will not work in the case of a ListView for the Enter key. For some reason the ListView handles the KeyDown event in case Enter is pressed.
A better way to handle key events when dealing with a ListView, as the question asks, is this.
private void ListView_Loaded(object sender, RoutedEventArgs e)
{
(sender as ListView).AddHandler(UIElement.KeyDownEvent, new KeyEventHandler(ListView_KeyDown), true);
}
private void ListView_KeyDown(object sender, KeyRoutedEventArgs args)
{
if (args.Key == Windows.System.VirtualKey.Enter)
{
}
}
Notice the last argument in the AddHandler function. This specifies whether we want to handle events already handled by a previous element in the visual tree.
Of course don't forget to unsubscribe from the event when appropriate
Here is one way to do it : subscribe to the global Window.Current.CoreWindow.KeyDown event.
Then save the focus state of your listview and react accordingly.
Here is the code :
public sealed partial class MainPage : Page
{
bool hasFocus = false;
public MainPage()
{
this.InitializeComponent();
Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
}
private void CoreWindow_KeyDown(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.KeyEventArgs args)
{
if(hasFocus)
{
Debug.Write("Key down on list");
}
}
private void myList_GotFocus(object sender, RoutedEventArgs e)
{
hasFocus = true;
}
private void myList_LostFocus(object sender, RoutedEventArgs e)
{
hasFocus = false;
}
You will also need to subscribe to the focus events in xaml, for your ListView :
<ListView .... GotFocus="myList_GotFocus" LostFocus="myList_LostFocus"/>
Corcus's solution doesn't work for me. What is working is handling PreviewKeyDown directly from XAML. Works well for SPACE or ENTER key:
XAML:
<ListView PreviewKeyDown="BookmarksListView_PreviewKeyDown">
Code behind:
private void BookmarksListView_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Enter)
{
// DO YOUR STUFF...
e.Handled = true;
}
}
You can use AddHandler method.
private void KeyEnterEventHandler(object sender, KeyRoutedEventArgs e)
{
if (e.OriginalKey == Windows.System.VirtualKey.Enter)
{
PlayFromListView();
}
}
private void LoadListView()
{
foreach (var music in playListStorageFile.PlayList)
{
ListViewItem item = new ListViewItem();
item.AddHandler(FrameworkElement.KeyDownEvent, new KeyEventHandler(KeyEnterEventHandler), true);
TextBlock mytext = new TextBlock();
mytext.Text = music.Nro.ToString() + " - " + music.Name;
mytext.Tag = music.Nro;
item.Content = mytext;
lvMusics.Items.Add(item);
}
}
https://learn.microsoft.com/en-us/uwp/api/windows.ui.xaml.uielement.addhandler?view=winrt-18362

c# showing error while browsing a webpage

This is a test program. I just created a simple Windows application form with one button, and if the button is clicked, I need it to do something. So, I wrote my code as:
IWebDriver driver;
public Form1()
{
InitializeComponent();
}
public void SetupTest()
{
driver = new FirefoxDriver();
}
private void button1_Click(object sender, EventArgs e)
{
driver.Navigate().GoToUrl("webaddress");
driver.FindElement(By.TagName("Atlast")).Click();
Thread.Sleep(5000);
}
I have included all of the dependencies (both code and references), but I am getting the following error when I click the button:
Object reference not set to an instance of an object. in driver.navigate part of my code..
What mistake did I make here? Can anyone please help me out with this?
private void button1_Click(object sender, EventArgs e)
{
SetupTest()
driver.Navigate().GoToUrl("webaddress");
driver.FindElement(By.TagName("Atlast")).Click();
Thread.Sleep(5000);
}
You need to be calling SetupTest in your button click code. Why? This is where you are creating your new instance of the IWebDriver, therefore it needs to be called otherwise any references to driver will simply refer to null (by default).

ServiceStack Profile Steps not rendering

I have a ServiceStack Service with a service call like so:
public class MyService : Service
{
public object Get(MyServiceRequest request)
{
using (Profiler.Current.Step("Getting Data"))
{
// code that gets data
using (Profiler.Current.Step("Doing work with data"))
{
// code that does work
}
}
return response;
}
}
and a global.asax.cs like so:
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
new AppHost().Init();
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
if (Request.IsLocal)
Profiler.Start();
}
protected void Application_EndRequest(object sender, EventArgs e)
{
Profiler.Stop();
}
}
My problem is that when I test the service call through the browser I only see profile information for the overall request. "show time with children" and "show trivial" don't provider any more granular information. I've also placed breakpoints within each using statement to get a look at Profiler.Current and noticed in each case its Children property is null. Am I doing it wrong? Are they any other things I can do to troubleshoot this?
For some reason setting the level argument to Profile.Info in the Step method resolves my issue. That's weird because Profile.Info is the default if no argument is provided. Oh well. Moving on.

How do I get the PrivilegedProcessorTime and UserProcessorTime for the current thread

I found several examples of how to get the PrivilegedProcessorTime and UserProcessorTime for a process and for all threads, but how do I get the PrivilegedProcessorTime and UserProcessorTime for the current managed thread.
private void button1_Click(object sender, EventArgs e)
{
ThreadPool.QueueUserWorkItem(new WaitCallback(WorkCore));
}
static void WorkCore(Object stateInfo)
{
// Code to get and do work
// Code to get and log PrivilegedProcessorTime and UserProcessorTime
}
I got this to work but AppDomain.GetCurrentThreadId has been deprecated
static void WorkCore(Object stateInfo)
{
// Code to get and do work
// Code to get and log PrivilegedProcessorTime and UserProcessorTime
ProcessThreadCollection m_Threads = Process.GetCurrentProcess().Threads;
foreach(ProcessThread t in m_Threads)
{
if (t.Id == AppDomain.GetCurrentThreadId())
{
sw.WriteLine("User Time " + t.UserProcessorTime.ToString(#"d\:hh\:mm\:ss"));
sw.WriteLine("Kernel Time " + t.PrivilegedProcessorTime.TotalSeconds.ToString("F0") + " sec");
}
}
}

Threading multiple async calls

Part of my Silverlight application requires data from three service requests. Up until now I've been chaining the requests so as one completes the other starts... until the end of the chain where I do what I need to do with the data.
Now, I know thats not the best method(!). I've been looking at AutoResetEvent (link to MSDN example) to thread and then synchronize the results but cannot seem to get this to work with async service calls.
Does anyone have any reason to doubt this method or should this work? Code samples gratefully received!
Take a look at this example:
Will fire Completed event and print 'done' to Debug Output once both services returned.
Key thing is that waiting for AutoResetEvents happens in background thread.
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
Completed += (s, a) => { Debug.WriteLine("done"); };
wrk.DoWork += (s, a) =>
{
Start();
};
wrk.RunWorkerAsync();
}
public event EventHandler Completed;
private void Start()
{
auto1.WaitOne();
auto2.WaitOne();
Completed(this, EventArgs.Empty);
}
public AutoResetEvent auto1 = new AutoResetEvent(false);
public AutoResetEvent auto2 = new AutoResetEvent(false);
BackgroundWorker wrk = new BackgroundWorker();
private void Button_Click(object sender, RoutedEventArgs e)
{
ServiceReference1.Service1Client clien = new SilverlightAsyncTest.ServiceReference1.Service1Client();
clien.DoWorkCompleted += new EventHandler<SilverlightAsyncTest.ServiceReference1.DoWorkCompletedEventArgs>(clien_DoWorkCompleted);
clien.DoWork2Completed += new EventHandler<SilverlightAsyncTest.ServiceReference1.DoWork2CompletedEventArgs>(clien_DoWork2Completed);
clien.DoWorkAsync();
clien.DoWork2Async();
}
void clien_DoWork2Completed(object sender, SilverlightAsyncTest.ServiceReference1.DoWork2CompletedEventArgs e)
{
Debug.WriteLine("2");
auto1.Set();
}
void clien_DoWorkCompleted(object sender, SilverlightAsyncTest.ServiceReference1.DoWorkCompletedEventArgs e)
{
Debug.WriteLine("1");
auto2.Set();
}
}
It could be done using the WaitHandle in the IAsyncResult returned by each async method.
The code is simple. In Silverlight I just do 10 service calls that will add an item to a ListBox. I'll wait until all the service calls end to add another message to the list (this has to run in a different thread to avoid blocking the UI). Also note that adding items to the list have to be done through the Dispatcher since they will modify the UI. There're a bunch of lamdas, but it's easy to follow.
public MainPage()
{
InitializeComponent();
var results = new ObservableCollection<string>();
var asyncResults = new List<IAsyncResult>();
resultsList.ItemsSource = results;
var service = new Service1Client() as Service1;
1.To(10).Do(i=>
asyncResults.Add(service.BeginDoWork(ar =>
Dispatcher.BeginInvoke(() => results.Add(String.Format("Call {0} finished: {1}", i, service.EndDoWork(ar)))),
null))
);
new Thread(()=>
{
asyncResults.ForEach(a => a.AsyncWaitHandle.WaitOne());
Dispatcher.BeginInvoke(() => results.Add("Everything finished"));
}).Start();
}
Just to help with the testing, this is the service
public class Service1
{
private const int maxMilliSecs = 500;
private const int minMillisSecs = 100;
[OperationContract]
public int DoWork()
{
int millisSecsToWait = new Random().Next(maxMilliSecs - minMillisSecs) + minMillisSecs;
Thread.Sleep(millisSecsToWait);
return millisSecsToWait;
}
}

Resources