本文介绍了在VB.net中观看MySQL表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一大堆应用程序(在网络上运行)并连接到常见的MySQL数据库。

这些应用程序中,大多数都涉及等待表格

1.换个新行

2.更改字段

3.按字段条件检索行

我的第一直觉是使用System.Threading.Thread(AddressOf X)创建一个线程
然后在里面有一个While True循环继续查询表然后睡了很短的时间(大约250 - 1000ms)

问题是应用程序导致系统CPU使用率上升到100%(请注意,我使用的是四核,4GB内存),内存使用率也达到100%! br />
通常从系统运行的应用程序数量并不重要。

所以我开始认为线程可能不是答案。是不是因为不断的查询?

任何有DB观看经验的人都可以帮帮我吗?

我已经有了基于线程的应用程序。大多数都是这样的:



1. MainUI.frm将有一个Class

I am developing a whole bunch of applications (that run on a network) and connect to common MySQL DB.
Of these applications, most of them involve waiting for a table to
1. Have a new row
2. Change a field
3. Retrieve rows by a field condition
My first instinct was to create a thread using System.Threading.Thread(AddressOf X)
and then have a While True loop inside that keeps on querying the table and then sleeps for a small amount of time (around 250 - 1000ms)
The problem is that the applications cause my System CPU usage to jump to 100% (mind you, I am using a Quad Core, 4GB RAM) and memory usage to hit 100% too!
It doesn't usually matter how many of the applications are running from a system.
So I got to thinking that maybe threading is not the answer. Is it because of the constant Querying?
Could anyone experienced in DB watching please help me out?
I already have the threading based applications. Most of them are like this:

1. MainUI.frm will have a Class

TableWatcherX



2.


2.

TableWatcherX

将有一个Start()方法和一个

will have a Start() method and a

private workingThread as Thread



3.在Start()方法中,我把


3. In the Start() method, I put

workingThread = new Thread(AddressOf KeepWorking)
                   workingThread.Start()



4.方法KeepWorking()


4. In the method KeepWorking()

While True
    Using ATable as <datasettable> = <datasetadapter>.getData
          For each of the rows, process and do stuff...
    End Using
End While</datasetadapter></datasettable>





显然出现了问题,因为我的内存和CPU完全锁定了!

有什么想法吗?



Apparently something is wrong, as I've got my memory and CPU completely locked!
Any thoughts?

推荐答案

Dim hasData as Boolean
Dim cmd as SqlCommand = new SqlCommand()
' setup your SqlCommand object to connect to your db and call the stored proc

while True
    hasData = CTyle(cmd.ExecutreScalar(), Boolean)
    if (hasData) Then
        'query the database (using a stored procedure again)
        'process ythe returned data
    End If
    Thread.Sleep(5000) 'sleep for 5 seconds
End While



此外,在该代码周围放置一个try / catch块并处理 ThreadAbortException 。可以使用异常,因为线程在调用 Thread.Abort 方法时中止。


Also, put a try/catch block around that code and handle the ThreadAbortException. It'ds okay to eat the exception because the thread aborts when you call the Thread.Abort method.




这篇关于在VB.net中观看MySQL表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 19:18