C#应用程序打开页面和关闭页面(使用计时器)
我正在创建一个应用程序,当按下该按钮时,将在Chrome中打开一个新选项卡,该选项卡将导致一个执行另一个功能的特定页面(我使用线程,因为它需要时间来执行此页面上的功能)。然后关闭页面。我怎样才能做到呢C#应用程序打开页面和关闭页面(使用计时器),c#,winforms,C#,Winforms,我正在创建一个应用程序,当按下该按钮时,将在Chrome中打开一个新选项卡,该选项卡将导致一个执行另一个功能的特定页面(我使用线程,因为它需要时间来执行此页面上的功能)。然后关闭页面。我怎样才能做到呢 var proc = Process.Start("chrome.exe", "google.com"); //open chrome tab. Thread.Sleep(1000); //wait for the page to load and execute
var proc = Process.Start("chrome.exe", "google.com"); //open chrome tab.
Thread.Sleep(1000); //wait for the page to load and execute the function on the page
proc.Kill(); //remove tab
我认为这段代码会起作用(google.com显然不是这个页面)
var proc = Process.Start("chrome.exe", "google.com"); //open chrome tab.
Thread.Sleep(1000); //wait for the page to load and execute the function on the page
proc.Kill(); //remove tab
但是,我得到了一个错误:
var proc = Process.Start("chrome.exe", "google.com"); //open chrome tab.
Thread.Sleep(1000); //wait for the page to load and execute the function on the page
proc.Kill(); //remove tab
“类型为'System.InvalidOperationException'的未处理异常”
发生在System.dll中
var proc = Process.Start("chrome.exe", "google.com"); //open chrome tab.
Thread.Sleep(1000); //wait for the page to load and execute the function on the page
proc.Kill(); //remove tab
其他信息:无法处理请求,因为该进程已启动
退出。”
var proc = Process.Start("chrome.exe", "google.com"); //open chrome tab.
Thread.Sleep(1000); //wait for the page to load and execute the function on the page
proc.Kill(); //remove tab
此外,该选项卡也不会关闭。在执行chrome.exe时,chrome本身会检查chrome的另一个实例是否已经在运行(当您只想添加一个新选项卡时就是这种情况)。因此,新执行的chrome.exe只向已经运行的chrome.exe发送一组信息,用户试图访问cmdline args提供的页面。之后,新执行的chrome.exe将终止
var proc = Process.Start("chrome.exe", "google.com"); //open chrome tab.
Thread.Sleep(1000); //wait for the page to load and execute the function on the page
proc.Kill(); //remove tab
已经运行的chrome.exe现在为新选项卡创建一个新的子进程,这与仅通知用户交互的进程(即:您的chrome.exe实例)完全不同
var proc = Process.Start("chrome.exe", "google.com"); //open chrome tab.
Thread.Sleep(1000); //wait for the page to load and execute the function on the page
proc.Kill(); //remove tab
一种可能的、但并非完全没有错误的方法是扫描打开的进程列表,并在调用完成后扫描新的chrome.exe进程
var proc = Process.Start("chrome.exe", "google.com"); //open chrome tab.
Thread.Sleep(1000); //wait for the page to load and execute the function on the page
proc.Kill(); //remove tab
但即使扼杀了这一进程,也不会结束这一支出。正如你在chrome的早期所注意到的,当一个网站进程崩溃时,用户会收到关于这个问题的通知,页面会被重新加载(一个新的进程会启动)
var proc = Process.Start("chrome.exe", "google.com"); //open chrome tab.
Thread.Sleep(1000); //wait for the page to load and execute the function on the page
proc.Kill(); //remove tab
我猜您必须为远程控制编写一个chrome扩展,或者可能有一个现有的chrome api来实现您想要的功能。我认为这是因为进程。启动时返回一个“null”而不是进程,因为chrome已经在运行
var proc = Process.Start("chrome.exe", "google.com"); //open chrome tab.
Thread.Sleep(1000); //wait for the page to load and execute the function on the page
proc.Kill(); //remove tab
proc.kill()中的null抛出异常
var proc = Process.Start("chrome.exe", "google.com"); //open chrome tab.
Thread.Sleep(1000); //wait for the page to load and execute the function on the page
proc.Kill(); //remove tab
试着看看这个问题,哪个做了类似的事情-
您可以使用visual studio C#中的“计时器”工具,它将简单地完成您的工作var proc = Process.Start("chrome.exe", "google.com"); //open chrome tab.
Thread.Sleep(1000); //wait for the page to load and execute the function on the page
proc.Kill(); //remove tab
请看以下代码:
var proc = Process.Start("chrome.exe", "google.com"); //open chrome tab.
Thread.Sleep(1000); //wait for the page to load and execute the function on the page
proc.Kill(); //remove tab
private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = 100;
}
int timerCounter=0;
private void timer1_Tick(object sender, EventArgs e)
{
timerCounter += 100;
if (timerCounter == 100)
{
var proc = Process.Start("chrome.exe", "google.com"); //open chrome tab.
}
if (timerCounter == 5000) //5000 = 5 minutes , like waiting time for 5 mins to execute the function on this page .
{
proc.Kill(); //remove tab
timerCounter = 0;
timer1.Stop();
}
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Start();
}
由于您已声明可能存在或可能尚未存在Chrome运行实例,因此这可能会导致您想要的结果:
var proc = Process.Start("chrome.exe", "google.com"); //open chrome tab.
Thread.Sleep(1000); //wait for the page to load and execute the function on the page
proc.Kill(); //remove tab
// Open chrome tab.
var proc = Process.Start("chrome.exe", "google.com");
// Wait for the page to load and execute the function on the page.
Thread.Sleep(1000);
// Check for valid running new instance of Chrome.
if (!proc.HasExited)
{
// Kill new Chrome proc.
proc.Kill();
}
else
{
// Find already running proc of Chrome.
var alreadyRunningProc = Process.GetProcessesByName("chrome.exe");
foreach (var chrome in alreadyRunningProc)
{
// Kill any already running Chrome procs.
chrome.Kill();
}
}
尝试执行此操作时chrome是否已在运行?是。我想在一个新的选项卡中运行。如果用户没有打开chrome浏览器;它将打开chrome,然后为他们做。好的,请查看我的答案,并让我知道这可能是你正在寻找的。我加入了一些评论以作进一步解释。刚刚发现了我的错误。请再试一次。我需要更改if语句来检查它是否“已退出”,而不仅仅是一个空对象。嗯,是的,我只是进一步研究了一下,似乎您可能无法仅通过C#关闭特定选项卡。它不允许您在该位置使用var proc。因此,您不能使用“Proc.KillI();”是的,您是对的,var Proc;如果(timerCounter==100){proc=Process.Start(“chrome.exe”,“google.com”);//打开chrome选项卡。}