2014-12-18 59 views
3

我是一位自學新手,嘗試使用Rijndael encryption algorithm,之前我已經將其工作得很好,但由於試圖使加密在後臺工作人員上運行,爲用戶解決了很多問題。將值傳遞給後臺工作人員

首先,我試着將我以前的代碼中使用的byval放到後臺工作器中,但是我得到了一個不兼容的錯誤 - 具體是ByVal Direction As CryptoAction

所以要清楚,我是想這樣的事情:

Private Sub bw_DoWork(ByVal sender As Object,ByVal strInputFile As String, 
            ByVal strOutputFile As String, 
            ByVal bytKey() As Byte, 
            ByVal bytIV() As Byte, 
            ByVal Direction As CryptoAction, ByVal e As DoWorkEventArgs) 


    Dim worker As BackgroundWorker = CType(sender, BackgroundWorker) 

    For i = 1 To 1 
     If bw.CancellationPending = True Then 
      e.Cancel = True 
      Exit For 
     Else 

      'Encryption code goes here. 
end sub 

但正如我所說,我被告知方向CrytoAction是不兼容的。在格蘭intrest completness,即子是:

Private Enum CryptoAction 
     'Define the enumeration for CryptoAction. 
     ActionEncrypt = 1 
     ActionDecrypt = 2 
    End Enum 
爲了讓代碼工作,我這樣做,我想這就是爲什麼我的加密文件解密成不可讀文件,我有時會得到錯誤

所以:填充無效,無法刪除。當關閉文件流時。

所以我宣佈bytkey,由IV,方向和其餘在我的公共類,這,正如我所說的讓代碼運行。但是我可以在加密後重新獲得文件。我的想法是,當我的代碼引用函數,而不是接收返回的值,它接收一個空字符串?

下面是引用了生成密鑰,僅僅是在什麼我的意思明確的功能的代碼。

    Select Case Direction 
         Case CryptoAction.ActionEncrypt 
          csCryptoStream = New CryptoStream(fsOutput, _ 
          cspRijndael.CreateEncryptor(bytKey, bytIV), _ 
          CryptoStreamMode.Write) 

        Case CryptoAction.ActionDecrypt 
         csCryptoStream = New CryptoStream(fsOutput, _ 
         cspRijndael.CreateDecryptor(bytKey, bytIV), _ 
         CryptoStreamMode.Write) 
       End Select 

所以,我的問題是,我是正確的代碼引用從聲明歸零值,或將代碼仍引用的功能呢?而且,是否可以將ByVal CryptoStream添加到後臺工作器中?

對於我的問題的大小,我最真誠的appologise,我到處尋找解決方案,但找不到任何相關的東西。

+0

我編輯了你的標題。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 –

回答

2

您不能更改DoWork事件的簽名,但它包含一個供您使用的通用Object(稱爲e.Argument)。定義一個自定義類以包含您想要移動到的所有數據以及BackgroundWorker

Option Strict On 

Public Class Form1 
    Class MyParameters 
    Friend Input As Integer 
    Friend Output As Integer 
    Friend Message As String 
    End Class 

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    Dim m As New MyParameters 
    m.Input = 1 
    ListBox1.Items.Add("Button1_Click") 
    ListBox1.Items.Add(" Input: " & m.Input) 
    BackgroundWorker1.WorkerReportsProgress = True 
    BackgroundWorker1.RunWorkerAsync(m) 'this triggers BackgroundWorker1_DoWork. When finished, BackgroundWorker1_RunWorkerCompleted() is raised 
    End Sub 

    Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork 
    Dim m As MyParameters = DirectCast(e.Argument, MyParameters) 'Convert the generic Object back into a MyParameters object 
    m.Message = "Progress report" 
    BackgroundWorker1.ReportProgress(50, m) 'this triggers BackgroundWorker1.ProgressChanged. N.B. We could actually use a different class here, but we'll re-use the same class for simplicity 
    m.Output = m.Input + 1 
    e.Result = m 
    'fun continues at BackgroundWorker1_RunWorkerCompleted 
    End Sub 

    Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged 
    ListBox1.Items.Add("BackgroundWorker1_ProgressChanged") 
    Dim m As MyParameters = DirectCast(e.UserState, MyParameters) 'Convert the generic Object back into a MyParameters object 
    ListBox1.Items.Add(" " & m.Message & " " & e.ProgressPercentage.ToString) 
    End Sub 

    Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted 
    ListBox1.Items.Add("BackgroundWorker1_RunWorkerCompleted") 
    Dim m As MyParameters = DirectCast(e.Result, MyParameters) 'Convert the generic Object back into a MyParameters object 
    ListBox1.Items.Add(" Output: " & m.Output) 
    End Sub 
End Class 
+0

謝謝,這對我有一些編輯工作;) – user3516240