System.Threading.ThreadStateException 打开文件对话框
System.Threading.ThreadStateException OpenFileDialog
今天,我试图用 C# 制作一个具有良好 GUI 的 adb 客户端。
所以,我做了一些研究并找到了 SharpAdbClient。
要进行文件推送,我使用 var file = openFileDialog2.ShowDialog();
到 select 一个文件。
但是如果我尝试推送一个大文件,GUI 会停止响应(它应该是这样的)。
所以,为了解决这个问题,我设置了一个执行推送的线程,但是当我尝试启动 OpenFileDialog
.[=15= 时,我遇到了 ThreadStateException
]
这是一个示例代码:
private void button4_Click(object sender, EventArgs e)
{
Thread pushFile = new Thread(push);
pushFile.Start();
}
private void push()
{
var device = AdbClient.Instance.GetDevices().First();
var file = openFileDialog2.ShowDialog();
var p = new Progress<int>(Progress_Bar);
String newPath = textBox2.Text;
if (file == DialogResult.OK)
{
String filePath = openFileDialog2.InitialDirectory + openFileDialog2.FileName;
using (SyncService service = new SyncService(new AdbSocket(new IPEndPoint(IPAddress.Loopback, AdbClient.AdbServerPort)), device))
using (Stream stream = File.OpenRead(filePath))
{
service.Push(stream, newPath, 444, DateTime.Now, p, CancellationToken.None);
}
}
}
您不能在非 GUI 线程的线程上调用 UI 方法。您必须将其分派到正确的线程。在 WinForms 中,您将使用 Invoke
、BeginInvoke
和类似的方法来做到这一点。
查看 Control.Invoke
documentation 了解更多信息。
今天,我试图用 C# 制作一个具有良好 GUI 的 adb 客户端。 所以,我做了一些研究并找到了 SharpAdbClient。
要进行文件推送,我使用 var file = openFileDialog2.ShowDialog();
到 select 一个文件。
但是如果我尝试推送一个大文件,GUI 会停止响应(它应该是这样的)。
所以,为了解决这个问题,我设置了一个执行推送的线程,但是当我尝试启动 OpenFileDialog
.[=15= 时,我遇到了 ThreadStateException
]
这是一个示例代码:
private void button4_Click(object sender, EventArgs e)
{
Thread pushFile = new Thread(push);
pushFile.Start();
}
private void push()
{
var device = AdbClient.Instance.GetDevices().First();
var file = openFileDialog2.ShowDialog();
var p = new Progress<int>(Progress_Bar);
String newPath = textBox2.Text;
if (file == DialogResult.OK)
{
String filePath = openFileDialog2.InitialDirectory + openFileDialog2.FileName;
using (SyncService service = new SyncService(new AdbSocket(new IPEndPoint(IPAddress.Loopback, AdbClient.AdbServerPort)), device))
using (Stream stream = File.OpenRead(filePath))
{
service.Push(stream, newPath, 444, DateTime.Now, p, CancellationToken.None);
}
}
}
您不能在非 GUI 线程的线程上调用 UI 方法。您必须将其分派到正确的线程。在 WinForms 中,您将使用 Invoke
、BeginInvoke
和类似的方法来做到这一点。
查看 Control.Invoke
documentation 了解更多信息。