如何解决这个交叉线程错误?

时间:2016-07-16 01:44:25

标签: mysql vb.net backgroundworker

跨线程操作无效:从创建它的线程以外的线程访问的控制文本框。 vb.net

#include <cstdio>
#include <iostream>

using namespace std;

void compute_coins(int coin_value, int& number, int& amount_left);

int main(){
int amount_left, number;

while(amount_left > 1 || amount_left < 99){
    cout << "Enter number of cents (or zero to quit):" << endl;
    cin >> amount_left;
if (amount_left == 0){
  break;
}
else{
  cout << amount_left << " cents can be given as ";
  compute_coins(25, number, amount_left);
  if (number == 1){
    cout << "1 quarter";
  }
  else if (number > 0){
  cout << number << " quarters";
  }
  if (amount_left > 0){
    cout << ", ";
  }

  compute_coins(10, number, amount_left);
  if (number == 1){
    cout << "1 dime";
  }
  else if (number > 1){
    cout << number << " dimes";
  }
  if (amount_left > 0){
    cout << ", ";
  }
  compute_coins(1, number, amount_left);
  if (number == 1){
    cout << "1 penny";
  }
  else if (number > 1){
    cout << number << " pennies";
  }
  std:: cout << ".";
}
cout << endl;
  }
  return 0;
 }
 void compute_coins(int coin_value, int& number, int& amount_left){
   number = amount_left/coin_value;
   amount_left = amount_left - (number * coin_value);
 }

1 个答案:

答案 0 :(得分:0)

这应该可行,但如果知道代码的位置有更多的话可能会更短

    Dim cmd2 As New Odbc.OdbcCommand
    Dim dr As Odbc.OdbcDataReader
    Dim myquery As String
    myquery = "select payee from tbrequest"
    cmd2.CommandText = myquery
    cmd2.Connection = con
    dr = cmd2.ExecuteReader

    While dr.Read
        If txtPayee.InvokeRequired Then
            txtPayee.Invoke(Sub()
                                txtPayee.AutoCompleteCustomSource.Add(dr.GetString(0)) 'this is the line where the error prompt
                            End Sub)
        Else
            txtPayee.AutoCompleteCustomSource.Add(dr.GetString(0)) 'this is the line where the error prompt
        End If

    End While
    dr.Close()