Windows mobile worker thread to post form data using a queue

A threaded form uploader app in C#

Recently I had to do an app that watches a dir for new files and then uploads them to a DocuShare server using http form post.

I was told, that it is very easy to use form post from code and decided to do the app in C#. Unfortunately, it was not that easy to find code that does uploads via form post, especially for Compact Framework (CF). Maybe it is easier in JAVA, but for JAVA I had to install a JAVA VM onto the windows mobile 6.1 device and for CF all runtimes are already on the device. I searched the internet for valuable form post code in C# and found one titled ‘C# is the better JAVA‘.
OK, this code looked good and i tried it within Compact Framework. After changing the functions to ones available in CF, the code runs fine. As you know, CF does not have the same full featured functions as the .Net Framework for PCs.

Here is my FormPost class:

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Drawing;
/*
string sForm = "<html>" +
"<body>" +
"<form action=\"http://xxx.xxx.xxx.xxx:80/docushare/UploadServlet\"" +
"enctype=\"multipart/form-data\" method=\"post\">" +
"<label for=\"summary\">Zusammenfassung</label>&nbsp;<input type=\"text\" id=\"summary\" name=\"summary\" size=\"100\"> <br/>" +
"<label for=\"description\">Beschreibung</label>&nbsp;<input type=\"text\" id=\"description\" name=\"description\" size=\"100\"> <br/> " +
"<br/>" +
"<label for=\"datafile\">Datei</label>&nbsp;<input type=\"file\" id=\"datafile\" name=\"datafile\" size=\"100\">" +
"<br/><input type=\"submit\" value=\"Send\">" +
"</form>" +
"</body>" +
"</html>";
*/

