|
The grid is not a problem, it updates correctly when the data is returned. The issue is that the rest of the form also waits for the thread to complete before it finishes form_load.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Your problem is that the UI thread is still doing the main work. You spawn a thread, and when that thread executes, it simply delegates the work back onto the UI thread. Woops.
Assuming that getting the DataTable is the part that's freezing your UI, here's what the correct code should look like, using the recommended pattern of the BackgroundWorker component.
class Form1 : Form
{
public Form1()
{
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += (sender, e) =>
{
e.Result = cashFlow.GetAttrForCashflow(cashFlow.CashflowID);
};
worker.RunWorkerCompleted += (sender, e) =>
{
var dataTable = (DataTable)e.Result;
UIUtils.dgLoad(dataTable, dataTable.DefaultView, "");
};
worker.RunWorkerAsync();
}
}
Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon
Judah Himango
|
|
|
|
|
Thanks Judah, I pinched Navaneeths solution.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Your LoadGrid method has the problem..
private void LoadGrid()
{
try
{
if (this.InvokeRequired)
{
this.BeginInvoke(new LoadGridDelegate(LoadGrid), null);
Now <code>InvokeRequired</code> will be false and else conditions executes.
}
else
{
DataTable dtAttr = cashFlow.GetAttrForCashflow(cashFlow.CashflowID);
UIUtils.dgLoad(dgAttr, dtAttr.DefaultView, "");
}
}
catch (Exception)
{ throw; }
} Here is the corrected code,
private void LoadGrid()
{
DataTable dtAttr = cashFlow.GetAttrForCashflow(cashFlow.CashflowID);
this.BeginInvoke((MethodInvoker)delegate
{
UIUtils.dgLoad(dgAttr, dtAttr.DefaultView, "");
});
}
|
|
|
|
|
Magic stuff - exactly what I was expecting from my code crap. Now to do some research on MethodInvoker
Thanks a lot!
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Mycroft Holmes wrote: Now to do some research on MethodInvoker
It's just a delegate which can point to any method which will have no parameters.
|
|
|
|
|
[MSDN]This API supports the .NET Framework infrastructure and is not intended to be used directly from your code[/MSDN]
This would normally make me leery of using it.
N a v a n e e t h wrote: method which will have no parameters.
I presume this limitation is the reason it is not recommended. In which case the background solution will need to looked into as well.
Thanks.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
In that case you can create your own delegate with required parameters. I have seen many applications using MethodInvoker when you need to invoke a simple parameter-less method. I am not sure why MSDN say's not to use it.
|
|
|
|
|
Hi Mycroft,
getting the DataTable is what takes most of 10 seconds and needs to be delegated to a thread.
Your LoadGrid() method uses Invoke to cause the GUI thread to do it anyway, so your efforts to speed up things are pointless so far.
Here is unchecked code to get the DataTable on the thread and the DGV update on the UI thread;
it does not rely on any fancy C# code, it should work on all .NET versions:
private void LoadGrid(DataTable dtAttr) {
if (this.InvokeRequired) {
dtAttr = cashFlow.GetAttrForCashflow(cashFlow.CashflowID);
this.BeginInvoke(new LoadGridDelegate(LoadGrid), dtAttr );
} else {
UIUtils.dgLoad(dtAttr, dtAttr.DefaultView, "");
}
}
With anonymous methods/delegates you can further simplify it as others have shown.
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,
I am attempting to unit test some code i have written. Unfortunately I am having an issue when using a generic list of an object type I have defined. I'm basically checking to see that this list is equal to 0 and if it is return false. However when the check is performed I get a null exception error. Below is an example (not exact code) of the code I'm trying to test and the bold and underline is where i'm receiving the error. Thanks in advance to all that reply.
private Car _car = new Car();
//constructor
public CarValidation(Car carDetails)
{
if (carDetails == null)
throw new ArgumentNullException();
else
_car = carDetails;
}
public bool CheckForValues()
{
bool valid = true;
if(_car.OilPressure == null)
valid = false;
if (_car.TirePressure == null)
valid = false;
if (_car.GasAmount == null)
valid = false;
if (_car.Mp3SongsLoaded.Count == 0)
valid = false;
return valid;
}
|
|
|
|
|
car.Mp3SongsLoaded is null. Make sure you initialize it before using it, e.g. car.Mp3SongsLoaded = new List<string>();
Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon
Judah Himango
|
|
|
|
|
Thanks. Totally forgot to initialize the list.
|
|
|
|
|
Hi all,
I've been having a little problem with the WebClient class, and am hoping someone can help.
Here is what I'm currently working with:
<br />
string uri = "WebURL";<br />
<br />
byte[] postBytes = Encoding.ASCII.GetBytes(postString_Begin);<br />
byte[] postBytes2 = Encoding.ASCII.GetBytes(postString2);<br />
<br />
WebClient myClient = new WebClient();<br />
myClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");<br />
<br />
byte[] response = myClient.UploadData(uri, postBytes);<br />
<br />
string myresponse = Encoding.ASCII.GetString(response);<br />
<br />
response = myClient.UploadData(uri2, postBytes2);<br />
Here is where I hit my wall.
After I upload the first queryString, the next step in the process is returned.
The URL does not change, but when I go to post the next section of values, it posts them back to the first page/form part.
Can anyone point me to How I can post my second querystring to the returned page from the first request?
Thanks
|
|
|
|
|
Hi,
I have a form that contains a ComboBox and a DataGridView. The ComboBox gets populated with all the countries as seen in the code below:
private void PopulateCountries()
{
cbCountryId.DataSource = dsCountries.Tables["Country"];
cbCountryId.DisplayMember = "Country";
cbCountryId.ValueMember = "Id";
}
What I need to do is populate the DataGridView based on what country the user select. For example: if you select Australia then all the Australian states should display in the gridview. The code to display the states look like this:
private void GetStateByCountryId(int CountryId)
{
this.dsWBGT_V4 = dataCommunicator.GetStateByCountryId(CountryId);
this.stateBindingSource.DataSource = this.dsWBGT_V4;
}
As you can see I have a Property named CountryId but I cannot put it in the SelectedIndexChanged event because if the application start the event fires and I get an error because there was no selection in the ComboBox. I set the Property with this line:
CountryId = Convert.ToInt32(cbCountryId.SelectedValue);
Can anyone tell me how can I get the ComboBox to display a value by default at first run and set the property equal to that property so that the states in the selected country displays??
Illegal Operation
|
|
|
|
|
In your selectedindexchanged event test that there is a valid item
if(DGV.SelectedItems.count == 0)<br />
{return ;}
This is a common issue - an index changes twice - deselects on [0] and selects[4] so the event fires twice. This is needed for all collection controls, you just need to find the correct property to test for each control type.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Thank you for the reply.
Excuse my ignorance but I have tried to add this code to the IndexChange event but I have no option of SelectedItems.count?
I have tried dgvState.SelectedRows.Count but obviously that does not work?
Illegal Operation
|
|
|
|
|
you can ignore the selectedIndexChanged Event whenever you want.
follow the steps to ignore the event and put it back when the form completly load.
comboBox1.SelectedIndexChanged -=comboBox1_selectedIndexChanged;
do your work here and then
comboBox1.SelectedIndexChanged +=comboBox1_selectedIndexChanged;
"comboBox1_selectedIndexChanged" is the selected Index Change event of comboBox1
|
|
|
|
|
All
I have an application that the client uses, we do release updates every few months and everytime there is an update we send them a CD.We decided to update app automatically.
The application uses an exe file along with 3 DLLs installed into GAC, while programming for auto updates I have no problem pulling the new files in to clients machine or with installing DLLs into GAC(because the DLLs have newer version number). But I am struck at replacing the original exe file, since the user clicks on a menu item that says "Get Updates", I don't see how I can replace the exe file without exiting the program itself!!!!!
Appreciate any help
~Sri~
|
|
|
|
|
Ah, the chicken-and-egg update problem.
What you'll want to do is spawn a separate to actually replace your executable and immediately close the app.
Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon
Judah Himango
|
|
|
|
|
i want to rename a file. i have one list view ,in this list view many files are listed. i want to rename a file just like we rename file in windows. how can i get this. any idea
thank you!
yogesh
|
|
|
|
|
Use File.Move (it's in System.IO).
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Set labeledit property to true. Handle AfterLabelEdit event to get notified when an item gets renamed.
|
|
|
|
|
Take a look at this[^], and take your pick.
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.”
|
|
|
|
|
When running the following code "stream" is null anyone see why? I'm a noob. Is there a better way to get my embedded resources out of my build?
private static void ExtractFile()
{
Assembly Assemb = Assembly.GetExecutingAssembly();
Stream stream = Assemb.GetManifestResourceStream("LockOut_NA.Properties.Resources.ECLock");
FileStream fs = new FileStream(@"c:\ECLock1.htm",FileMode.CreateNew,FileAccess.Write);
StreamReader Reader = new StreamReader(stream);
StreamWriter Writer = new StreamWriter(fs);
Writer.Write(Reader.ReadToEnd());
}
Thanks in advance.
|
|
|
|
|
I guess the string passed to GetManifestResourceStream is incorrect. Call GetManifestResourceNames to see available embedded resources.
|
|
|
|