将条形码扫描器中的数据添加到DataGridView使我的应用程序崩溃C# [英] Adding data from barcode scanner to DataGridView makes my app crash C#

查看:108
本文介绍了将条形码扫描器中的数据添加到DataGridView使我的应用程序崩溃C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个应用程序,使用  POS
for .NET 1.14
 然后将其添加到  DataGridView

I have an app that reads barcode from a OPOS Scanner using POS for .NET 1.14 and then add it to a DataGridView.


我的问题是,在2-3个扫描条形码后,应用程序挂起(虽然由于某些原因没有调试),当添加到datagridview的数据使垂直滚动条出现时,它似乎挂起。

My problem is that the application hangs(not in debug though for some reason) after 2-3 scanned barcode, it seems like it hangs when the data added to datagridview makes the vertical scrollbar appear.


这是我的代码片段:

Here is a snippet of my code:


private BindingSource bSource = new BindingSource();
private DataTable dataTableGrid;

//Reads data from the OPOS barcoder scanner
private async void myScanner_DataEvent(object sender, DataEventArgs e)
{
    ASCIIEncoding myEncoding = new ASCIIEncoding();
    var barcode = myEncoding.GetString(myScanner.ScanDataLabel);
    toolStripStatusLabelBarcodeLetto.Text = "Barcode: " + barcode;
    await Task.Run(() => AnalisiBarcode(barcode)); //Runs on a different thread so the user can scan when the UI is still updating
    CaricaDatiInseriti(); //Should run on UI thread
    myScanner.DataEventEnabled = true;
}

private void AnalisiBarcode(string barcode)
{
//Analyse the scanned barcode and add data to dataTableGrid
}

private void CaricaDatiInseriti() //Load the data created in AnalisiBarcode to DataGridView
{
    if (IsHandleCreated) //To avoid the app to crash if the user scan a barcode when the windows handle is not created yet
    {
         dataGridViewDati.BeginInvoke((MethodInvoker) delegate //Delegate to avoid cross thread problem
         {
              bSource.DataSource = dataTableGrid;
              dataGridViewDati.DataSource = bSource;
              dataGridViewDati.Refresh();
              dataGridViewDati.PerformLayout();
              AggiornaTotCapiInseriti();
         });
    }
}






推荐答案

可能是您的代码陷入死锁,因为您有一个async方法,返回类型为void,而代码中等待任务结果,
除非那些方法是
UI 控制按钮点击等事件,否则 void 永远不应该使用异步等待,您可以尝试更改方法以返回
任务而不是等待

The possibility is your code is getting in to a deadlock, as you have an async method with void as return type and in code you are awaiting a task result, void is async await should never be used unless those methods are UI controls events like button click for example, you can try changing your method to return Task instead of await.

//Reads data from the OPOS barcoder scanner
private async Task myScanner_DataEvent(object sender, DataEventArgs e)
{
    ASCIIEncoding myEncoding = new ASCIIEncoding();
    var barcode = myEncoding.GetString(myScanner.ScanDataLabel);
    toolStripStatusLabelBarcodeLetto.Text = "Barcode: " + barcode;
    await Task.Run(() => AnalisiBarcode(barcode)); //Runs on a different thread so the user can scan when the UI is still updating
    CaricaDatiInseriti(); //Should run on UI thread
    myScanner.DataEventEnabled = true;
}

您还可以阅读以下帖子,以明确何时使用异步任务
async void

You can also read the following post to get clarity on when to use async Task and when async void:

https://stackoverflow.com/questions/12144077/async-await-when-to-return-a-task-vs-void

您也可以参考这篇文章来了解导致死锁的原因:

You may also refer to this post to understand that what causes the deadlock:

https://blog.stephencleary.com/2012/07/dont-block-on-async -code.html

希望有所帮助。


这篇关于将条形码扫描器中的数据添加到DataGridView使我的应用程序崩溃C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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