如何使用Thread从C#中的冻结中释放应用程序 [英] How to use Thread to release the application from freezing in c#
本文介绍了如何使用Thread从C#中的冻结中释放应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有下面的代码要扫描,但是当我按下按钮开始扫描时,Windows表单将冻结,并且在完成该过程之前,我无法移动该表单或最小化/压缩它!我是使用Thread的新手,但是我尝试添加Thread,但我无法解决问题. 有谁知道我可以在哪里(如何)编写线程来释放冻结的表单? 谢谢.
I have the code below for scanning but When i press the button to start scanning, the windows form freezes and i can not move the form or minimizie/colse it until the process will be finished! i'm new in working with Thread but i tried to add Thread andi couldn't solve the problem. Does anyone know to where (how) can i write a Thread to release my form from freezing? Thanks.
public Bitmap GetBitmapFromRawData(int w, int h, byte[] data)
{
//Do sth
}
//button to start the scan proccess
private void button1_Click(object sender, EventArgs e)
{
try
{
var deviceManager = new DeviceManager();
for ( int i = 1; i <= deviceManager.DeviceInfos.Count; i++ )
{
if ( deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType )
{
continue;
}
lstListOfScanner.Items.Add(deviceManager.DeviceInfos[i].Properties["Name"].get_Value());
}
}
catch ( COMException ex )
{
MessageBox.Show(ex.Message);
}
try
{
var deviceManager = new DeviceManager();
DeviceInfo AvailableScanner = null;
for ( int i = 1; i <= deviceManager.DeviceInfos.Count; i++ )
{
if ( deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType ) // Skip device If it is not a scanner
{
continue;
}
AvailableScanner = deviceManager.DeviceInfos[i];
break;
}
var device = AvailableScanner.Connect();
var ScanerItem = device.Items[1];
var imgFile = (ImageFile)ScanerItem.Transfer();
var data = (byte[])imgFile.FileData.get_BinaryData();
var bitmap = GetBitmapFromRawData(imgFile.Width, imgFile.Height, data);
var Path = @"C:\....\ScanImg.jpg";
bitmap.Save(Path, ImageFormat.Jpeg);
}
catch ( COMException ex )
{
MessageBox.Show(ex.Message);
}
推荐答案
尝试采用异步/等待方法:
Try adopting the async/await approach:
private async void button1_Click(object sender, EventArgs e)
{
try
{
object[] items = await Task.Run<object[]>(() =>
{
var deviceManager = new DeviceManager();
List<object> result = new List<object>();
for (int i = 1; i <= deviceManager.DeviceInfos.Count; i++)
{
if (deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType)
{
continue;
}
result.Add(deviceManager.DeviceInfos[i].Properties["Name"].get_Value());
}
return result.ToArray();
});
foreach (var item in items)
{
lstListOfScanner.Items.Add(item);
}
}
catch (COMException ex)
{
MessageBox.Show(ex.Message);
}
try
{
await Task.Run(() =>
{
var deviceManager = new DeviceManager();
DeviceInfo AvailableScanner = null;
for (int i = 1; i <= deviceManager.DeviceInfos.Count; i++)
{
if (deviceManager.DeviceInfos[i].Type != WiaDeviceType.ScannerDeviceType) // Skip device If it is not a scanner
{
continue;
}
AvailableScanner = deviceManager.DeviceInfos[i];
break;
}
var device = AvailableScanner.Connect();
var ScanerItem = device.Items[1];
var imgFile = (ImageFile)ScanerItem.Transfer();
var data = (byte[])imgFile.FileData.get_BinaryData();
var bitmap = GetBitmapFromRawData(imgFile.Width, imgFile.Height, data);
var Path = @"C:\....\ScanImg.jpg";
bitmap.Save(Path, ImageFormat.Jpeg);
});
}
catch (COMException ex)
{
MessageBox.Show(ex.Message);
}
}
这篇关于如何使用Thread从C#中的冻结中释放应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文