I'm facing another problem, my application downloads a file from a web, extracts it, deletes it and so on, it runs fine for the first run, then when it comes to downloading the next file it simply freezes the download and hangs there forever.. It's probably something with trying to open an already open connection, but I have no idea how to close it, this is my first time networking with C# and I'm self teaching.
My code:
public void start() {
if (File.Exists("Data/version.txt")) { File.Delete("Data/version.txt"); }
label1.Text = "Getting update information...";
WebClient webClient = new WebClient();
webClient.DownloadFileAsync(new Uri("http://127.0.0.1/update/version.txt"), #"Data/version.txt");
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(versionCompleted);
}
private void versioncheck() {
if (File.Exists("Main.exe"))
{
label1.Text = "Contacting update server...";
var versionInfo = FileVersionInfo.GetVersionInfo("Main.exe");
string version = versionInfo.ProductVersion;
string[] nversion = version.Split('.');
string updateversion = nversion[3];
int version = Int32.Parse(updateversion);
///////////////////////////////////////////////////////////
StreamReader sr = new StreamReader("Data/version.txt");
///////////////////////////////////////////////////////////
string readline = sr.ReadLine();
sr.Dispose();
int serverversion = Int32.Parse(readline);
if (serverversion > version) {
string filenumber = (version+1).ToString();
downloadfile(filenumber);
}
else if(serverversion == version){
label1.Text = "Game is up to date!";
startButton.Enabled = true;
}
}
else { MessageBox.Show("Unexpected Error!", "Error!"); }
}
private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
private void Completed(object sender, AsyncCompletedEventArgs e)
{
label1.Text = "Extracting Files...";
var versionInfo = FileVersionInfo.GetVersionInfo("Main.exe");
string version = versionInfo.ProductVersion;
string[] nversion = version.Split('.');
string updateversion = nversion[3];
int version = Int32.Parse(updateversion); string nversion = (version + 1).ToString();
Process proc = Process.Start("update"+nversion+".exe"); // extract in the silent mode
proc.WaitForExit();
File.Delete("update" + nversion + ".exe");
label1.Text = "Checking for more updates...";
versioncheck();
}
private void versionCompleted(object sender, AsyncCompletedEventArgs e) {
versioncheck();
}
private void downloadfile(string filenumber)
{
try
{
//MessageBox.Show("Download working");
System.Net.WebClient webClient = new System.Net.WebClient();
webClient.OpenRead("http://127.0.0.1/Update/update" + filenumber + ".exe");
Int64 bytes_total = Convert.ToInt64(webClient.ResponseHeaders["Content-Length"]);
string updatelength = Convert.ToString((bytes_total / 1024).ToString());
label2.Text = "File size:" + updatelength + "KB";
////////////////////////////////////////////////////
label1.Text = "Downloading Update...";
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
webClient.DownloadFileAsync(new Uri("http://127.0.0.1/Update/update" + filenumber + ".exe"), #"update"+filenumber+".exe");
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
webClient.Dispose();
}
catch (WebException )
{
MessageBox.Show("Connection error!", "Error!");
Application.Exit();
}
catch (IOException)
{
MessageBox.Show("Unknown Error!", "Error!");
Application.Exit() ;
}
}
If u use async operation u gonne need to use the async and await keywords.
U need to dispose or close your webclient when it is done downloading or failing.
As b1tsh1ft stated best thing is to use the using statement.
string version = versionInfo.ProductVersion;
string[] nversion = version.Split('.');
int version = Int32.Parse(updateversion);
string nversion = (version + 1).ToString();
Isn't your VS not giving a conflicting error in your editor about this ?
First you should put WebClient in a using block because it implements IDisposable
using(var webClient = new WebClient())
{
// do work here
webClient.DownloadFile(..)
}
Don't use the async version. An exception is likely being thrown and is lost on the other thread. Test and make it work regularly first.
Also put your StreamReader (or anything implementing IDisposable) in a using() statement. It is more reliable then manually calling dispose because it disposes even on failure.
Related
good evening all,
i been working on a little downloader for my program only downloads the python installer small little things will be adding more over time,
however i have a issue when i run it in debug it works downloads fine
when i do it in the debug folder and run the exe without visual studio it load the update box but creates a file with 0 bytes and says when trying to load,
this is not compatible with your os
code i have.
private string GetFileName(string url)
{
string[] parts = url.Split('/');
string fileName = "";
if (parts.Length > 0)
fileName = parts[parts.Length - 1];
else
fileName = url;
return fileName;
}
private String getFileName(string url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
string fileName = "";
try
{
HttpWebResponse res = (HttpWebResponse)request.GetResponse();
using (Stream rstream = res.GetResponseStream())
{
fileName = res.Headers["Content-Disposition"] != null ?
res.Headers["Content-Disposition"].Replace("attachment; filename=", "").Replace("\"", "") :
res.Headers["Location"] != null ? Path.GetFileName(res.Headers["Location"]) :
Path.GetFileName(url).Contains('?') || Path.GetFileName(url).Contains('=') ?
Path.GetFileName(res.ResponseUri.ToString()) : GetFileName(url);
}
res.Close();
}
catch { }
return fileName;
}
public async Task downloadupdate()
{
if(os == "x86")
{
filename = getFileName("https://www.python.org/ftp/python/3.11.0/python-3.11.0-amd64.exe");
string url = "https://www.python.org/ftp/python/3.11.0/python-3.11.0-amd64.exe";
lblfilename.Text = "File Name: " + getFileName(url);
using (WebClient wc = new WebClient())
{
wc.DownloadFileCompleted += wc_completed;
wc.DownloadProgressChanged += wc_DownloadProgressChanged;
XtraMessageBox.Show(filename);
filename = "python.exe";
wc.DownloadFileAsync(new Uri(url), "C://Temp" + "//" + filename);
}
}
else
{
filename = getFileName("https://www.python.org/ftp/python/3.11.0/python-3.11.0.exe");
string url = "https://www.python.org/ftp/python/3.11.0/python-3.11.0.exe";
lblfilename.Text = "File Name: " + getFileName(url);
using (WebClient wc = new WebClient())
{
wc.DownloadFileCompleted += wc_completed;
wc.DownloadProgressChanged += wc_DownloadProgressChanged;
wc.DownloadFileAsync(new Uri(url), "C://Temp" + "//" + filename);
}
}
}
private void wc_completed(object sender, AsyncCompletedEventArgs e)
{
_MessageResult.Instance.Append("Download Completed!");
//unzipupdate(Tiaga_Res.unzipupdate = "fixes19.zip");
//DirectoryCopy(Tiaga_Res.startpath + "\\fixes19\\", Tiaga_Res._bo3_root, true);
//XtraMessageBox.Show("Applied Fix Aug And Reflex Sight Fix");
// richTextBox1.Text = _MessageResult.Instance.Merge();
Process.Start("C://Temp" + "//" + filename);
this.Close();
}
void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
progressBarControl1.Properties.Step = 1;
progressBarControl1.Properties.PercentView = true;
//progressBarControl1.Properties.Maximum = e.ProgressPercentage;
progressBarControl1.PerformStep();
UpdateProgressBar(e.ProgressPercentage);
txtpercent.Text = e.ProgressPercentage + "%";
double bytesIn = double.Parse(e.BytesReceived.ToString());
double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
lbldownloaded.Text = "Downloaded: " + bytesIn + "/" + totalBytes;
}
private void UpdateProgressBar(int value)
{
progressBarControl1.EditValue = value;
}
screen shot of in debug.
any help would be much appreciated i cant see it would be a libz issue i used that since day one to compile dlls into my project never had this issue before
I have a program, that has to comunicate with another program that is called from the first.
I have managed to send the required data from the first to the second program using NamedPipes.
When the second program closes, i need to send some data back to the first program. I set up a NamedPipeServerStream in the first program and from the Closing event of the second program a NamedPipeClientStream. Now when i try to write from ClientStream i get a broken pipe error.
This is the code from the first program:
private void CreateOverlay(object sender, EventArgs e)
{
if (oinstallfolder != null)
{
string processfilename = oinstallfolder.ToString() + "\\SecondProgram.exe";*/
string processfilename = "SecondProgram.exe";
if (File.Exists(processfilename))
{
foreach (Process clsProcess in Process.GetProcesses())
{
if (clsProcess.ProcessName.Equals("SecondProgram"))
{
this.threadHandleOverlayBounds = new Thread(new ThreadStart(this.ExchangeMapOverlayBoundsServer));
this.threadHandleOverlayBounds.Start();
this.threadHandleOverlayFile = new Thread(new ThreadStart(this.ExchangeMapOverlayFileServer));
this.threadHandleOverlayFile.Start();
ShowWindow(clsProcess.MainWindowHandle, SW_SHOWNORMAL);
SetForegroundWindow(clsProcess.MainWindowHandle);
return;
}
}
try
{
this.threadHandleOverlayBounds = new Thread(new ThreadStart(this.ExchangeMapOverlayBoundsServer));
this.threadHandleOverlayBounds.Start();
Logger.Logger.Write("Log.txt", "Starting Filethread serverside");
this.threadHandleOverlayFile = new Thread(new ThreadStart(this.ExchangeMapOverlayFileServer));
this.threadHandleOverlayFile.Start();
Process.Start(processfilename);
}
catch { }
}
}
}
private void ExchangeMapOverlayFileServer()
{
try
{
using (NamedPipeServerStream namedPipeServer = new NamedPipeServerStream("OverlayFilePipe", PipeDirection.In, 1, PipeTransmissionMode.Byte))
{
string line;
namedPipeServer.WaitForConnection();
StreamReader sr = new StreamReader(namedPipeServer);
line = sr.ReadLine();
namedPipeServer.Disconnect();
namedPipeServer.Close();
}
}
catch(Exception e)
{
Logger.Logger.Write("OverlayGenerator.txt", "namedPipeServer exception: " + e.Message + "\n" + e.StackTrace);
}
}
this is the code from the second program
this.Closing += (s, a) =>
{
Logger.Logger.Write("Log.txt", "Window Closing");
this.threadHandleOverlayFile = new Thread(new ThreadStart(this.HandleWindowClosing));
this.threadHandleOverlayFile.Start();
while (!done)
{
Thread.Sleep(100);
}
};
private void HandleWindowClosing()
{
if (!String.IsNullOrEmpty(this.latestSavedOverlayPath))
{
try
{
using (NamedPipeClientStream namedPipeClient = new NamedPipeClientStream(".", "OverlayFilePipe", PipeDirection.Out))
{
namedPipeClient.Connect();
StreamWriter sw = new StreamWriter(namedPipeClient);
sw.AutoFlush = true;
sw.WriteLine("testphrase");
namedPipeClient.Close();
this.done = true;
}
}
catch (Exception e) { Logger.Logger.Write("OverlayGenerator.txt", "Exception in Client: " + e.Message + "\n" + e.StackTrace); }
}
else
{
Logger.Logger.Write("OverlayGenerator.txt", "Latest saved OverlayPath = " + this.latestSavedOverlayPath);
}
}
I tried this with Autoflush=true and without. With it, i get a broken pipe exception at the line
sw.WriteLine("testphrase");
and without it, the client side just runs to the end and nothing happens at all.
Where is my mistake?
Thank you.
EDIT
OK, i just don't get it. I made a test program, that is build the same way as the actual one. Two applications, the first starting the second. I start the pipe server in a thread in the first application and the client, also in a thread, in the second. The only difference is, that i run this test project in debug mode, which i can't do for the actual program. Now, in the test program, this matter is realy simple and it works. When i use the exact same approach in the actual program, it doesn't work.
In the testprogram i need to use flush on the streamwriter and i don't get an exception. The syncronization of the threads gets handled by the pipe itself, as it should be if i understood this correctly.
The Master looks like this:
private const string CLIENTPROC = "C:\\Users\\Maik\\Source\\Repos\\PipeTest\\PipeClient\\obj\\x86\\Release\\PipeClient.exe";
private ManualResetEvent threadResetEvent;
private Thread threadHandlePipeSendLast;
private Process procClient;
private string msgPipeSend;
private volatile bool bMsgPipeSend;
public MainWindow()
{
InitializeComponent();
this.threadResetEvent = new ManualResetEvent(false);
System.Diagnostics.Debug.WriteLine("### starting thread");
this.threadHandlePipeSendLast = new Thread(new ThreadStart(this.ExchangeMapOverlayFileServer));
this.threadHandlePipeSendLast.Start();
}
private void ExchangeMapOverlayFileServer()
{
System.Diagnostics.Debug.WriteLine("server thread started");
Thread.CurrentThread.Name = "apocalypse";
try
{
using (NamedPipeServerStream namedPipeServer = new NamedPipeServerStream("ClosingPipe", PipeDirection.In, 1, PipeTransmissionMode.Byte))
{
string line;
namedPipeServer.WaitForConnection();
StreamReader sr = new StreamReader(namedPipeServer);
Thread.Sleep(100);
line = sr.ReadLine();
handleRecvMsgFromPipe(line);
namedPipeServer.Disconnect();
namedPipeServer.Close();
}
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine("### " + e.Message + "\n" + e.StackTrace);
}
}
private void handleRecvMsgFromPipe(string line)
{
this.outbox.Text = line;
}
private void buttonOpenFormClient_Click(object sender, EventArgs e)
{
#if DEBUG
#else
if (this.procClient == null)
{
try
{
this.procClient = Process.Start(new ProcessStartInfo(CLIENTPROC));
}
catch (Exception exc)
{
MessageBox.Show(exc.Message, "Error");
}
}
#endif
}
}
And that's the client side:
private ManualResetEvent threadResetEvent;
private Thread threadHandlePipeSendLast;
private string msgPipeSend;
private volatile bool bMsgPipeSend;
private bool done;
public MainWindow()
{
InitializeComponent();
this.threadResetEvent = new ManualResetEvent(false);
this.Closing += (s, a) =>
{
System.Diagnostics.Debug.WriteLine("+++ FormClosing started.");
this.threadHandlePipeSendLast = new Thread(new ThreadStart(this.HandleWindowClosing));
this.threadHandlePipeSendLast.Start();
while (!done)
{
Thread.Sleep(1000);
}
};
}
private void HandleWindowClosing()
{
try
{
using (NamedPipeClientStream namedPipeClient = new NamedPipeClientStream(".", "ClosingPipe", PipeDirection.Out))
{
namedPipeClient.Connect();
StreamWriter sw = new StreamWriter(namedPipeClient);
//sw.AutoFlush = true;
sw.WriteLine("last message");
namedPipeClient.WaitForPipeDrain();
namedPipeClient.Close();
this.done = true;
}
}
catch (Exception e) { System.Diagnostics.Debug.WriteLine(e.Message + "\n" + e.StackTrace); }
}
Can anyone tell me, why this works in the test app and not in the actual one? I also tried various things from Thread.Sleep(x) at different points in the programs over Thread.Join() to pipe methods like WaitForPipeDrain (which is just ignored).
I am having problem downloading files using Background transfer. After completion of download when moving file, it gives you an exception Operation not permitted
void addTransferRequest(string fileName)
{
if (string.IsNullOrEmpty(fileName))
return;
string filePathToDownload = string.Empty;
filePathToDownload = activeReciter.DownloadURL;
filePathToDownload += fileName;
Uri transferUri = new Uri(Uri.EscapeUriString(filePathToDownload),
UriKind.RelativeOrAbsolute);
BackgroundTransferRequest transferRequest = new
BackgroundTransferRequest(transferUri);
transferRequest.Method = "GET";
transferRequest.TransferPreferences = TransferPreferences.AllowBattery;
Uri downloadUri = new Uri(DataSource.TEMPDOWNLOADLOCATION + fileName,
UriKind.RelativeOrAbsolute);
transferRequest.DownloadLocation = downloadUri;
transferRequest.Tag = fileName;
transferRequest.TransferStatusChanged +=
new EventHandler<BackgroundTransferEventArgs>
(transfer_TransferStatusChanged);
transferRequest.TransferProgressChanged += new
EventHandler<BackgroundTransferEventArgs>(transfer_TransferProgressChanged);
try
{
BackgroundTransferService.Add(transferRequest);
chapterFileNames.Dequeue();
}
catch (InvalidOperationException)
{
}
catch (Exception)
{
}
}
void transfer_TransferStatusChanged(object sender, BackgroundTransferEventArgs e)
{
ProcessTransfer(e.Request);
}
void transfer_TransferProgressChanged(object sender, BackgroundTransferEventArgs e)
{
}
private void ProcessTransfer(BackgroundTransferRequest transfer)
{
switch (transfer.TransferStatus)
{
case TransferStatus.Completed:
if (transfer.StatusCode == 200 || transfer.StatusCode == 206)
{
using (IsolatedStorageFile isoStore =
IsolatedStorageFile.GetUserStoreForApplication())
{
try
{
string filename = transfer.Tag;
string folderPath = string.Format(#"{0}{1}\{2}\",
DataSource.DOWNLOADLOCATION, activeReciter.ReciterID, chapter.ChapterID);
string fileFullPath = folderPath + filename;
if (!isoStore.DirectoryExists(Path.GetDirectoryName(folderPath)))
isoStore.CreateDirectory(Path.GetDirectoryName(folderPath));
if (isoStore.FileExists(fileFullPath))
isoStore.DeleteFile(fileFullPath);
isoStore.MoveFile(transfer.DownloadLocation.OriginalString, fileFullPath);
//Excpetion is thrown here
RemoveTransferRequest(transfer.RequestId);
}
catch (Exception ex)
{
MessageBox.Show("Error Occured: " + ex.Message + transfer.Tag, "Error",
MessageBoxButton.OK);
return;
}
}
}
break;
}
}
When moving file, it throws exception, I don't know what is wrong with moving (this happens on some of the files not all files).
From the MSDN Page, under File System Restrictions section:
You can create any additional directory structure you choose
underneath the root “/shared/transfers” directory, and you can copy or
move files after the transfer is complete to ensure that the
background transfer service does not modify the files, but attempting
to initiate a transfer using a path outside of the “/shared/transfers”
directory will throw an exception.
Make sure you are not trying to move your file outside from the /Shared/Transfers folder.
I am programming an application for downloading articles from an SQL Database on the internet. I have programmed the Website for managing the articles. Now I'm downloading the article List in gzip Format and then I decompress them to a xml-File. When I'm done I want to insert the articles to the mobile phone. This works great. Now I want to add an progress Bar to see the state of the insertion. I tried with Threading but this doesn't work. I'm posting some pieces of code from my application and also the progressUpdate methods.
private void btn_send_Click(object sender, EventArgs e)
{
label1.Text = "Download started";
string ArticlesURL = "URLTOSITE";
InvokeAsync(ArticlesURL);
}
private void InvokeAsync(string URL)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.AllowWriteStreamBuffering = true;
allDone.Reset();
request.BeginGetRequestStream(new AsyncCallback(ReadArticlesCallback), request);
allDone.WaitOne();
request.BeginGetResponse(new AsyncCallback(ResponseArticlesCallback), request);
}
private static void ReadArticlesCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
//End the operation.
Stream postSream = request.EndGetRequestStream(asynchronousResult);
string postData = "articles=test";
//Convert the string into a byte array.
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
//Write to the request stream.
postSream.Write(byteArray, 0, postData.Length);
postSream.Close();
allDone.Set();
}
private static void ResponseArticlesCallback(IAsyncResult asynchronousResult)
{
Form1 f = new Form1();
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse resp = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
Stream streamResponse = resp.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse);
string responseString = streamRead.ReadToEnd();
nbrArticles = Convert.ToInt16(responseString);
// Close the stream object.
streamResponse.Close();
streamRead.Close();
// Release the HttpWebResponse.
resp.Close();
f.truncate_articles();
f.get_articles();
}
private void get_articles()
{
string url = "URLTOSITE";
int startPoint = 0;
DownloadZipFile((object)startPoint, url);
DecompressFile();
getXmlAndInsertInDB();
}
private void getXmlAndInsertInDB()
{
int total = nbrArticles;
int count = total / 100; //How much articles are 1 percent
int i = 0;
String barcode = "";
String name = "";
bool state = false;
XmlTextReader reader = new XmlTextReader("Program Files\\SmartDeviceProject1\\articles.xml");
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element: //The node is an element
while (reader.MoveToNextAttribute()) //Get the attributes like barcode, lastname, firstname, pincode
switch (reader.Name)
{
case "barcode":
barcode = reader.Value.ToString();
state = false;
break;
case "name":
name = reader.Value.ToString();
state = true;
break;
}
break;
}
if (state == true)
{
cmd.CommandText = "INSERT INTO articles(barcode, name) " +
"VALUES('" + barcode + "','" + name + "');";
cmd.ExecuteNonQuery();
state = false;
i++;
if (i == count)
{
Thread t = new Thread(new ThreadStart(this.incrementProgressBar));
t.Start();
//incrementProgressBar();
i = 0;
}
}
}
reader.Close();
}
private void updateProgressBarMethod(int progress)
{
if (progressBar1.InvokeRequired)
{
//It was called from a non UI thread so we create a delegate
//and have the UI Thread call this method again
UpdateProgressBar = new UpdateProgressBarDelegate(updateProgressBarMethod);
this.Invoke(UpdateProgressBar, progress);
}
else
{
//Called from the UI Thread OK to update
//update your progress bar here
progressBar1.Value += progress;
}
}
private void incrementProgressBar()
{
//Call the method to update progress Bar on UI thread
//we do not need a delegate here that will be taken care of
//in the method
updateProgressBarMethod(1);
Application.DoEvents();
}
I think the problem is that I am using Callbacks. I have read that the Callbacks are also starting Threads. So I think the problem is there but I can't solve it.
I've found another very good site for threading with mobile applications: Updating the User Interface from a Worker Thread
Now with the new code, the debugger stops always at the same piece of code without any notification or exception :( Here is my new code:
if (i == count)
{
this.info_percent = "Synchro " + step.ToString() + "%";
this.Invoke(new EventHandler(WorkerUpdate)); //The Debugger stops here!
i = 0;
step++;
Thread.Sleep(700);
}
public void WorkerUpdate(object sender, EventArgs e)
{
this.lbl_percent.Text = this.info_percent;
this.lbl_percent.Update();
this.progressBar1.Value = step;
this.progressBar1.Update();
}
The Debugger stops at: this.Invoke(new EventHandler(WorkerUpdate));
I would suggest using the Background worker class. I had a similar problem and implemented the Background worker and it fixed my problem. Hopefully it will fix yours also
http://www.dotnetperls.com/backgroundworker
http://msdn.microsoft.com/en-us/library/cc221403(v=vs.95).aspx
I found another thread discussing this and thought it would help:
Is there a BackgroundWorker replacement for .NET Compact Framework 3.5?
Your code will always hang at this point:
if (i == count)
{
this.info_percent = "Synchro " + step.ToString() + "%";
this.Invoke(new EventHandler(WorkerUpdate)); //The Debugger stops here!
i = 0;
step++;
Thread.Sleep(700);
}
Make these changes:
public delegate void MethodInvoker(); // this is not defined in CF
if (i == count)
{
this.info_percent = "Synchro " + step.ToString() + "%";
object sender = null; // make this whatever you want/need
EventArgs e = new EventArgs();
if (this.InvokeRequired) {
MethodInvoker mi = delegate { WorkerUpdate(sender, e); } };
this.BeginInvoke(mi);
} else {
WorkerUpdate(sender, e);
}
i = 0;
step++;
// Thread.Sleep(700); Why is this here?
}
This should prevent those obnoxious freezes.
How to access VIP in the proxy_OpenReadCompleted method?
void method1()
{
String VIP = "test";
WebClient proxy = new WebClient();
proxy.OpenReadCompleted += new OpenReadCompletedEventHandler(proxy_OpenReadCompleted);
String urlStr = "someurl/lookup?q=" + keyEntityName + "&fme=1&edo=1&edi=1";
}
void proxy_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
}
There are two approaches to this. First is to pass the string as the second parameter in the OpenReadAsync call, this parameter becomes the value of the UserState property of the event args.
void method1()
{
String VIP = "test";
WebClient proxy = new WebClient();
proxy.OpenReadCompleted += proxy_OpenReadCompleted;
String urlStr = "someurl/lookup?q=" + keyEntityName + "&fme=1&edo=1&edi=1";
proxy.OpenReadAsync(new Uri(urlStr, UriKind.Relative), VIP);
}
void proxy_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
String VIP = (string)e.UserState;
// Do stuff that uses VIP.
}
Another approach is to access the variable directly using a closure:-
void method1()
{
String VIP = "test";
WebClient proxy = new WebClient();
proxy.OpenReadCompleted += (s, args) =>
{
// Do stuff that uses VIP.
}
String urlStr = "someurl/lookup?q=" + keyEntityName + "&fme=1&edo=1&edi=1";
proxy.OpenReadAsync(new Uri(urlStr, UriKind.Relative), VIP);
}
void method1()
{
String VIP = "test";
WebClient proxy = new WebClient();
proxy.OpenReadCompleted += (s,e) => proxy_OpenReadCompleted(s,e,VIP);
String urlStr = "someurl/lookup?q=" + keyEntityName + "&fme=1&edo=1&edi=1";
}
Be aware that if the async callback method writes into a databound variable, you are likely to get a cross-thread exception. You will need to use BeginInvoke() to get back to the UI thread. Here's an example using WCF services, but the principle is the same.
public void examsCallback(IAsyncResult result)
{
try
{
EntityList<ExamEntity> examList = ((IExamService) result.AsyncState).EndGetAllExams(result);
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
foreach (ExamEntity exam in examList)
{
Exams.Add(exam);
}
ItemCount = Exams.Count;
TotalItemCount = Exams.ItemCount;
});
}
catch (Exception ex)
{
ErrorHandler.Handle(ex);
}
}