|
FormClosed event handler is too late to cancel form closing. Try FormClosing[^] event. You can use FormClosingEventArgs.Cancel property to cancel the form closing.
|
|
|
|
|
Hello everybody,
My application uses a ListView control to display a list of files. Everything works fine except that when I set the
View property of the ListView to View.List, the list appears but, here's[^] a screenshot of how it looks. I can't understand,
why the filenames are condensed and appended with a "...". I've searched
google and msdn, but couldn't found a solution for the problem. The Large Icon[^] and Details Views[^]
are working perfectly. Just the list view is misbehaving.
My second problem is regarding a BackgroundWorker. I have a BGWKR that I'm using to perform a database transaction.
Now the problem is that the user may close the form while the transaction is in progress.
To handle this, I added code to the form's closing event handler, to inform the user that a transaction is active and ask
him/her, whether to cancel it. If the user affirms, then a call to BGWKR.CancelAsync() is made. Here's the form_closing event.
private void WndSearchResults_FormClosing(object sender, FormClosingEventArgs e)
{
if (bgDatabaseSearcher.IsBusy)
{
if (MessageBox.Show(Resources.msgSearchActive_WindowClosing, "Question",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
e.Cancel = true;
return;
}
else
{
bgDatabaseSearcher.CancelAsync();
while (bgDatabaseSearcher.IsBusy)
Thread.Sleep(500);
}
}
}
Here's the BGWKR's Do_Work event
private void bgDatabaseSearcher_DoWork(object sender, DoWorkEventArgs e)
{
VFS virtualFS = new VFS();
foreach (ListEntry entry in imagesToSearch)
{
try
{
virtualFS.OpenConnection(Path.Combine(AppSettingsManager.ImagesDirectory, entry.imageDbPath));
foreach (VFSSearchTerm searchTerm in termsToSearch)
{
virtualFS.SearchVFS(searchTerm, searchOptions, SearchResultProcessor, entry.imageID);
if (bgDatabaseSearcher.CancellationPending)
{
virtualFS.CloseConnection(true);
return;
}
}
}
finally
{
virtualFS.CloseConnection(true);
}
}
}
However, the DoWork methods cancels gracefully if CancelAsync() is called from a button's click event handler.
Please help.
Thanks
Excuse me for buttin' in, but I'm interrupt driven.
modified on Monday, April 6, 2009 11:27 PM
|
|
|
|
|
Hi,
1.
there seems to be no easy solution.
This article[^] is probably the best approach, however I never tried it.
2.
I did not understand the problem; also the post is too wide for easy reading.
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
My apologies for the post being too much wide. I've reformatted it now.
Excuse me for buttin' in, but I'm interrupt driven.
|
|
|
|
|
Hi,
much better now.
I still don't see what your problem #2 is, you say it works??
I do have 3 comments:
1. assuming lViewSResultsBrowser is a ListView, you are not allowed to perform a Groups.Add() operation (or any other Control operation) on it since Controls are not thread-safe. Without an InvokeRequired/Invoke construct it may hang your GUI temporarily or forever.
2. IMO all that is required for the cancellation is a simple
if (bgDatabaseSearcher.CancellationPending) break;
since that will bring you in the finally block, hence close the connection and return.
3. I don't particularly like the loop while (bgDatabaseSearcher.IsBusy) Thread.Sleep(500); since it makes your GUI hang for as long as the BGW takes; you have made your GUI thread a slave of your background worker now, so if the BGW never terminates, your app is stuck while the user wanted it to exit. The minimum I would do is impose an upper limit.
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
Hi,
Thanks for your response.
Well, my problem is not that the BGWKR is not working. Infact, it's working perfectly.
My problem is that if I call CancelAsync() from FormClosing event, the Do_Work method never stops
execution and the .IsBusy property never becomes false. At the same time, the code inside the Do_Work
event seems to be jammed. I can say this as the if(CancellationPending) condition in the Do_Work event is never hit.
The very same architecture works flawlessely, it CancelAsync() is called from a button's click event handler.
Your 1st comment: That Groups.Add() call is nothing. I added it for testing purposes while debugging.
So don't worry about that. I know it's not a thread-safe operation and will remove it.
Your 2nd comment: Doing so will only get me out of the inner foreach loop. What about the outer one?
Your 3rd comment: That i'll replace with code to hide the form and then wait till the BGWKR finishes or a deadline is met.
Excuse me for buttin' in, but I'm interrupt driven.
|
|
|
|
|
OK,
here is an hypothesis: maybe CancelAsync does not get forwarded to the BGW (or has no net effect) as long as the GUI thread is still busy; when inside a Button_Click handler, it is free right away; in your Form_Closing handler, you keep the GUI thread occupied with the sleep loop.
suggested experiment: rather than the while-sleep loop, set e.Handled true and see if the CancelAsync now works; if it does, it confirms the hypothesis; I then suggest you don't use CancelAsync, instead create your own boolean variable to communicate from Form_Closing to DoWork.
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
Thanks,
Your hypothesis was perfectly correct. When I removed the while(...)...; loop from the form_closing event, it started working perfectly.
Excuse me for buttin' in, but I'm interrupt driven.
|
|
|
|
|
I'm following this example:
http://www.geekpedia.com/tutorial179_Creating-a-download-manager-in-Csharp.html
it seems like a really straight-forward tutorial, however, the line "webResponse = (HttpWebResponse)webRequest.GetResponse();" always gives a (407) Proxy Authentication Required exception.
What can I do to make my app pick up the same authentication that Internet Explorer uses ?
Note: Internet Explorer LAN settings is set to:
Checked "Use automatic configuration script"
Address: http://xxxxxxxxxxxxxx.yyyyy.zzzzzzz.net/proxy.pac
|
|
|
|
|
|
GetDefaultProxy() sounds like a dream come true since I don't want the user to have to hassle with copy and pasting their proxy IP and port into my "settings" form. Additionally, I don't want the responsibility of storing their details in an encrypted file.
However, GetDefaultProxy() is now antiquated.
Also, using UseDefaultCredentials = true doesn't work.
Is there an alternate solution to using GetDefaultProxy() ? Maybe reading the registry ?
|
|
|
|
|
WebRequest.GetSystemWebProxy() doesn't work either. I still get the 407 error.
i.e.
webRequest = (HttpWebRequest)WebRequest.Create(strURL);
webRequest.Credentials = CredentialCache.DefaultCredentials;
IWebProxy wp = WebRequest.GetSystemWebProxy();
webRequest.Proxy = wp;
|
|
|
|
|
through trial-and-error I finally got it solved.
On my journey I've noticed that MANY people have problems with this, so no doubt this code will be copied plenty:
public string GetWebPage(string strURL)
{
string strResult = "";
HttpWebRequest webRequest;
webRequest = (HttpWebRequest)WebRequest.Create(strURL);
IWebProxy proxy = WebRequest.GetSystemWebProxy();
proxy.Credentials = CredentialCache.DefaultCredentials;
webRequest.Proxy = proxy;
webRequest.Method = "GET";
webRequest.UserAgent = "Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.1;+.NET+CLR+1.1.4322)";
webRequest.Timeout = -1;
HttpWebResponse webResponse;
webResponse = (HttpWebResponse)webRequest.GetResponse();
using (StreamReader sr = new StreamReader(webResponse.GetResponseStream()))
{
strResult = sr.ReadToEnd();
sr.Close();
}
return strResult;
}
|
|
|
|
|
this is also not working boss.M getting error
|
|
|
|
|
I get the Proxy Authentication only when using NUnit; the same code works fine when invoked in the real environment.
Anyway, adding the following lines makes it work in NUnit:
IWebProxy proxy = WebRequest.GetSystemWebProxy();
proxy.Credentials = (System.Net.NetworkCredential)CredentialCache.DefaultCredentials;
request.Proxy = proxy;
|
|
|
|
|
Hi, not sure if you're still struggling with this.
Maybe this could help... I was looking at a solution to call a web service from behind a company firewall, but I didn't want to code any proxy settings nor user credentials in my app. The app should simply use my default IE proxy settings.
I'm using .Net 2.0 and here's the C# code to make it work.
<br />
IWebProxy oProxy = WebRequest.GetSystemWebProxy();<br />
oProxy.Credentials = (System.Net.NetworkCredential)CredentialCache.DefaultCredentials;<br />
"your WS stub".Proxy = oProxy;<br />
Hope this helps.
|
|
|
|
|
Hi all,
I need help regarding how to communicate with OPCServer. I am developing an application in C#. In project Reference I added OPCAutomation 2.0(COM). I am adding the code which I done.
OPCServer MyServer = null;
OPCGroups MyOPCGroups = null;
OPCGroup MYOPCGroup = null;
MyServer = new OPCServer();
MyServer.Connect(ProgID,NODE);
// till here ok
MyOPCGroups =(OPCGroups) MyServer.OPCGroups;
// but when I tried to create the OPCGroups I am getting casting error saying:
// "Unable to cast object of type 'OPCAutomation.OPCServerClass' to type 'OPCAutomation.IOPCGroups'."
MYOPCGroup = (OPCGroup)MyOPCGroups.Add("MyGroup");
I really hope you can help me!
Regards,
Alessandro
|
|
|
|
|
Hi All,
I have a datagridview with some textbox and combobox columns. This shows some data from postgresql.
It works fine bud when im trying to set the DataPropertyName of the combobox column, im getting the error:
System.ArgumentException: The value of DatagridviewComboboxCell is invalid
Here is a part of my code:
NpgsqlCommand com = new NpgsqlCommand("SELECT tablee AS Table, columnn AS Column, sort AS Sort, " + " criteria AS Criteria, or1 AS ColOr1, or2 AS ColOr2 FROM queryfilters "+ " WHERE category='" + category + "' AND queryname='" + descr + "' AND modifier='"+modifier+"'", mycon); NpgsqlDataAdapter da = new NpgsqlDataAdapter(com); DataTable dt = new DataTable(); BindingSource bs = new BindingSource(); da.Fill(dt); bs.DataSource = dt; if(dt.Rows.Count != 0) { qDesigner.dgvFilters.AutoGenerateColumns = false; qDesigner.dgvFilters.DataSource = bs; qDesigner.dgvFilters.Columns["dgvColTable"].DataPropertyName = dt.Columns[0].ColumnName;
Can anyone help me with this issue?
Thanks
|
|
|
|
|
i am trying to show the records in labels like so:
User Name User
Alias Alias
Income Income
Gender Gender
Age Age
and then i want the buttons to control so i can hit next to show the next record
so for that i have this code
public void GetUserMatch(XmlNode usersNode, string CritBody, string CritAgeFrom, string CritAgeTo, string CritHeightFrom, string CritHeightTo)
{
XmlNodeList userNodes = usersNode.SelectNodes("Clients");
Users[] users = new Users[userNodes.Count];
MatchUsers[] matchUser = new MatchUsers[users.Length];
for (int i = 0x0; i < users.Length; i++)
{
XmlNode userNode = userNodes[i];
string userName = userNode.SelectSingleNode("Username").InnerText;
string alias = userNode.SelectSingleNode("Alias").InnerText;
string bodyType = userNode.SelectSingleNode("BodyType").InnerText;
string strheight = userNode.SelectSingleNode("Heightft").InnerText;
string displayheight = userNode.SelectSingleNode("DisplayHeight").InnerText;
string strage = userNode.SelectSingleNode("Age").InnerText;
string gender = userNode.SelectSingleNode("Gender").InnerText;
string income = userNode.SelectSingleNode("Income").InnerText;
int intHeight = Convert.ToInt32(strheight);
int intAge = Convert.ToInt32(strage);
users[i] = new Users(userName, alias, bodyType, intHeight, displayheight, intAge, gender, income);
}
MatchUsers[] matchUsers = new MatchUsers[0];
int ii = 0;
foreach (Users mUser in users)
{
string Bodytype = mUser.BodyType;
if (string.Compare(Bodytype, CritBody) == 0)
{
int intAge = Convert.ToInt32(mUser.Age);
if (intAge >= Convert.ToInt32(CritAgeFrom) & intAge <= Convert.ToInt32(CritAgeTo))
{
int intHeight = Convert.ToInt32(mUser.Height);
if (intHeight >= Convert.ToInt32(CritHeightFrom) & intHeight <= Convert.ToInt32(CritHeightTo))
{
string userName = mUser.UserName;
string alias = mUser.Alias;
string bodyType = mUser.BodyType;
string height = mUser.DisplayHeight;
string age = mUser.Age.ToString();
string gender = mUser.Gender;
string income = mUser.Income;
matchUser[ii] = new MatchUsers(userName, alias, bodyType, height, age, gender, income);
ii++;
}
}
}
}
DataSet ds = new DataSet();
DataTable dt = new DataTable("UserMatch");
ds.Tables.Add(dt);
DataColumn dc1 = new DataColumn("User Name");
DataColumn dc2 = new DataColumn("Alias");
DataColumn dc3 = new DataColumn("Body Type");
DataColumn dc4 = new DataColumn("Height");
DataColumn dc5 = new DataColumn("Age");
DataColumn dc6 = new DataColumn("Gender");
DataColumn dc7 = new DataColumn("Income");
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
dt.Columns.Add(dc3);
dt.Columns.Add(dc4);
dt.Columns.Add(dc5);
dt.Columns.Add(dc6);
dt.Columns.Add(dc7);
int iii = 0;
foreach (MatchUsers mUsers in matchUser)
{
DataRow dr = dt.NewRow();
if (mUsers != null)
{
dr["User Name"] = mUsers.UserName;
dr["Alias"] = mUsers.Alias;
dr["Body Type"] = mUsers.BodyType;
dr["Height"] = mUsers.Height;
dr["Age"] = mUsers.Age;
dr["Gender"] = mUsers.Gender;
dr["Income"] = mUsers.Income;
dt.Rows.Add(dr);
iii++;
}
}
this.UserBindingSource.DataSource = ds;
int asdf = this.UserBindingSource.Count;
this.lblMatchUserName.DataBindings.Add(
new Binding("Text",
this.UserBindingSource,
"UserMatch.User Name",
true));
this.lblMatchUserAlias.DataBindings.Add(
new Binding("Text",
this.UserBindingSource,
"UserMatch.Alias",
true));
this.lblMatchUserBody.DataBindings.Add(
new Binding("Text",
this.UserBindingSource,
"UserMatch.Body Type",
true));
this.lblMatchUserHeight.DataBindings.Add(
new Binding("Text",
this.UserBindingSource,
"UserMatch.Height",
true));
this.lblMatchUserAge.DataBindings.Add(
new Binding("Text",
this.UserBindingSource,
"UserMatch.Age",
true));
this.lblMatchUserGender.DataBindings.Add(
new Binding("Text",
this.UserBindingSource,
"UserMatch.Gender",
true));
this.lblMatchUserIncome.DataBindings.Add(
new Binding("Text",
this.UserBindingSource,
"UserMatch.Income",
true));
}
Now the problem im having is that when i check the count of binding source it is equal to one.. and the code i have to go to the next record is like so:
private void button1_Click(object sender, EventArgs e)
{
if (this.UserBindingSource.Position + 1 < this.UserBindingSource.Count)
{
this.UserBindingSource.MoveNext();
this.fnDisplayPosition();
}
}
private void button2_Click(object sender, EventArgs e)
{
this.UserBindingSource.MovePrevious();
this.fnDisplayPosition();
}
private void fnDisplayPosition()
{
this.lblPostion.Text = this.UserBindingSource.Position +
1 + " of " + this.UserBindingSource.Count;
}
so the part
if (this.UserBindingSource.Position + 1 < this.UserBindingSource.Count) is never true because binding source is always one. please tell me what am i doing wrong and how to solve this. thank you
|
|
|
|
|
Firstly are you sure that the GetUserMatch method is retrieving more than one record?
Secondly it strikes me that it might be easier to read the data directly into the DataSet and apply bindings through that.
Lastly and this is purely out of curiosity. Reading your code I noticed this:
for (int i = 0x0; i < users.Length; i++)<br />
Is there a particular reason that you are initializing i in hex? Can't think why not, just never seen it before.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
to answer the first yes i did check the getusermatch it is retreiving two records.. as it falls under it.. and second and third.. i really don't know alot about it but it is what i did after looking around in couple of websites and on msdn.
|
|
|
|
|
If you haven't already done so, take a look at the DataSet.ReadXML method.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Hi,
I am absolutely new to .NET and XML programming.
My query is regarding interprocess communication.
Assume that I have two executables written in .NET and I wish to pass some XML information between them.
I need to consider 2 cases: 1. These 2 executables reside on the same machine 2. They reside on different machines.
What methods/protocols are available to do this kind of communication?
Any pointers would be really helpful.
Thanks !
- NB
|
|
|
|
|
You have lots of options:
Remoting[^]
MSMQ[^]
Sockets[^]
All of those can send data between processes across machines and if you can do that then you can also send data between processes on the same machine.
|
|
|
|
|
Thanks !
Do you think SOAP is also an option for this scenario?
|
|
|
|
|