C#htmlelement如何从没有竞争条件的计算机中选择一个文件(来自webbrowser访问的htmlelement) [英] C# htmlelement how to programmaticaly select a file (from a htmlelement accessed by a webbrowser) from a computer without race condition

查看:54
本文介绍了C#htmlelement如何从没有竞争条件的计算机中选择一个文件(来自webbrowser访问的htmlelement)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于htmlelement类来说,我是相当新手!



我现在要做的是填充一个非常无聊的外部网页加载已经存在于计算机上的数据(程序在所述计算机上本地执行)。手动做的烦人和耗时的事情之一是从你的计算机中选择设备安装的图片,然后将其提交到那个表格,这样做,可以通过使用HTMLElement类和webBrowser控件,但我讨厌不安全的编码需要这样做,我称之为不安全,因为它有竞争条件问题,请看一下:



以下代码不是由我创建的

  async 任务PopulateInputFile(HtmlElement文件)
{
file.Focus();

// 延迟执行SendKey以显示选择文件对话框
var sendKeyTask = Task.Delay( 500 )。ContinueWith((_)= < span class =code-keyword>>
{
// 当对话框可见时执行此操作
SendKeys.Send( C:\\\ \\ images\\CCPhotoID.jpg + {ENTER});
},TaskScheduler.FromCurrentSynchronizationContext());

file.InvokeMember( 点击); // 这会显示对话框

等待 sendKeyTask;

// 延迟继续让选择文件对话框隐藏
await Task.Delay( 500 );
}

async 任务填充()
{
var elements = webBrowser.Document.GetElementsByTagName( input);
foreach (HtmlElement文件 元素)
{
if (file.GetAttribute( name) == file
{
file.Focus();
await PopulateInputFile(file);
}
}
}



好​​的,现在你的计算机硬盘处于睡眠状态,或者处于非常紧张的防病毒扫描中同时更新窗口,自然出现的模态窗口甚至可以达到10秒。那意味着你输入文件的路径并在显示的对话框出现之前按下输入方式,这将导致程序卡住,永远不会离开执行代码行,也不会向用户或编码器显示任何错误(这是预期的。



我试图做的正是这个,但同时避免非常明显的竞争条件,我正在考虑在一个循环中检查每个500毫秒看看窗口是否打开然后才执行sendkeys,如下所示:



 AutoUploadPicture(字符串路径
{
fileUploadDialogElement.Focus(); // HTMLElement取自webBrowser1.Document

Task.Run(()=> PerformAsyncCheckup());
fileUploadDialogElement.InvokeMember( click);
CancelTask​​ = true ;

// 成功继续编码,因为它避免了丑陋的竞争条件
}
bool CancelTask​​ = false ;
PerformAsyncCheckup()
{
while (webBrowser1.AnyDialogFormShowing == false && CancelTask​​ == false
{
Thread.Sleep( 200 < /跨度>);
}
if (CancelTask​​ == false
SendKeys。发送( C:\\Images\\CCPhotoID.jpg + {ENTER});



是什么我想要可能?香港专业教育学院搜索谷歌和stackoverflow,所有的解决方案都是基于一些随机计时器,总是存在竞争条件错误。



最好的问候!



我尝试过:



 PerformAsyncCheckup()
{
while (webBrowser1.AnyDialogFormShowing == false && CancelTask​​ == false
{
Thread.Sleep( 200 );
}
if (CancelTask​​ == false
SendKeys。发送( C:\\Images\\CCPhotoID.jpg + {ENTER});

解决方案

我已经通过忽略sendkeys方法并使用线程和windows内部消息系统的组合来检测和配置我的应用程序之上的模态窗口来解决问题,这解决了当前编码的可靠性问题。 / BLOCKQUOTE>

im quite the newbie when it comes to htmlelement class!

What i am trying to do right now is to fill a very very boring external web page with loads of data that already exists on the computer (the program is executed locally on said computer). one of those annoying and time consuming things to do manually is to select picture of an equipment installation from your computer and then submit it to that form, doing so, is possible by using the HTMLElement class and webBrowser control, but i hate the unsafe coding required to do so, i call it unsafe because it has race condition issues, here take a look:

The following code was not created by me

async Task PopulateInputFile(HtmlElement file)
{
    file.Focus();

    // delay the execution of SendKey to let the Choose File dialog show up
    var sendKeyTask = Task.Delay(500).ContinueWith((_) =>
    {
        // this gets executed when the dialog is visible
        SendKeys.Send("C:\\Images\\CCPhotoID.jpg" + "{ENTER}");
    }, TaskScheduler.FromCurrentSynchronizationContext());

    file.InvokeMember("Click"); // this shows up the dialog

    await sendKeyTask;

    // delay continuation to let the Choose File dialog hide
    await Task.Delay(500); 
}

async Task Populate()
{
    var elements = webBrowser.Document.GetElementsByTagName("input");
    foreach (HtmlElement file in elements)
    {
        if (file.GetAttribute("name") == "file")
        {
            file.Focus();
            await PopulateInputFile(file);
        }
    }
}


Ok, now image your computer has the HDD sleeping, or its in a very stressfull antivirus scanning + updating windows at the same time, naturally the modal window to appear can take even up to 10 seconds. what then that means is youre typing the path of the file and pressing enter way before the shown dialog appears which will cause the program to get stuck never leaving that failed to execute line of code, nor presenting the user or the coder with any error (which is expected).

What im trying to do is exactly that but at the same time avoiding that very obvious race condition, i was considering checking each 500ms in a loop to see if window is open and only then execute the sendkeys, something like this:

AutoUploadPicture(string path)
{
    fileUploadDialogElement.Focus();//HTMLElement Taken from webBrowser1.Document

    Task.Run(()=>PerformAsyncCheckup());
    fileUploadDialogElement.InvokeMember("click");
    CancelTask = true;

    //successfully continue coding as it avoided that ugly race condition
}
bool CancelTask = false;
PerformAsyncCheckup()
{
     while (webBrowser1.AnyDialogFormShowing==false && CancelTask == false)
     {
         Thread.Sleep(200);
     }
     if (CancelTask ==false)
         SendKeys.Send("C:\\Images\\CCPhotoID.jpg" + "{ENTER}");


Is what i want possible? ive searched google and stackoverflow and all solutions were based on some random timer which always risks race condition errors.

Best Regards!

What I have tried:

PerformAsyncCheckup()
{
     while (webBrowser1.AnyDialogFormShowing==false && CancelTask == false)
     {
         Thread.Sleep(200);
     }
     if (CancelTask ==false)
         SendKeys.Send("C:\\Images\\CCPhotoID.jpg" + "{ENTER}");

解决方案

I have fixed the problem by ignoring the sendkeys method and using a combination of threading and windows internal messaging system to detect and configure modal windows on top of my application, which fixes the reliability issues with this current coding.


这篇关于C#htmlelement如何从没有竞争条件的计算机中选择一个文件(来自webbrowser访问的htmlelement)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