在代码后面使用SqlDataSource插入它自己的线程

时间:2017-02-01 01:57:37

标签: .net vb.net multithreading

我有一个sqldatasource,我需要在插入之前弹出一个模态,然后关闭一个模态表单。我想,如果我在它自己的线程上执行插入操作,我就可以这样做。在这种情况下我该怎么做?我调查了它,只找到了使用SQLConnection的例子。

 Protected Sub CreateAdditionalBatch()

     '- Do Something before
     SqlDataSource_ESignCreateFormulation.Insert()
     '- Do something after

 end sub

1 个答案:

答案 0 :(得分:1)

这是你做的,在伪代码中,我不记得确切的语法

private Event InsertCompleted 

sub ThisSubDoesInsert()
    ' insert here . . . . 
    Raise Event InsertCompleted
end sub

Sub ThisSubOpensModal()
    using f as new MyForm(ThisSubDoesInsert) ' may need AdressOf(...)
        AddHandler Me.InsertCompleted, AddressOf f.SubThatWillCloseForm 
        f.ShowModal()
        RemoveHandler Me.InsertCompleted, AddressOf f.SubThatWillCloseForm
    End using
End sub

并以表格

class MyForm
    private _callback AS Action ' - set this in constructor

    sub Form_Load
        Me.Show()
        _callback() ' This will call insert
    end sub

    sub SubThatWillCloseForm()
        Me.Close()
    end sub

End Class

在此代码中,您调用一个弹出模态窗体的方法,同时传递回调以在模态打开时执行插入操作。插入完成后,将触发该事件。此事件连接到将关闭表单的表单方法。这是伪代码,你需要让它真实。这里不需要多线程。