File I/O Exceptions - c#

i am currently working on a Windows Forms App in c# which will allow the user to add or delete records. when i hit the button to display all the records written to the file the files appear, but when i try to delete by transact number i get an exception saying that "The process cannot access the the because it is being used somewhere else". i have tried putting it in a try-catch block to make sure the reader/writer will close and still not working code will be attached any help is greatly appreciated. p.s im not looking for code to finish this project just help getting by this exception and make it work like it is suppose.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MMFileIO
{
public partial class MMAssignment3 : Form
{
StreamWriter writer;
StreamReader reader;
string record = "";
public MMAssignment3()
{
InitializeComponent();
}
private void MMAssignment3_Load(object sender, EventArgs e)
{
txtFile.Text = #"c:\burnable\assignment3.txt";
if (!Directory.Exists(txtFile.Text.Substring
(0, txtFile.Text.LastIndexOf('\\'))))
MessageBox.Show("File path does not exist");
}
private void btnAdd_Click(object sender, EventArgs e)
{
try
{
if (radNew.Checked)
writer = new StreamWriter(txtFile.Text, append: false);
else
writer = new StreamWriter(txtFile.Text, append: true);
}
catch(Exception ex)
{
if (writer != null)
writer.Close();
MessageBox.Show($"exception adding new record: {ex.Message}");
return;
}
record = $"{txtTransact.Text}::{txtDate.Text}::{txtSerial.Text}::" +
$"{txtToolPurchased.Text}::${txtPrice.Text}::{txtQty.Text}::" +
$"${txtAmount.Text}";
try
{
writer.WriteLine(record);
lblMessage.Text = ($"Record added");
txtTransact.Text = txtDate.Text = txtSerial.Text =
txtToolPurchased.Text = txtPrice.Text = txtQty.Text =
txtAmount.Text = "";
}
catch(Exception ex)
{
MessageBox.Show($"exception adding a new record: {ex.Message}");
}
finally
{
writer.Close();
}
}
private void btnDelete_Click(object sender, EventArgs e)
{
reader = new StreamReader(txtFile.Text);
List<string> records = new List<string>();
while (! reader.EndOfStream)
{
record = reader.ReadLine();
records.Add(record);
}
if (records.Count == 0)
MessageBox.Show("No records left in file");
reader.Close();
writer = new StreamWriter(txtFile.Text, append: false);
foreach (string item in records)
{
}
}
private void btnCloseFile_Click(object sender, EventArgs e)
{
txtDataDisplay.Text = "";
reader.Close();
}
private void btnDisplay_Click(object sender, EventArgs e)
{
reader = new StreamReader(txtFile.Text);
txtDataDisplay.Text = $"{"#".PadRight(10)}\t" +
$"{"Purchase-Date".PadRight(10)}\t{"Serial #".PadRight(10)}\t" +
$"{"Manufacturing Tools".PadRight(10)}\t{"Price".PadRight(10)}\t" +
$"{"Qty".PadRight(10)}\t{"Amount".PadRight(10)}\n{"".PadRight(50)}\n";
while (!reader.EndOfStream)
{
record = reader.ReadLine();
string[] fields = record.Split(new string[] { "::" }, StringSplitOptions.None);
txtDataDisplay.Text += $"{fields[0].PadRight(10)}\t" +
$"{fields[1].PadRight(10)}\t{fields[2].PadRight(10)}\t" +
$"{fields[3].PadRight(30)}\t{fields[4].PadRight(10)}\t" +
$"{fields[5].PadRight(10)}\t{fields[6].PadRight(10)}\n";
}
reader.Close();
}

Related

Incorrect reading of an array

I am working in c-sharp .net and I am working on creating a login screen for my project. I have a text file with login info for all the users. My c-sharp scrip reads that file to a string then cuts it up into two lists, _usernames and _passwords. When the user types their login info and hits login the _usernames[0] and _passwords[0] account info are the only ones that work. What I want it to do is look through all the _usernames for the inputted one, if it finds it then check the _password[same index as _usernames] and if both are the same as what the user submitted then it will add "true" to the richTextBox.
Why is it not correctly reading from the array?
This is my users.txt:
admin,test|
andrew,yeet|
zana,happy|
This is my c-sharp script:
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace BaseUserInterfase
{
public partial class Login : Form
{
string path = Directory.GetCurrentDirectory() + "\\data\\users.txt";
string data;
string[] _usernames = new string[10];
string[] _passwords = new string[10];
public Login()
{
InitializeComponent();
}
private void Login_Load(object sender, EventArgs e)
{
GetLoginData();
}
private void btnLogin_Click(object sender, EventArgs e)
{
rtbTemp.Text = "";
for(int i = 0; i < _usernames.Length; i++)
{
if(_usernames[i] == null)
{
break;
}
rtbTemp.AppendText("\n" + _usernames[i]);
rtbTemp.AppendText("\n" + tbUsername.Text.ToString());
rtbTemp.AppendText("\n" + _passwords[i]);
rtbTemp.AppendText("\n" + tbPassword.Text.ToString());
if (_usernames[i] == tbUsername.Text.ToString())
{
rtbTemp.AppendText("\nUsername true");
if (_passwords[i] == tbPassword.Text.ToString())
{
rtbTemp.AppendText("\nPassword true");
rtbTemp.AppendText("\ntrue");
return;
}
}
}
rtbTemp.AppendText("\nfalse");
}
public void GetLoginData()
{
using (StreamReader streamReader = new StreamReader(path, Encoding.UTF8))
{
data = streamReader.ReadToEnd();
}
List<string> _data = data.Split('|').ToList();
_data.RemoveAt(_data.Count - 1);
rtbTemp.AppendText("\n");
Array.Resize<string>(ref _usernames, _data.Count);
Array.Resize<string>(ref _passwords, _data.Count);
foreach (string _item in _data)
{
List<string> userdata = _item.Split(',').ToList();
string username = userdata[0].ToString();
string password = userdata[1].ToString();
Console.WriteLine(_item);
Console.WriteLine(username);
Console.WriteLine(password);
for(int i = 0; i < _data.Count; i++)
{
if(_usernames[i] == null)
{
_usernames[i] = username;
_passwords[i] = password;
break;
}
}
}
}
private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
And this is an image of the login screen:
1
Your file contains carriage return characters. Remove them before you split its content
data = streamReader.ReadToEnd().Replace("\r\n", "");

No Book Results from ISBNDB with Valid ISBN

I've got a weird issue here. I'll start by explaining my program:
I have a C# application. The main goal of the program is to get information about a book based on its ISBN. The ISBN is passed to the program via a TCP/IP scanner on an Android device. The ISBN is then put into a valid URL which is used to grab the XML data from ISBNDB.com.
The issue that I am having is this:
When I query an ISBN typed into a TextBox, the program works fine. When I query an ISBN scanned from the reader, it returns 'No Results'
I have implemented various ways to try and get to the bottom of this case. Right before the XML is read, I have a message box show me the XML that it received:
As you can see, it shows no results. However, when I visit the URL (Also gotten from within the program):
I get this in Microsoft Edge:
Which, is exactly what I would think the application would get as well.
Does anyone know what is going on? If so, what can I do to fix it and how can my code be improved to eliminate this error?
For those interested, here is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Threading;
using System.Diagnostics;
namespace LibraryBookLister
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string XML = "";
private void btnQuery_Click(object sender, EventArgs e)
{
GetXMLBarcodeData();
}
private void GetXMLBarcodeData()
{
string Barcode4 = textBarcode.Text;
MessageBox.Show(Barcode4);
string barcode = Barcode4;
StringBuilder output = new StringBuilder();
XmlUrlResolver resolver = new XmlUrlResolver();
resolver.Credentials = System.Net.CredentialCache.DefaultCredentials;
// Set the reader settings object to use the resolver.
if(barcode.Length > 13)
{
barcode = barcode.Remove(14);
MessageBox.Show(barcode);
}
string xmlString = #"?access_key=IDC057UX&results=details&index1=isbn&value1=" + barcode;
MessageBox.Show("GEttting book info for : " + barcode);
Uri baseUri = new Uri("https://isbndb.com/api/books.xml");
Uri fulluri = resolver.ResolveUri(baseUri, xmlString);
MessageBox.Show("Now Getting The URL: " + fulluri.ToString());
Process.Start(fulluri.ToString());
StringBuilder sb = new StringBuilder();
XmlReader readesr = XmlReader.Create(fulluri.ToString());
MessageBox.Show("REading data from " + fulluri.ToString());
while (readesr.Read())
{
sb.AppendLine(readesr.ReadOuterXml());
}
string XMLs = sb.ToString();
XML = XMLs;
MessageBox.Show("XML : " + XML);
GetXMLStuff();
}
public void GetXMLStuff()
{
tcplistener.Stop();
XmlDocument doc = new XmlDocument();
doc.LoadXml(XML);
XmlNodeList nodes = doc.DocumentElement.SelectNodes("/ISBNdb/BookList");
List<Book> books = new List<Book>();
foreach (XmlNode node in nodes)
{
Book book = new Book();
try
{
if (node.SelectSingleNode("BookData/AuthorsText").InnerText == null)
{
MessageBox.Show("Could not find this book. Please enter data by hand.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
textBarcode.Clear();
return;
}
}
catch
{
MessageBox.Show("Could not find this book. Please enter data by hand.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
// textBarcode.Clear();
return;
}
book.author = node.SelectSingleNode("BookData/AuthorsText").InnerText;
book.title = node.SelectSingleNode("BookData/Title").InnerText;
book.ISBN = node.SelectSingleNode("BookData").Attributes["isbn"].Value;
books.Add(book);
MessageBox.Show(book.author);
addInfo(book.author, book.title, book.ISBN);
textBarcode.Clear();
}
// MessageBox.Show("Total books: " + books.Count);
}
private void addInfo(string Author, string Title, string ISBN)
{
textAuthor.Text = Author;
textTitle.Text = Title;
textISBN.Text = ISBN;
}
class Book
{
public string ISBN;
public string title;
public string author;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
int time = 10;
bool cancel = false;
private void timer1_Tick(object sender, EventArgs e)
{
if(time > 0)
{
labelTime.Text = time.ToString();
button1.Text = "Change Data";
cancel = true;
labelTime.Visible = true;
time--;
// MessageBox.Show(time.ToString());
}
if(time <= 0)
{
cancel = false;
button1.Text = "Add to List";
timer1.Stop();
time = 10;
labelTime.Visible = false;
MessageBox.Show("Submitting");
}
}
private void button1_Click(object sender, EventArgs e)
{
if(cancel)
{
timer1.Stop();
labelTime.Visible = false;
time = 10;
cancel = false;
button1.Text = "Add to List";
}
else
{
timer1.Start();
}
}
private void button2_Click(object sender, EventArgs e)
{
Thread tcpServer = new Thread(new ParameterizedThreadStart(TCPServerRun));
//TCPServerRun();
tcpServer.Start();
}
bool on = true;
TcpListener tcplistener = new TcpListener(IPAddress.Any, 5004);
private void TCPServerRun(object test)
{
try
{
MessageBox.Show("Starting Listener");
tcplistener.Start();
}
catch { MessageBox.Show("COULDNT START TPCSERVER"); return; }
while (on == true)
{
try
{
TcpClient client = tcplistener.AcceptTcpClient();
Thread tcpHandlerThread = new Thread(new ParameterizedThreadStart(tcpHandler));
// tcpHandlerThread.Start(client);
tcpHandler(client);
}
catch
{
tcplistener.Stop();
// MessageBox.Show("Stopping Listener");
}
}
}
string bCode = "";
private void tcpHandler(object client)
{
TcpClient mClient = (TcpClient)client;
NetworkStream stream = mClient.GetStream();
byte[] message = new byte[1024];
stream.Read(message, 0, message.Length);
bCode = Encoding.ASCII.GetString(message);
stream.Close();
mClient.Close();
MessageBox.Show(bCode);
this.textBarcode.Text = bCode;
GetXMLBarcodeData();
}
}
}
Possible Hint: Could it have something to do with how I have threads working?
*Edit: * **I have updated the code to have the barcode be put in a textBox and then used to fetch the data. This does not seem to work either because it 'Cannot access the control on a thread other than on which it was created'
If manual user input succeed while automated input fail, the simplest hack is just replacing the automated input to a call to manual control BeginInvoke. For your code this would be :
textBarcode.BeginInvoke(new Action(() => {
textBarcode.Text = bCode;
GetXMLBarcodeData();
}));

Auto find files, pass to connection string and read

Firstly, I would just like to add that I am an absolute beginner at C#, I'm doing my best to learn so I am sorry if this is a noob question, but I have hit a brick wall.
I am working on a program that when finished it will find .DBF files from a specified folder, read the file and insert into a mysql database. I am stuck pretty much at the first hurdle.
I am trying to make the program loop through each file it finds and read them.
I can't seem to be able to access the filename string from the GetFiles() Void.
Is there another way around passing the filename to the queryString rather than specifying it myself?
here is my code -
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.OleDb;
namespace WindowsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//OPEN PROGRAM
private void Form1_Load(object sender, EventArgs e)
{
this.richTextBox1.Text = "Waiting for commands...";
this.toolStripStatusLabel1.Text = "Waiting for commands...";
}
// FIND FILES BUTTON CLICK
private void button1_Click(object sender, EventArgs e)
{
this.richTextBox1.Text = "Looking for files...";
GetFiles();
}
// function to read files at source
private void GetFiles()
{
List<String> Myfiles = new List<string>();
string[] allFiles = System.IO.Directory
.GetFiles(#"C:\Users\74-des\Desktop\", "*.DBF");
if (allFiles.Length > 0)
{
try
{
foreach (string filename in allFiles)
{
this.richTextBox1.Text = string.Join(Environment.NewLine,allFiles);
string filenameWithoutPath = Path.GetFileName(filename);
}
}
catch (SystemException excpt)
{
this.richTextBox1.Text = excpt.Message;
}
}
}
private void ReadData()
{
this.toolStripStatusLabel1.Text = "Preparing To Read Data";
this.Refresh();
DirectoryInfo dir = new DirectoryInfo(#"C:\Users\74-des\Desktop\");
string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + dir + ";Extended Properties=dBase IV";
string queryString = "SELECT * FROM " + "test4.DBF";
try
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
OleDbCommand command = new OleDbCommand(queryString, connection);
connection.Open();
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
if (reader.IsDBNull(1))
{
this.richTextBox1.Text = "Null";
}
else
{
string DATE = reader.GetValue(0).ToString();
string TIME = reader.GetValue(1).ToString();
string CODE = reader.GetValue(2).ToString();
string item = reader.GetValue(3).ToString();
this.richTextBox1.Text = DATE + TIME + " " + CODE + " " + item;
this.Refresh();
}
}
reader.Close();
connection.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button2_Click(object sender, EventArgs e)
{
ReadData();
}
}
}
you can do the following
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.OleDb;
namespace WindowsApplication4
{
public partial class Form1 : Form
{
private string mDirectory; // this will hold the directory path you are working on
private string[] mFiles; // this will hold all files in the selected directory
public Form1()
{
InitializeComponent();
}
//OPEN PROGRAM
private void Form1_Load(object sender, EventArgs e)
{
this.richTextBox1.Text = "Waiting for commands...";
this.toolStripStatusLabel1.Text = "Waiting for commands...";
}
// FIND FILES BUTTON CLICK
private void button1_Click(object sender, EventArgs e)
{
this.richTextBox1.Text = "Looking for files...";
GetFiles();
}
// function to read files at source
private void GetFiles()
{
mDirectory = #"C:\Users\74-des\Desktop\"; // better to use OpenFolderDialog to choose the directory
mFiles = System.IO.Directory.GetFiles(mDirectory, "*.DBF");
if (mFiles.Length > 0)
{
try
{
foreach (string filename in mFiles)
{
this.richTextBox1.Text = string.Join(Environment.NewLine, mFiles);
string filenameWithoutPath = System.IO.Path.GetFileName(filename);
}
}
catch (SystemException excpt)
{
this.richTextBox1.Text = excpt.Message;
}
}
}
private void ReadData()
{
this.toolStripStatusLabel1.Text = "Preparing To Read Data";
this.Refresh();
string connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=dBase IV", mDirectory);
try
{
foreach (var file in mFiles)
{
string queryString = string.Format("SELECT * FROM " + "{0}.DBF", System.IO.Path.GetFileNameWithoutExtension(file));
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
OleDbCommand command = new OleDbCommand(queryString, connection);
connection.Open();
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
if (reader.IsDBNull(1))
{
this.richTextBox1.Text = "Null";
}
else
{
string DATE = reader.GetValue(0).ToString();
string TIME = reader.GetValue(1).ToString();
string CODE = reader.GetValue(2).ToString();
string item = reader.GetValue(3).ToString();
this.richTextBox1.Text = DATE + TIME + " " + CODE + " " + item;
this.Refresh();
}
}
reader.Close();
connection.Close();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button2_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(mDirectory))
MessageBox.Show("You should Get Files first!");
else
ReadData();
}
}
}
hope it will help you

Need help making and waiting for a reocurring event

I am having multiple problems, there are going to be multiple usernames and passwords in the listbox...
I need the application to wait while and account is in the text boxes, and then when the textboxes are clear then the account next in the listbox can go to the next line..
Here is the code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net.Mail;
using System.Net;
using System.Text.RegularExpressions;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnOpenFile_Click(object sender, EventArgs e)
{
this.lbAccounts.Items.Clear();
OpenFileDialog Open = new OpenFileDialog();
Open.Title = "Select Email List";
Open.Filter = "Text Document|*.txt|All Files|*.*";
try
{
Open.ShowDialog();
StreamReader Import = new StreamReader(Convert.ToString(Open.FileName));
while (Import.Peek() >= 0)
lbAccounts.Items.Add(Convert.ToString(Import.ReadLine()));
}
catch (Exception)
{
}
}
private void SendTestMail()
{
try
{
NetworkCredential loginInfo = new NetworkCredential(txtUser.Text, txtPass.Text);
MailMessage msg = new MailMessage();
msg.From = new MailAddress(txtUser.Text);
msg.To.Add(new MailAddress("example#gmail.com"));
msg.Subject = "Hi, I'm Valid :D";
msg.Body = txtUser.Text + ":" + txtPass.Text;
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = loginInfo;
client.Send(msg);
lbGoodAccounts.Items.Add(txtUser.Text + ":" + txtPass.Text);
txtUser.Clear();
txtPass.Clear();
}
catch (Exception)
{
lbFailAccounts.Items.Add(txtUser.Text + ":" + txtPass.Text);
txtUser.Clear();
txtPass.Clear();
}
}
private void btnCheck_Click(object sender, EventArgs e)
{
SendTestMail();
}
private void btnExport_Click(object sender, EventArgs e)
{
StreamWriter Write;
SaveFileDialog Open = new SaveFileDialog();
try
{
Open.Filter = ("Text Document|*.txt|All Files|*.*");
Open.FileName = ("Good Gmail Accounts");
Open.ShowDialog();
Write = new StreamWriter(Open.FileName);
for (int I = 0; I < lbGoodAccounts.Items.Count; I++)
{
Write.WriteLine(Convert.ToString(lbGoodAccounts.Items[I]));
}
Write.Close();
}
catch (Exception ex)
{
MessageBox.Show(Convert.ToString(ex.Message));
return;
}
}
}
}
If I understood you right this is the code you will need :
void AccountsListBox_Click (object sender, EventArgs args)
{
if (AccountsListBox.SelectedIndex < 0)
return;
if (!string.IsNullOrEmpty(TextBoxName.Text) && !string.IsNullOrEmpty(TextBoxPass.Text))
return; // stop processing if they have some data
// otherwise get acccount:password,
// split it into acc and pass and write them to TextBoxes
string value = AccountsListBox.Items[AccountsListBox.SelectedIndex];
string[] values = value.Split(':');
if (values.Length != 2)
return; // wrong format
var acc = values[0];
var pass = values[1];
TextBoxName.Text = acc;
TextBoxPass.Text = pass;
}
I haven't used WinForms for some time so if you find some errors please correct them :)
See MSDN ListBox documentation for details.

InvalidOperationException using PFX w/ Windows Forms

I have two exceptions here. Not sure why they occur because I use Form.Invoke to run UI updates on the UI thread. So first,
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Xml;
using System.Windows.Forms;
namespace Toplr
{
using System.Collections.Specialized;
using System.Xml.XPath;
using System.Xml.Linq;
using System.Text;
using System.ServiceModel.Web;
using System.ServiceModel.Syndication;
using System.Net;
using System.Web;
using System.Xml.Schema;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
public partial class ToplrForm : Form
{
private readonly Uri SearchBase = new Uri(#"http://www.twine.com/feed/atom/entries/");
private readonly UriTemplate SearchTemplate = new UriTemplate(#"search?type={type}&author={author}");
public ToplrForm()
{
InitializeComponent();
Exiting = false;
TaskContext = new TaskManager();
Items = new AsyncBindingList<Twine>(this);
twineBindingSource.DataSource = Items;
}
private void ToplrForm_Load(object sender, EventArgs e)
{
}
private readonly TaskManager TaskContext;
private readonly AsyncBindingList<Twine> Items;
private bool Exiting;
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("Close()");
Close();
}
private void ToplrForm_FormClosing(object sender, FormClosingEventArgs e)
{
MessageBox.Show("Exiting = tru");
Exiting = true;
//TaskContext.Dispose();
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
var sfd = new SaveFileDialog()
{
ValidateNames = true
};
if (sfd.ShowDialog() == DialogResult.OK)
{
using (var xtw = new XmlTextWriter(sfd.FileName, Encoding.UTF8))
{
var xw = XmlWriter.Create(xtw);
xw.WriteStartDocument();
xw.WriteStartElement("opml");
xw.WriteAttributeString("version", "1.1");
xw.WriteStartElement("head");
xw.WriteElementString("title", userNameComboBox.Text);
xw.WriteEndElement();
xw.WriteStartElement("body");
foreach (var row in twineDataGridView.SelectedRows)
{
var twine = (Twine)((DataGridViewRow)row).DataBoundItem;
if (twine != null)
{
xw.WriteStartElement("outline");
xw.WriteAttributeString("text", twine.Title);
xw.WriteAttributeString("type", "link");
xw.WriteAttributeString("url", twine.HtmlAddress);
xw.WriteStartElement("outline");
xw.WriteAttributeString("text", twine.Title);
xw.WriteAttributeString("type", "atom");
xw.WriteAttributeString("url", twine.AtomAddress);
xw.WriteEndElement();
xw.WriteEndElement();
}
}
xw.WriteEndElement();
xw.WriteEndElement();
xw.WriteEndDocument();
xw.Close();
}
}
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("Copyright (C) 2009 Bent Rasmussen");
}
private void accessButton_Click(object sender, EventArgs e)
{
var user = userNameComboBox.Text;
Task.Create(x => ProcessAccount(user));
}
public void ProcessAccount(string user)
{
this.Invoke((Action)(() =>
{
userNameComboBox.Enabled = false;
accessButton.Enabled = false;
toolStripStatusLabel1.Text = "Processing...";
}));
var param = new NameValueCollection();
param.Add("type", "Twine");
param.Add("author", user);
var source = SearchTemplate.BindByName(SearchBase, param);
var wc = new WebClient();
using (var feedStream = wc.OpenRead(source))
{
var reader = XmlReader.Create(feedStream);
var feed = SyndicationFeed.Load(reader);
int c = 0, i = 0;
foreach (var item in feed.Items)
{
this.Invoke((Action)(() =>
{
toolStripProgressBar1.Increment(1);
toolStripStatusLabel1.Text = "Processing...";
}));
if (item.Links.Count != 0)
{
//try
{
ProcessTwine(item);
i++;
}
//catch (Exception)
{
c++;
}
}
if (Exiting)
break;
}
}
this.Invoke((Action)(() =>
{
userNameComboBox.Enabled = true;
accessButton.Enabled = true;
}));
}
private Twine ProcessTwine(SyndicationItem item)
{
var result = new Twine();
result.Title = item.Title.Text;
result.HtmlAddress = item.Links[0].Uri.ToString();
result.AtomAddress = "";
var wc = new WebClient();
var data = wc.DownloadData(result.HtmlAddress);
var stream = new MemoryStream(data);
var readerSettings = new XmlReaderSettings()
{
ProhibitDtd = false,
ValidationType = ValidationType.None,
ValidationFlags = XmlSchemaValidationFlags.None,
};
var reader = XmlReader.Create(stream, readerSettings);
var doc = XDocument.Load(reader);
var htmlNs = (XNamespace)"http://www.w3.org/1999/xhtml";
var root = doc.Root;
var atom = from r in root.Descendants(htmlNs + "head").Descendants(htmlNs + "link")
where r.Attribute("rel").Value == "alternate" && r.Attribute("type").Value == "application/atom+xml"
select r.Attribute("href");
foreach (var e in atom)
{
if (e.Value != "")
{
result.AtomAddress = e.Value;
this.BeginInvoke((Action)(() =>
{
Items.Add(result);
toolStripProgressBar1.Increment(1);
}));
}
break;
}
return result;
}
}
}
This triggers the exception "Cannot access a disposed object" on this fragment
this.Invoke((Action)(() =>
{
toolStripProgressBar1.Increment(1);
toolStripStatusLabel1.Text = "Processing...";
}));
If this fragment is commented out, I run into the next problem - a TargetInvocationException on Program level.
The inner exception of this is an InvalidOperationException.
The code is quite simple, so it should not be hard to implement this, I just new a few hints to move on.
Visual Studio project files.
If the user hits the exit button, the Close() method is called, and the UI starts getting torn down. However, your worker code keeps running and attempts to update the UI, which it can no longer do.
If you centralise all those invoke calls:
public void UpdateUI(Action action) {
if(!Exiting) this.Invoke(action);
}
you can call:
UpdateUI(() =>
{
toolStripProgressBar1.Increment(1);
toolStripStatusLabel1.Text = "Processing...";
});
(etc - all the this.Invoke calls should use UpdateUI instead)
and it should work. Also, make Exiting volatile.

Categories

Resources