|
Hmm... I got that. So how do I apply the thing you just said in my scenario??? Or what do I need to change to make this work???
Please note that in my case creating 2 assemblies is inevitable. Also I cannot provide more than 1 assembly to the client.
Please advice.
Regards,
Samar
|
|
|
|
|
Hi All
I downloaded a library to compress files into zip archives...
I am now trying to add an event handler (or any other type of handler) that will monitor the progress of the compression.
This is the code to compress the files: (I have marked where I am having difficulty)
using (ZipFile zip = new ZipFile())
{
zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
ZipEntry e = zip.AddFile(ex1);
e = zip.AddFile(ex2);
zip.AddProgress += new ProgressChangedEventHandler(saveProgress);
zip.Save(compressedFile);
}
This is the code to track the changes:
private void saveProgress(Object sender, SaveProgressEventArgs e)
{
switch (e.EventType)
{
case ZipProgressEventType.Saving_Completed:
Console.WriteLine("Save Complete");
sendFile("Compressed/Package.zip");
break;
case ZipProgressEventType.Saving_Started:
Console.WriteLine("Saving Started...");
break;
default:
Console.WriteLine("Saving...");
break;
}
}
I am not sure how to go about adding an eventhandler to the zip to monitor the progress.
Thanks for all those who can assist!
|
|
|
|
|
Could you elaborate a little on what type of error occurs? Is it compilation error or runtime error? What does the error say?
|
|
|
|
|
It is a compilation error:
"No overload for 'saveProgress' matches delegate 'System.ComponentModel.ProgressChangedEventHandler'"
Viewing other examples of eventhandling, I haven't seen any of them adding parameters to the methods they call as the event handlers.
|
|
|
|
|
I didn't use the library you talk about, but the example I found is bit different, than what you supplied: Link[^]. The link I suppliy is documentation of the event handler you want to use. And there event argument is AddProgressEventArgs instance and you use SaveProgressEventArgs. Maybe you attached wrong handler? Or you wanted to attach to a different event (SaveProgress)? Hope this leads you to a solution for your problem.
|
|
|
|
|
Hi Again
I tried the link that you sent and it worked perfectly...
...I then tried the same thing, but with SaveProgress, and it worked too.
So thank you for your speedy assistance, it is much appreciated!
They should have some sort of rating system for rating answers and the people who send them.
Thank you for your time!
Enjoy the rest of the day!
|
|
|
|
|
Actually they have. If you expand my previous answer you should have links "Good answer" and "Bad answer". Click whichever you think is better
|
|
|
|
|
A quick question: can "Microsoft Surface Toolkit for Windows Touch Beta" be used with standard multi-touch devices (like this one: link[^]). Because I haven't found any conclusive answer to this question and don't want to spend cash if I wan't be able to use it. And do I need anything special to use it? Or just standard Windows 7 or Vista will suffice?
Thanks in advance.
|
|
|
|
|
Hi,
Is there any support for graphs in .Net (simple bar, line and dot graphs)? Any examples to use will be good.
Gurpreet
|
|
|
|
|
Have a look at this article.
My signature "sucks" today
|
|
|
|
|
you can use microsoft chart control for graphs in dotnet applications
|
|
|
|
|
Yes. See this[^] link.
/ravi
|
|
|
|
|
protected virtual void Dispose(bool bDisposing)
{
if (bDisposing)
{
// No need to call the finalizer since we've now cleaned
// up the unmanaged memory
GC.SuppressFinalize(this);
}
if(this.m_pNativeObject != IntPtr.Zero)
{
// Call the DLL Export to dispose this class
DisposeNetworkImp(this.m_pNativeObject);
}
}
|
|
|
|
|
Shouldn't the DisposeNetworkImp IF and code go inside the if(bDisposing) block?
|
|
|
|
|
There might be a problem with the "Data Execution Prevention" (DEP). We had lots of problems with a third party component till we were told about that.
Just switch DEP off for a test. If that resolves the problem, you can also prevent DEP for your program by adding a post build action:
call "$(VS80COMNTOOLS)\vsvars32.bat"
editbin.exe /NXCOMPAT:NO "$(TargetPath)"
|
|
|
|
|
I found the error i was creating the object with diff class and trying to delete it with different class destructor the problem was in DLL only.
|
|
|
|
|
How to solve this problem?
System.Data.SqlClient.SqlException: Can not open 'User Management' in the request database. Login failed.
User 'HUIZHI-C02E58FC\liuhanpo' Login failed.
In System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
In System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
In System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
In System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK)
In System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, Int64 timerExpire, SqlConnection owningObject)
In System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(String host, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, Int64 timerStart)
In System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance)
In System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance)
In System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection)
In System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options)
In System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject)
In System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject)
In System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)
In System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
In System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
In System.Data.SqlClient.SqlConnection.Open()
In User Management.ChangestaffPwd.button5_Click(Object sender, EventArgs e) Location C:\Documents and Settings\liuhanpo\Desktop\User Management\ChangestaffPwd.cs:Line 137
In System.Windows.Forms.Control.OnClick(EventArgs e)
In System.Windows.Forms.Button.OnClick(EventArgs e)
In System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
In System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
In System.Windows.Forms.Control.WndProc(Message& m)
In System.Windows.Forms.ButtonBase.WndProc(Message& m)
In System.Windows.Forms.Button.WndProc(Message& m)
In System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
In System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
In System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
|
|
|
|
|
Make sure the credentials you are using are valid. Check for typos, uses SSMS to connect using the credentials.
If you are using a restored database you may need to drop and recreate the user in SQL Server.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I thought I had this down from asking this question before but I'm having issues with this once again.
What I am trying to do is load a picture from a database using the IDataReader (so I can read it byte by byte). Now I have this function in another class with events. So I have a ProgressChanged event and a Finished Event. Before calling these events I check if they are null and it still gives me this error
[5/23/2010 10:58:31 PM LoadPicture]: System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'frmViewAsset'.
at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous)
at System.Windows.Forms.Control.Invoke(Delegate method, Object[] args)
at System.Windows.Forms.Control.Invoke(Delegate method)
at AssetMgmt.frmViewAsset.p_ProgressChanged(Int32 current, Int32 max, String c, String t) in C:\Applications\Windows Applications\AssetMgmt\AssetMgmt\frmViewAsset.cs:line 388
at AssetMgmt.LoadPicture.Load() in C:\Applications\Windows Applications\AssetMgmt\AssetMgmt\Operations\LoadPicture.cs:line 74
Now first you probably want to know what is line 388 on frmViewasset (this is the parent form). The code below is the event in the parent form.. the action it takes when the event is called from the LoadPicture class. The thing is I am "dispoing" of this event on the FormClosing and it still isn't working right.. givingme that error
private void p_ProgressChanged(int current, int max, string c, string t)
{
if (!this.IsDisposed)
{
Invoke((Action)(() =>
{
pbPicture.Maximum = max;
pbPicture.Value = current;
lbPicProgress.Text = string.Format("{0}MB / {1}MB", c, t);
}));
}
}
LoadPicture lp;
private void frmViewAsset_Load(object sender, EventArgs e)
{
lp = new LoadPicture(AssetID);
lp.ProgressChanged += new LoadPicture.ProgressDelegate(p_ProgressChanged);
lp.Finished += new LoadPicture.FinishedDelegate(p_Finished);
t4 = new Thread(new ThreadStart(PopulatePicture));
t4.Start();
}
private void PopulatePicture()
{
lp.Load();
}
private void frmViewAsset_FormClosing(object sender, FormClosingEventArgs e)
{
lp.Stop();
lp.Dispose();
}
So basicaly when the form loads it creates another thread that calls the lp.Load(). This begins the SQL Connection and reads byte by byte (or every 1024 bytes). Each time it loops it calls:
if (ProgressChanged != null)
ProgressChanged(blahblahblah);
So how come i'm c alilng my lp.Dispose() (which sets ProgressChanged = null) i still get this error when the user exits? I just can't figure it out
|
|
|
|
|
so what is in line 388?
and I'm afraid we need to see more code: the LoadPicture constructor, its Load method, and how its Progress and Finished events work. and can we see p_Finished()?
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
I'm sorry I was nervous about putting taht much code in the first post. I know its not recommended..
Ok the LoadPicture Class:
class LoadPicture : IDisposable
{
public delegate void FinishedDelegate(Image img);
public delegate void ProgressDelegate(int current, int max, string c, string t);
public event FinishedDelegate Finished;
public event ProgressDelegate ProgressChanged;
string Tag;
bool stop;
public LoadPicture(string Tag)
{
this.Tag = Tag;
stop = false;
}
~LoadPicture()
{
ProgressChanged = null;
Finished = null;
}
public void Load()
{
SqlConnection conn = new SqlConnection(AssetConfig.ConnectionString);
SqlCommand cmd = new SqlCommand("SELECT Picture FROM Inventory_Pictures WHERE PictureID=@Tag", conn);
cmd.Parameters.AddWithValue("@Tag", Tag);
try
{
conn.Open();
SqlDataReader tmp = cmd.ExecuteReader();
if (tmp.HasRows)
{
tmp.Close();
IDataReader r = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
if (r != null)
{
r.Read();
long size = r.GetBytes(0, 0, null, 0, 0);
byte[] buffer = new byte[size];
int bufferSize = 1024;
int dataIndex = 0;
long bytesRead = 0;
while ((bytesRead < size) && ((size - bytesRead) > 1024))
{
if (stop)
break;
bytesRead += r.GetBytes(0, dataIndex, buffer, dataIndex, bufferSize);
dataIndex += 1024;
decimal FileSize = (Convert.ToDecimal(size) / 1024) / 1024;
decimal CurrentSize = (Convert.ToDecimal(bytesRead) / 1024) / 1024;
if (ProgressChanged != null)
ProgressChanged(dataIndex, Convert.ToInt32(size), Math.Round(CurrentSize, 2).ToString(), Math.Round(FileSize, 2).ToString());
}
if (!stop)
{
bytesRead += r.GetBytes(0, dataIndex, buffer, dataIndex, Convert.ToInt32((size - bytesRead)));
MemoryStream ms = new MemoryStream(buffer);
if (Finished != null)
Finished(Image.FromStream(ms));
ms.Close();
}
}
r.Close();
}
}
catch (Exception ex)
{
Error.WriteError(ErrorType.SQL, "LoadPicture", ex.ToString());
}
finally
{
if (conn != null)
{
conn.Close();
conn.Dispose();
}
if (cmd != null)
cmd.Dispose();
}
}
public void Stop()
{
stop = true;
}
public void Dispose()
{
System.GC.SuppressFinalize(true);
}
}
Here is frmViewAsset (some of it) and you notice how it initializes the events:
LoadPicture lp;
Thread t4;
private void frmViewAsset_Load(object sender, EventArgs e)
{
lp = new LoadPicture(AssetID);
lp.ProgressChanged += new LoadPicture.ProgressDelegate(p_ProgressChanged);
lp.Finished += new LoadPicture.FinishedDelegate(p_Finished);
t4 = new Thread(new ThreadStart(PopulatePicture));
t4.Start();
}
private void PopulatePicture()
{
lp.Load();
}
private void p_ProgressChanged(int current, int max, string c, string t)
{
if (!this.IsDisposed)
{
Invoke((Action)(() =>
{
pbPicture.Maximum = max;
pbPicture.Value = current;
lbPicProgress.Text = string.Format("{0}MB / {1}MB", c, t);
}));
}
}
private void p_Finished(Image img)
{
Invoke((Action)(() =>
{
picAsset.Image = img;
}));
}
private void frmViewAsset_FormClosing(object sender, FormClosingEventArgs e)
{
lp.Stop();
lp.Dispose();
}
You will see on the frmViewAsset I am calling lp.Stop(); which should be stopping the datareader from reading any further (breaking out of the while statement in the LoadPicture class). You also notice I call lp.Dispose() which should be setting both PRogressChanged and Finished events to null
I'm assuming that LoadPicture is having a hard time disposing before frmViewAsset can dispose (hints why it is still called). But why?!?!
FYI I have also tried this on form closing:
lp.ProgressChanged -= new LoadPicture.ProgressDelegate(p_ProgressChanged);
lp.Finished -= new LoadPicture.FinishedDelegate(p_Finished);
The ultimate goal is to load the picture by every 1024 bytes which allows me to display a progress bar and label showing 1.0MB out of 2.0MB (that kind of thing). All this works except if the user closing this form before it has finished loading the picture (which I need it to safely dipose / stop loading it without the user noticing). Right now the user won't notice but thats because on error it writes the Error to the error log. That doesn't work for me ... I like to try to do things the correct way lol
** EDIT **
Don't know if it will make a difference but I neglected to mention that the PROGRESSBAR and the LABEL are both in the status bar. They are not labels directly on the form.
|
|
|
|
|
Hi Jacob,
I think you handled the stopping and disposing the wrong way. For one, you should not implement a destructor; normally one implements a Dispose() method that disposes first by providing and calling a Dispose(bool) method, then calls SuppressFinalize. Your Dispose() not calling Dispose(bool) at all, I think your destructor is unreachable. On top of that I see no need for all of it.
This is what I would do:
D1. remove "IDisposable" and all dispose related code from LoadPicture. There is no need for LoadPicture to be disposable, it is not holding on to lots of memory that otherwise may get lost.
D2. implement a better Stop method; all you need is for LoadPicture to really have stopped somehow before you close the Form, and make the Progress/Finished events point to something that does no longer exist.
public void Stop() {
stop = true;
ProgressChanged = null;
Finished = null;
}
This is not waterproof yet, there is a slim chance of Load() to have tested say ProgressChanged (turned out to be not null), the main thread starting to close the form, setting ProgressChanged null, stop true, actually then closing the form, and only then Load calling ProgressChanged.
So you could:
- make stop volatile
- and test both the event and stop in that order (opposite from the Stop order), hence:
if (ProgressChanged != null && !stop) ProgressChanged(dataIndex, ...)
That should take care of it.
I do have more comments:
C1.
you used a strange way to cope with the end of the stream. I would simply use a loop containing
bytesRead += r.GetBytes(0, dataIndex, buffer, dataIndex, bufferSize);
dataIndex += bytesRead;
and exit the loop when bytesRead turns zero.
C2.
your progress reporting seems expensive, in two ways:
a. you switch from int/long to decimal, to cope with small sizes, which would vanish when divided by 1024 or 1024*1024.
b. and you report very frequently, so consecutive reports will not differ by much, probably wasting a lot of CPU cycles, and actually slowing down progress significantly.
I would use a larger data buffer (say 64KB), calculate an integer percentage locally, compare it to the previous value, and only report it on a change of percentage value.
C3.
You don't want your thread (t4) to ever prevent your app from exiting, so it is a background thread. You should tell it so: t4.IsBackground=true; before t4.Start();
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
I am still trying your example.. Having issues with converting the bytesRead and chanigng the way I read the bytes.
Plus i think bytesRead would never be zero since you are using +=
|
|
|
|
|
Sorry, my mistake; you are right of course. What I meant was:
int newAmount = r.GetBytes(0, dataIndex, buffer, dataIndex, bufferSize);
bytesRead += newAmount;
dataIndex += newAmount;
and exit when newAmount is zero. You probably don't need both bytesRead and dataIndex now.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
Here is my updated version. I set the thread to IsBackground = true and also on frmViewAsset closing it calls lp.Stop();
public void Load()
{
SqlConnection conn = new SqlConnection(AssetConfig.ConnectionString);
SqlCommand cmd = new SqlCommand("SELECT Picture FROM Inventory_Pictures WHERE PictureID=@Tag", conn);
cmd.Parameters.AddWithValue("@Tag", Tag);
try
{
conn.Open();
IDataReader r = cmd.ExecuteReader(CommandBehavior.SequentialAccess);
if (r != null)
{
bool exist = r.Read();
if (exist)
{
long size = r.GetBytes(0, 0, null, 0, 0);
byte[] buffer = new byte[size];
int bufferSize = 65536;
long bytesRead = 0;
decimal prevPercent = 0;
while ((bytesRead < size) && ((size - bytesRead) > bufferSize))
{
if (stop)
break;
long newAmount = r.GetBytes(0, (Int32)bytesRead, buffer, (Int32)bytesRead, bufferSize);
bytesRead += newAmount;
if (newAmount == 0)
break;
decimal FileSize = (size / 1024) / 1024;
decimal CurrentSize = (bytesRead / 1024) / 1024;
if (ProgressChanged != null && !stop)
{
decimal currentPercent = (CurrentSize / FileSize) * 100;
if (currentPercent != prevPercent)
ProgressChanged((int)CurrentSize, (int)FileSize, Math.Round(CurrentSize, 2).ToString(), Math.Round(FileSize, 2).ToString());
prevPercent = currentPercent;
}
}
if (!stop)
{
MemoryStream ms = new MemoryStream(buffer);
if (Finished != null)
Finished(Image.FromStream(ms));
ms.Close();
}
}
r.Close();
}
}
catch (Exception ex)
{
Error.WriteError(ErrorType.SQL, "LoadPicture", ex.ToString());
}
finally
{
if (conn != null)
{
conn.Close();
conn.Dispose();
}
if (cmd != null)
cmd.Dispose();
}
}
Problem is ProgressChanged is apparently being called after frmViewAsset disposes??
[5/24/2010 10:39:13 AM LoadPicture]: System.InvalidOperationException: Invoke or BeginInvoke cannot be called on a control until the window handle has been created.
at System.Windows.Forms.Control.MarshaledInvoke(Control caller, Delegate method, Object[] args, Boolean synchronous)
at System.Windows.Forms.Control.Invoke(Delegate method, Object[] args)
at System.Windows.Forms.Control.Invoke(Delegate method)
at AssetMgmt.frmViewAsset.p_Finished(Image img) in C:\Applications\Windows Applications\AssetMgmt\AssetMgmt\frmViewAsset.cs:line 379
at AssetMgmt.LoadPicture.Load() in C:\Applications\Windows Applications\AssetMgmt\AssetMgmt\Operations\LoadPicture.cs:line 81
It fails on the Finished.. I think only because I can't close the window quick enough during the Progress. Funny thing is I saw the image appear in the picturebox before it finally closed and it still showed this
modified on Monday, May 24, 2010 11:39 AM
|
|
|
|
|