线程检索数据库就好了,但是当我尝试访问自定义控件中的某些方法时,我会抛出异常并且我不知道为什么!!
这是我的代码:
{
DatabaseLoader = new Thread(()=> AquireDatabases(LoginInfo));
DatabaseLoader.Name = "DatabaseLoader";
DatabaseLoader.IsBackground = true;
DatabaseLoader.Start();
}
private void AquireDatabases(List<Object> loginInfo)
{
DataTable dtDatabases = new DataTable();
try
{
dtDatabases = objBkp.FillCBDatabases(loginInfo[0].ToString(), loginInfo[1].ToString(), loginInfo[2].ToString());
}
catch (Exception ex)
{
string s = ex.ToString();
MessageBox.Show(s);
}
LoadDatabasesinControl(dtDatabases, null);
}
private void LoadDatabasesinControl(DataTable dtDatabases, RunWorkerCompletedEventArgs e)
{
if (InvokeRequired)
{
this.Invoke(new WorkerCompleteDelegate(LoadDatabasesinControl), dtDatabases, e);
return;
}
if (DatabaseLoader.IsAlive || !(DatabaseLoader.ThreadState == ThreadState.AbortRequested))
{
// CLEAR ALL ITEMS IN the control before filling it
serverControl.ClearControl(); // --> EXCEPTION THROWN
for (int i = 0; i < dtDatabases.Rows.Count; i++)
{
serverControl.CreateDatabase();
serverControl.DatabaseName = dtDatabases.Rows[i][0].ToString();
serverControl.lblSubDBSize = 150513; // --> EXCEPTION THROWN
}
}
}
方法ClearControl()是我控件中的公共方法,它返回类型void,就像CreateDatabase()一样 另外,lblSubDBSize是一个常规公共标签,就像DatabaseName!
一样为什么仅在这两个上抛出异常,而我尝试访问的其他函数都没有? 我认为这是一个线程问题,但我不知道它究竟是什么。 我将自定义控件添加为现有项目,并将其作为项目的引用添加到我的解决方案而不是作为dll。我读到某处可能是那个原因,但事实并非如此,我尝试了一堆没有运气的东西。
任何人的想法???
[编辑]
serverControl是一个自定义用户控件
public Database database;
private Label lblServer;
private Button btnServerPanelExpander;
private Panel pnlServerHeader;
private List<Database> databaseCollection = new List<Database>();
public ServerControl()
{
InitializeComponent();
ControlInitializer();
}
private void ControlInitializer()
{
lblServer = new Label();
pnlServerHeader = new Panel();
btnServerPanelExpander = new Button();
btnServerPanelExpander.Click += btnServerPanelExpander_Click;
pnlServerHeader.Controls.Add(btnServerPanelExpander);
pnlServerHeader.Controls.Add(lblServer);
pnlServer.Controls.Add(pnlServerHeader);
setProperties();
}
public void ClearControl()
{
if (database == null)
return;
this.pnlServer.Controls.Clear();
this.ControlInitializer();
this.pnlServer.Invalidate();
}
public void CreateDatabase()
{
database = new Database();
database.SetIcon(DatabaseIcon);
databaseCollection.Add(database);
pnlServer.Controls.Add(database.pnlDatabase);
DrawDatabase(database);
}
这里是堆栈跟踪和异常......希望这能澄清事情
异常消息 “未找到方法:'Void ServersControl.ServerControl.ClearControl()'。”
堆栈跟踪 “at Forms.MainSmall.LoadDatabasesinControl(DataTable dtDatabases,RunWorkerCompletedEventArgs e)\ r \ n在Forms.MainSmall.AquireDatabases(List`1 loginInfo)\ r \ n在Forms.MainSmall.b__1370_0()\ r \ n在System.Threading .ExecutionContext.RunInternal(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean preserveSyncCtx)\ r \ n在System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean preserveSyncCtx)\ r \ n在System.Threading .ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态)\ r \ n在System.Threading.ThreadHelper.ThreadStart()“