/*    //================== usage =================
List<Post.IPostAble> Fields = new List<Post.IPostAble>();
Fields.Add(new Post.PostText("Test", "Hi"));
Fields.Add(new Post.PostPicture("C:\\Dokumente und Einstellungen\\Kevin\\Desktop\\test.png", "file", System.Drawing.Imaging.ImageFormat.Png));
Fields.Add(new Post.PostText("Test2", "Hallo"));
HPText.Text = Post.ExecutePostCommand("http://localhost/Test/upload.php", Fields);
*/
namespace Peiler.ServerCommunication
{
public static class Post
{
private static string CrLf = "\r\n";
public static String UserAgent = "PostMan";
public static String ExecutePostCommand(String URL, List<IPostAble> Fields)
{   // uriStr is set by config - the address where the form posts to.
HttpWebRequest Request = (HttpWebRequest)HttpWebRequest.Create(URL);
Request.PreAuthenticate = true;
Request.AllowWriteStreamBuffering = true;

String Boundary = System.Guid.NewGuid().ToString();

Request.ContentType = String.Format("multipart/form-data;boundary={0}", Boundary);
Request.Method = "POST";
// not sure if this was necessary, but found forums where it was an issue not to set it.
Request.UserAgent = Post.UserAgent;

// Build Contents for Post
String Header = String.Format("--{0}", Boundary);
String Footer = Header + "--";

//Get Bytes
Byte[] FieldBytes = new Byte[0];
foreach(IPostAble Field in Fields)
{
FieldBytes = ConnectByteArrays(FieldBytes, Field.GetBytes(Header));
}

//Footer
FieldBytes = ConnectByteArrays(FieldBytes, Encoding.UTF8.GetBytes(Footer.ToString()));

// now we have all of the bytes we are going to send, so we can calculate the size of the stream
Request.ContentLength = FieldBytes.Length;

using (Stream requestStream = Request.GetRequestStream())
{
requestStream.Write(FieldBytes, 0, FieldBytes.Length);
requestStream.Flush();
requestStream.Close();
System.Diagnostics.Debug.WriteLine("### post finished ###");

using (WebResponse response = Request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
return reader.ReadToEnd();
}
}
}
}

public interface IPostAble
{
Byte[] GetBytes(String Header);
}

public class PostText : IPostAble
{
public String FieldName;
public String FieldValue;

public PostText(String FieldName, String FieldValue)
{
this.FieldName = FieldName;
this.FieldValue = FieldValue;
}

public Byte[] GetBytes(String Header)
{
StringBuilder Content = new StringBuilder();
Content.Append(Header+"\r\n");
Content.Append(String.Format("Content-Disposition:form-data; name=\"{0}\"\r\n", FieldName));
Content.Append("\r\n");
Content.Append(FieldValue+"\r\n");
return Encoding.UTF8.GetBytes(Content.ToString());

}
}

public class PostPicture : IPostAble
{
public Bitmap Bmp;
public String FieldName;
public String FileName;
public System.Drawing.Imaging.ImageFormat Format;
public string sFormat = "image/jpeg";

public PostPicture(String FileName, String FieldName, System.Drawing.Imaging.ImageFormat Format)
{
this.FileName = Path.GetFileName(FileName);
this.FieldName = FieldName;
this.Bmp = new Bitmap(FileName);
this.Format = Format;
}

public PostPicture(String FileName, String FieldName, string strMimeType)
{
this.FileName = Path.GetFileName(FileName);
this.FieldName = FieldName;
this.Bmp = new Bitmap(FileName);
this.Format = System.Drawing.Imaging.ImageFormat.Jpeg;
this.sFormat = strMimeType;
}

public PostPicture(String FileName, String FieldName, Bitmap Bmp)
{
this.FileName = Path.GetFileName(FileName);
this.FieldName = FieldName;
this.Bmp = Bmp;
}

public Byte[] GetBytes(String Header)
{
if (Format == null)
Format = System.Drawing.Imaging.ImageFormat.Jpeg;
StringBuilder Content = new StringBuilder();
Content.Append(Header+CrLf);
Content.Append(String.Format("Content-Disposition:form-data; name=\"{0}\"; filename=\"{1}\""+CrLf, FieldName, FileName));
Content.Append("Content-Type: " + GetContentType(Format) + CrLf);
//Content.AppendLine("Content-Transfer-Encoding: binary");
Content.Append(CrLf);
MemoryStream BitmapStream = new MemoryStream();
Bmp.Save(BitmapStream, Format);
Byte[] ContentBytes = Encoding.UTF8.GetBytes(Content.ToString());
Byte[] BitmapBytes = BitmapStream.ToArray();
BitmapBytes = ConnectByteArrays(BitmapBytes, Encoding.UTF8.GetBytes(CrLf));//New Line
return ConnectByteArrays(ContentBytes, BitmapBytes);

}

private String GetContentType(System.Drawing.Imaging.ImageFormat Format)
{
System.Diagnostics.Debug.WriteLine("Format='"+Format.ToString()+"', JPEG.format='"+System.Drawing.Imaging.ImageFormat.Jpeg.ToString()+"'");
if (sFormat.Length > 0)
return sFormat;
if (Format.Equals(System.Drawing.Imaging.ImageFormat.Jpeg))
return "image/jpeg";
else if (Format == System.Drawing.Imaging.ImageFormat.Png)
return "image/png";
else if (Format == System.Drawing.Imaging.ImageFormat.Gif)
return "image/gif";
throw new Exception("Unknown ImageFormat!");
}

}

internal static Byte[] ConnectByteArrays(Byte[] B1, Byte[] B2)
{
Byte[] Bytes = new Byte[B1.Length + B2.Length];
B1.CopyTo(Bytes, 0);
B2.CopyTo(Bytes, B1.Length);
return Bytes;
}

}
}

As you can see, I had to change some lines. For whatever reason, the Class function GetContentType was unable to recognize ImageFormat correctly. The compare condition did fail, although the Format was set correctly. As a workaround I used a string to define the format and then used this to identify the format of the attachement to post.
At the top of the code you see the html form code, which is used inside a web browser to post the data. I included it here for reference.

Using a queue for posting data
Another nice implementation is the use of a queue for the data to post. Using this technique I can simply put the files and texts I need to post in a queue and let a background thread do the work. The background thread tries to post the data using the formPost class as long as the application is running.

The background worker class
Here is the thread class I used to work in the background:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Peiler.ServerCommunication;
using System.Windows.Forms;

