P
pbd22
Hi.
I need some help on progress updates for file uploads.
I have Page.aspx.cs and, in the codebehind, I have a loop:
List<ServerVariable> files = new List<ServerVariable>();
for (i = 0; i <= Request.Files.Count - 1; i++)
{
m_objFile = Request.Files;
m_strFileName = m_objFile.FileName;
m_strFileName = Path.GetFileName(m_strFileName);
files.Add(new Status(0, this.m_strFileName,
DateTime.Now));
}
//CODE TO COPY A FILE FOR UPLOAD TO THE
//WEB SERVER
//WHEN THE UPLOAD IS DONE, SET THE ITEM TO
//COMPLETED
int index = files.FindIndex(p => p.Completed == "0");
files[index] = new ServerVariable(1, this.m_strFileName,
DateTime.Now);
Now, I have a client method that uses JQuery/AJAX/JSON to poll a Web
Service (named GetStatus) at intervals with the intent of returning a
JSON string describing which files have not started, which files are
in progress, and which files are completed.
MY QUESTION IS how do I handle the logic so the loop that is setting
data is not deadlocked by the AJAX call that is getting data? Using
code examples online, I have the following class:
public class Status
{
private int _flag; // 0 before upload. 1 at completion.
private string _fileName;
private DateTime _lastUpdate;
private object _sync = new object(); // single lock for both
fields
public Status(int _flag, string _fileName, DateTime
_lastUpdate)
{
this.Flag = _flag;
this.FileName = _fileName;
this.LastUpdate = _lastUpdate;
}
public int Flag
{
get { lock (_sync) { return _flag; } }
set
{
lock (_sync)
{
_flag = value;
}
// Notify listeners
EventHandler handler = Changed;
if (handler != null)
{
handler(this, null);
}
}
}
public string FileName
{
get { lock (_sync) { return _fileName; } }
set { lock (_sync) { _fileName = value; } }
}
public DateTime LastUpdate
{
get { lock (_sync) { return _lastUpdate; } }
set { lock (_sync) { _lastUpdate = value; } }
}
public event EventHandler Changed;
}
Will the above prevent deadlocks as data setting/getting happens? I
would prefer to do it this way if I can.
The below code is where I need help. I am attempting to access
data updates to my Status properties when the AJAX call comes in and
return the data to the client.
This is new territory for me and the code below is based on online
reading. Can somebody comment on what is going on here and correct me
(code examples appreciated) if I am off the mark. Thanks in advance.
List<Status> IUploadService.GetStatus()
{
Status status = new Status();
ManualResetEvent changedEvent = new ManualResetEvent
(false);
Thread thread = new Thread(
delegate() {
status.Changed += delegate { changedEvent.Set();};
while (true) {
changedEvent.WaitOne(Timeout.Infinite);
int flag = status.Flag;
string file = status.FileName;
DateTime lastUpdate = status.LastUpdate;
changedEvent.Reset();
}
}
);
//return progress as a List
}
I need some help on progress updates for file uploads.
I have Page.aspx.cs and, in the codebehind, I have a loop:
List<ServerVariable> files = new List<ServerVariable>();
for (i = 0; i <= Request.Files.Count - 1; i++)
{
m_objFile = Request.Files;
m_strFileName = m_objFile.FileName;
m_strFileName = Path.GetFileName(m_strFileName);
files.Add(new Status(0, this.m_strFileName,
DateTime.Now));
}
//CODE TO COPY A FILE FOR UPLOAD TO THE
//WEB SERVER
//WHEN THE UPLOAD IS DONE, SET THE ITEM TO
//COMPLETED
int index = files.FindIndex(p => p.Completed == "0");
files[index] = new ServerVariable(1, this.m_strFileName,
DateTime.Now);
Now, I have a client method that uses JQuery/AJAX/JSON to poll a Web
Service (named GetStatus) at intervals with the intent of returning a
JSON string describing which files have not started, which files are
in progress, and which files are completed.
MY QUESTION IS how do I handle the logic so the loop that is setting
data is not deadlocked by the AJAX call that is getting data? Using
code examples online, I have the following class:
public class Status
{
private int _flag; // 0 before upload. 1 at completion.
private string _fileName;
private DateTime _lastUpdate;
private object _sync = new object(); // single lock for both
fields
public Status(int _flag, string _fileName, DateTime
_lastUpdate)
{
this.Flag = _flag;
this.FileName = _fileName;
this.LastUpdate = _lastUpdate;
}
public int Flag
{
get { lock (_sync) { return _flag; } }
set
{
lock (_sync)
{
_flag = value;
}
// Notify listeners
EventHandler handler = Changed;
if (handler != null)
{
handler(this, null);
}
}
}
public string FileName
{
get { lock (_sync) { return _fileName; } }
set { lock (_sync) { _fileName = value; } }
}
public DateTime LastUpdate
{
get { lock (_sync) { return _lastUpdate; } }
set { lock (_sync) { _lastUpdate = value; } }
}
public event EventHandler Changed;
}
Will the above prevent deadlocks as data setting/getting happens? I
would prefer to do it this way if I can.
The below code is where I need help. I am attempting to access
data updates to my Status properties when the AJAX call comes in and
return the data to the client.
This is new territory for me and the code below is based on online
reading. Can somebody comment on what is going on here and correct me
(code examples appreciated) if I am off the mark. Thanks in advance.
List<Status> IUploadService.GetStatus()
{
Status status = new Status();
ManualResetEvent changedEvent = new ManualResetEvent
(false);
Thread thread = new Thread(
delegate() {
status.Changed += delegate { changedEvent.Set();};
while (true) {
changedEvent.WaitOne(Timeout.Infinite);
int flag = status.Flag;
string file = status.FileName;
DateTime lastUpdate = status.LastUpdate;
changedEvent.Reset();
}
}
);
//return progress as a List
}