我有一个可以同时显示两个窗体/窗口的应用程序。第一个通过Form.Show()方法显示,因此它是无模的并且浮到一侧。第二种形式是通过Form.ShowDialog()显示的,因此它是模式形式的,并且会阻塞。
它阻止的事实很重要,因为与第一种形式(基本上只是装饰)不同,第二种形式获取重要信息,因此我不希望我的程序在关闭之前继续运行。
不幸的是,我现在需要允许用户在显示第二个表单的同时,与第一个表单进行有限的交互(调整其大小的能力以及其他较小的视觉调整)。
显然,当第二个对话框是模式对话框时,这是行不通的。因此,要么我需要找到一种方法来使第二个表单处于无模式状态,但在打开时仍会阻塞……否则,我需要使第二个表单在模态可见时以某种方式可访问。
我是一位经验丰富的Java Swing程序员,但是我对.NET表单还很陌生,所以这里可能有一个明显的答案,我只是因为对NET api不太熟悉而想念它吗?
这是我编写的一个简单代码:
private void button1_Click(object sender, EventArgs e)
{
label1.Text = "first";
Thread.Sleep(1000);
label1.Text = "second";
}
Run Code Online (Sandbox Code Playgroud)
但标签从不显示"第一".我使用断点检查并且语句label1.text ="first"被执行但在标签中没有显示"first",只显示"second".
为什么会这样?
我试图找到一种从yelp.com获取数据的方法
我有一个电子表格,其中有几个关键字和位置.我希望根据我的电子表格中已有的关键字和位置从yelp列表中提取数据.
我创建了以下代码,但它似乎得到了荒谬的数据,而不是我正在寻找的确切信息.
我想获得公司名称,地址和电话号码,但我得到的只是一无所获.如果有人在这里可以帮我解决这个问题.
Sub find()
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
With ie
ie.Visible = False
ie.Navigate "http://www.yelp.com/search?find_desc=boutique&find_loc=New+York%2C+NY&ns=1&ls=3387133dfc25cc99#start=10"
' Don't show window
ie.Visible = False
'Wait until IE is done loading page
Do While ie.Busy
Application.StatusBar = "Downloading information, lease wait..."
DoEvents
Loop
' Make a string from IE content
Set mDoc = ie.Document
peopleData = mDoc.body.innerText
ActiveSheet.Cells(1, 1).Value = peopleData
End With
peopleData = "" 'Nothing
Set mDoc = Nothing
End Sub
Run Code Online (Sandbox Code Playgroud) 我正在做一些MultiThreading但是表格在加载时暂停.
我试图显示表单,然后在后台它应该填充组合框而不暂停表单.
在我的Form_Load事件中,我有这个:
private void frmIni_Load(object sender, EventArgs e)
{
Application.DoEvents();
Thread threadOne = new Thread(GetServers);
threadOne.Start();
}
Run Code Online (Sandbox Code Playgroud)
在我的GetServers()方法中:
private void GetServers()
{
cboServer.BeginInvoke(
(Action)(() => {
servers = SmoApplication.EnumAvailableSqlServers(false);
Thread.Sleep(1);
foreach (DataRow server in servers.Rows)
{
cboServer.Properties.Items.Add(server["Name"]);
Thread.Sleep(1);
}
}));
}
Run Code Online (Sandbox Code Playgroud)
我在这里错过了什么?表单不应该暂停,它应该工作,然后最终当线程完成时,它应该只填充组合框.
我在客户端系统上面临一个问题.在尝试使用示例代码重现它时,我已经复制了它.
这是示例代码
Imports System.IO
Public Class Form1
Private _lock As New Object
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim t As New Threading.Thread(AddressOf createFile)
With t
.IsBackground = True
.Name = Guid.NewGuid.ToString
.Start()
End With
End Sub
Private Sub createFile()
Dim path As String = "D:\SomeFile.txt"
For i As Integer = 0 To 1000
SyncLock _lock
If File.Exists(path) Then File.Delete(path)
Using fs As New FileStream(path, FileMode.CreateNew)
End Using
End SyncLock
Next
End Sub
End …Run Code Online (Sandbox Code Playgroud) 我尝试在C#中显示/隐藏面板,但是当我点击button1时,我想看到panel1但是出现了panel2.当我按下button2时,panel2消失了.但是当我第一次在按钮2上爬行时,panel2没有出现.我不知道我的代码有什么问题,但它是:
public Form3()
{
InitializeComponent();
}
bool show1;
bool show2;
private void button1_Click(object sender, EventArgs e)
{
if(show1)
{
panel1.Visible = false;
show1 = false;
}
else
{
panel1.Visible = true;
show1 = true;
}
Application.DoEvents();
}
private void button2_Click(object sender, EventArgs e)
{
if (!show2)
{
panel2.Visible = true;
show2 = true;
}
else
{
panel2.Visible = false;
show2 = false;
}
Application.DoEvents();
}
Run Code Online (Sandbox Code Playgroud) 我试图理解Windows Forms中的某个长期概念:UI编程; 以下代码来自Chris Sells的Windows窗体编程书(第2版,2006):
void ShowProgress(string pi, int totalDigits, int digitsSoFar) {
// Display progress in UI
this.resultsTextBox.Text = pi;
this.calcToolStripProgressBar.Maximum = totalDigits;
this.calcToolStripProgressBar.Value = digitsSoFar;
if( digitsSoFar == totalDigits ) {
// Reset UI
this.calcToolStripStatusLabel.Text = "Ready";
this.calcToolStripProgressBar.Visible = false;
}
// Force UI update to reflect calculation progress
this.Refresh();
}
Run Code Online (Sandbox Code Playgroud)
此方法是小样本应用程序的一部分,它具有另一个计算Pi的长期运行方法.每次计算一组数字时,都会调用ShowProgress()来更新UI.正如书中所解释的,这段代码是"错误"的处理方式,并且当应用程序最小化时会导致UI冻结,然后再次进入前台,导致系统要求应用程序重新绘制自己.
我不明白:由于this.Refresh()被重复调用,为什么它不处理任何等待注意的系统重绘事件?
还有一个后续问题:当我在this.Refresh()之后立即添加Application.DoEvents()时,冻结问题就会消失. 这是不必诉诸于Invoke/BeginInvoke等.任何评论?
好的,所以我知道我应该使用ImageMagick DLL ......我正在学习,测试并努力做到这一点.但与此同时,我正在使用通过一个进程调用Imagemagick的convert.exe的低效方法.
当我测试时,它可以正常处理数百张图像.但后来我将我的WindowsForms程序移动到更快的机器上,并且每次都在同一点崩溃.
这是一个两步过程调用.第一次循环遍历所有文件并生成PNG.然后我循环遍历所有PNG并将其与背景合成并输出JPG.但是每次正好在22个图像上出现错误"System.OutOfMemoryException:Out of memory".有什么东西可以填满,我需要杀死什么?
foreach (string file in files)
{
try
{
string captureImg = Path.GetFileName(file);
string maskImg = file.Replace("X.JPG", "Y.JPG");
string OutputImage = string.Format("{0}.png", Path.GetFileNameWithoutExtension(captureImg));
string output = Path.Combine(destFolder, OutputImage);
//MessageBox.Show(output);
progressBarImage.Value = progressBarImage.Value + 1;
lblStatus.Text = string.Format("Image {0} of {1}", progressBarImage.Value, maxFiles);
makePNG(file, maskImg, output);
Application.DoEvents();
}
catch (Exception)
{
}
}
if (chkBG.Checked)
{
//try
//{
string JPGdir = Path.Combine(destFolder, "JPGs");
string[] PNGfiles = Directory.GetFiles(destFolder, "*C.PNG");
lblProgress.Text = "Generating JPGs with …Run Code Online (Sandbox Code Playgroud) 我有一个函数,当函数启动时,我想显示一些UI组件然后开始工作,最后,我想从那里擦除那些组件.问题是我没有在表单上看到UI的变化.
我这样做的功能是:
public void Processor()
{
// ------------- SETTING UI COMPONENTS
lblProgress.Visible = true;
progressBar.Visible = true;
btnStop.Visible = true;
// ------------- WORKING
int counter = 0, percent = 0;
foreach (string url in Urls)
{
.... WORKING THAT TAKES TIME
counter += 1;
percent = ((counter * 100) / Urls.Count());
// ------------- MODIFYING UI COMPONENTS
// Modification doesn't appear on the form while running
lblProgress.Text = "Progress: " + (percent > 100 ? 100 : percent) + "%";
progressBar.Value …Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,我调用了一个更新软件的过程 - 它存储在自己的类中.即使您Application.DoEvents()出于某种原因在少数几个地方写作,更新表单中的标签也没有更新,表单本身也处于非活动状态.
Namespace software
Public Class updater
Public Function UpdateSoftware(ByVal url As String, ByVal downloadFolder As String) As Boolean
Application.DoEvents()
Thread.Sleep(1000)
frmUpdate.lblResult.Text = "Update is about to begin"
Thread.Sleep(1000)
frmUpdate.lblResult.Text = "Downloading data"
Thread.Sleep(1000)
Application.DoEvents()
frmUpdate.lblResult.Text = "About to start the writing process"
Application.DoEvents()
frmUpdate.lblResult.Text = "Software was updated, please restart your device."
End Function
End Class
End Namespace
Run Code Online (Sandbox Code Playgroud) Visual Studio 2017、Windows 10、.NET Framework 4.8、CefSharp WinForms 83.4.20、平台目标 x64
创建了新的非常简单的 CefSharp Windows 窗体应用程序。我无法获取网页的 html 源代码。我想我已经查看了 StackOverflow 上的每个 CefSharp 和异步同步问题 - 尝试了很多解决方案 - 我的头已经变得糊涂了。这是我看到的第一个问题 - 我也有同样的问题。
'浏览器.ViewSource();' 确实会弹出一个记事本,其中包含网页的源代码。但是当我尝试使用源代码获取字符串时 - 任务似乎永远不会运行。运行以获取网页源的任务显示 ---> Status = WaitingForActivation ---> 并且永远不会返回源。
我尝试过异步到同步的转换 - 可能有十种不同的方式。没有工作。一个 StackOverflow 解决方案建议使用 Application.DoEvents() - 所以我什至尝试过。
希望有人有一些想法。这个浏览器似乎有很大的潜力 - 但我需要获取网页源 html。
using System;
using System.Threading.Tasks;
using System.Windows.Forms;
using CefSharp;
using CefSharp.WinForms;
using System.Diagnostics;
namespace Test1
{
public partial class Form1 : Form
{
public ChromiumWebBrowser browser;
public Form1() …Run Code Online (Sandbox Code Playgroud) c# ×7
winforms ×5
.net ×2
vb.net ×2
async-await ×1
asynchronous ×1
cefsharp ×1
excel ×1
excel-vba ×1
file-access ×1
filestream ×1
invoke ×1
modal-dialog ×1
panel ×1
process ×1
vba ×1