namespace MDIwatch
{
class myThread
{
//===============================================================
private Queue m_Queue = new Queue();
private bool m_bRunThread = false;
private System.Threading.Thread t2;
private bool m_safeToCloseGUI = true;
#region TEST THREADS
public bool _safeToCloseGUI {
get { lock (this) { return m_safeToCloseGUI; } }
}

public bool bRunThread
{

get { lock (this) { return m_bRunThread; } }
set { lock (this) { m_bRunThread = value; } }
}

private MDIwatch2 m_Form;

public myThread(Queue queueArgs, MDIwatch2 mForm)
{
this.m_Queue = queueArgs;
t2 = new System.Threading.Thread(new System.Threading.ThreadStart(threadProc2));
t2.Priority = System.Threading.ThreadPriority.BelowNormal;
m_Form = mForm;
t2.Start();
}
public void AbortThread(string s)
{
if (s == string.Empty)
s = "Thread abort requested!";
t2.Abort(s);
}
private void updateGUI(string s){
if(bRunThread)
m_Form.Invoke(m_Form.myUpdateTextBox, new object[] { s });
}
private void updateGUIstatus()
{
//just toggle an indicator and show we are still running
if (bRunThread)
m_Form.Invoke(m_Form.myUpdateStatus);

}
private void threadProc2()
{
try
{
postData pData = new postData();
bRunThread = true;
m_safeToCloseGUI = false;
updateGUI("--- Send Thread started ---");
do
{
updateGUIstatus();
//System.Threading.Interlocked.Increment(ref isSending);
while ((m_Queue.Count > 0) && bRunThread)
{
//ICollection syncedCollection = m_Queue;
lock (m_Queue.SyncRoot)// syncedCollection.SyncRoot)
{
pData = (postData)m_Queue.Peek();// get object from queue
}
//System.Threading.Interlocked.Decrement(ref isSending);
//send file now
System.Diagnostics.Debug.WriteLine("### Sending file '" + pData.sFullFileName);
updateGUI("### Sending file '" + pData.sFullFileName + "'");

if (postFormData(pData) == 0) //post and check
{
updateGUI("... file '" + pData.sFullFileName + "' posted");
lock (m_Queue.SyncRoot)
{
pData = (postData)m_Queue.Dequeue(); //remove object from queue
m_Form.Invoke(m_Form.myUpdateTextBox, new object[] { "___ file '" + pData.sFullFileName + "' removed from queue" });
}
}
System.Threading.Thread.Sleep(2000);
updateGUIstatus();
}
System.Threading.Thread.Sleep(2000);

} while (bRunThread);
}
catch (System.Threading.ThreadAbortException tax)
{
System.Diagnostics.Debug.WriteLine("ThreadAbortException: " + tax.Message);
updateGUI("ThreadAbortException: " + tax.Message);
}
finally {
updateGUI("--- Send Thread stopped ---");
System.Diagnostics.Debug.WriteLine("--- Send Thread stopped ---");
lock (this)
{
m_safeToCloseGUI = true;
bRunThread = false;
}
}
}

public void addFile2Queue2(postData pData)
{
//System.Net.HttpWebRequest wr;
//ICollection syncedCollection = m_fiQueue;
lock (m_Queue.SyncRoot)// syncedCollection.SyncRoot
{
m_Queue.Enqueue(pData);
m_Form.Invoke(m_Form.myUpdateTextBox, new object[] { "+++ Added file '" + pData.sFullFileName + "' to Queue" });
}
}

private int postFormData(postData pData)
{
int iRet = 0;
try
{
string theURL = "http://xxx.xxx.xxx.xxx:80/docushare/UploadServlet";

List<Post.IPostAble> Fields = new List<Post.IPostAble>();
Fields.Add(new Post.PostText("summary", pData.sSummary));
Fields.Add(new Post.PostText("description", pData.sDescription));
Fields.Add(new Post.PostPicture(pData.sFullFileName, "datafile", pData.sMimeString)); //System.Drawing.Imaging.ImageFormat.Jpeg));
string HPText /*.Text*/ = Post.ExecutePostCommand(theURL, Fields);
System.Diagnostics.Debug.WriteLine(HPText);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Exception " + ex.Message + "' in postFormData");
m_Form.Invoke(m_Form.myUpdateTextBox, new object[] { "==== Exception " + ex.Message + "' in postFormData ====" });
iRet = -1;
}
return iRet;
}
#endregion
public class postData
{
private string m_sDescription;
private string m_sSummary;
private string m_sFullFileName;
private string m_sMimeString;
public postData()
{
m_sDescription = "Description";
m_sSummary = "Summary";
m_sFullFileName = "FullFileName";
m_sMimeString = "mime/jpeg";
}
/// <summary>
/// provide an object for post data
/// </summary>
/// <param name="sDesc">a description of the post</param>
/// <param name="sSum">a summary about the post</param>
/// <param name="sFullName">an image file to post</param>
public postData(string sDesc, string sSum, string sFullName)
{
m_sDescription = sDesc;
m_sSummary = sSum;
m_sFullFileName = sFullName;
string sExt = System.IO.Path.GetExtension(sFullName);
if (sFullName.EndsWith("jpg", StringComparison.OrdinalIgnoreCase))
m_sMimeString = "image/jpeg";
else if (sFullName.EndsWith("bmp", StringComparison.OrdinalIgnoreCase))
m_sMimeString = "image/bmp";
else if (sFullName.EndsWith("tif", StringComparison.OrdinalIgnoreCase))
m_sMimeString = "image/tiff";
else if (sFullName.EndsWith("gif", StringComparison.OrdinalIgnoreCase))
m_sMimeString = "image/gif";
else
m_sMimeString = "image/unknown";

}
public string sDescription
{
get { return m_sDescription; }
set { m_sDescription = value; }
}
public string sSummary
{
get { return m_sSummary; }
set { m_sSummary = value; }
}
public string sFullFileName
{
get { return m_sFullFileName; }
set { m_sFullFileName = value; }
}
public string sMimeString
{
get { return m_sMimeString; }
set { m_sMimeString = value; }
}
}
}
}

