I have a struct with five properties, which are set on button _click
method (using Windows Forms Designer). After setting these, the bacground worker is run, using .RunWorkerAsync
. The struct is then used inside the _DoWork
method of the background worker.
However, when I call the struct inside the _ DoWork
method, the properties of the struct all has the value null. Can anyone tell me what I am doing wrong? I am kind of new to C#.
Defining the struct
struct
FtpSetting
{
public String Server { get; set; }
public String Username { get; set; }
public String Password { get; set; }
public String FileName { get; set; }
public String FullName { get; set; }
}
FtpSetting _inputParameter;
Button click function (only relevant code)
FileInfo fi = new FileInfo(ofd.FileName);
_inputParameter.Server = txtServer.Text;
_inputParameter.Username = txtServer.Text;
_inputParameter.Password = txtServer.Text;
_inputParameter.FileName = fi.Name;
_inputParameter.FullName = fi.FullName;
backgroundWorker.RunWorkerAsync();
DoWork function (only relevant code)
string filename = ((FtpSetting)e.Argument).FileName; // Code crashes here
string fullname = ((FtpSetting)e.Argument).FullName; // ...But all the below values
string username = ((FtpSetting)e.Argument).Username; // ...Are null as well :-(
string password = ((FtpSetting)e.Argument).Password;
string server = ((FtpSetting)e.Argument).Server;
What is null
here are not the struct
's properties but the Argument
all together.
You need to call the overload of RunWorkerAsync(object argument)
that takes the argument:
backgroundWorker.RunWorkerAsync(_inputParameter);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.