As you can see, I always try different ways to do things. Dont worry about this, I am not a software designer, I try to write working code.

Using Invoke and Delegate

As a worker thread can not update the GUI directly, as the GUI is running on a different thread, one has to use invoke to update the GUI. I included two UpdateGUI functions here: updateGUI and updateGUIstatus.

...
public delegate void UpdateTextBox(string str);
public UpdateTextBox myUpdateTextBox;
public delegate void UpdateStatus();
public UpdateStatus myUpdateStatus;
...
myQueue = new Queue();
mThread = new myThread(myQueue, this);

//new delegate for thread update via Invoke
myUpdateTextBox = new UpdateTextBox(UpdateTextBoxMethod);
//new delegate for thread status invoke
myUpdateStatus = new UpdateStatus(UpdateStatusMethod);
...
public void UpdateTextBoxMethod(string str)
{
this.textBox1.Text = str + "\r\n" + this.textBox1.Text;
showNotification(str);
}
public void UpdateStatusMethod()
{
radioStatus.Checked = !radioStatus.Checked;

}
...

Shutting down the app
At the beginning of the implementation, I was not able to close the app. The worker thread invoked the GUI although the GUI thread already removed the TextFields I used to update the GUI. So I had to implement _safeToCloseGUI. Using this var I could safely close the GUI without the side effect of a worker thread trying to update the already removed GUI elements.

private void MDIwatch_Closing(object sender, CancelEventArgs e)
{
// Signal the BackgroundWorkerThread to end
mThread.bRunThread=false;
System.Threading.Thread.Sleep(2000);

// The _safeToCloseGUI flag will  be true when the
// background worker thread has finished
if (mThread._safeToCloseGUI == false)
{
// The background worker thread has not closed yet,
// so don't close the GUI yet
e.Cancel = true;
}
}

The worker thread and the queue

The worker thread threadProc2 always checks the queue for available data. If there is data, it peeks the next one from the queue and tries to post it. If the data has been posted, the data is removed from the queue. Doing so ensures, that you will not lost any data to send.

The watch dir function

The app has to watch two dirs. Unfortunately, the OpenNetCF FileSystemWatcher class does not support watching for different extensions. So I had to declare 4 FileSystemWatcher objects: for dir one with tif, jpg and bmp and a fourth for dir2. The FileSystemWatcher OnCreated events will then add new post data to the queue:

private void postFile(myThread.postData pData){

mThread.addFile2Queue2(pData);

}

Again, dont blame me for the code design. I hope this code will help you solve similar problems.

[Download not found]

2 Comments

  1. I was very happy to find this site. I wanted to thank you for this informative post.

  2. josef says:

    Hopefully at least one has some usage 😉

    Thank you for letting me know

    ~Josef

Leave a Reply