|
hey all,
I've GUI application, which executes some logic application,
i do it with backgroundworker.
in the simple way (no operation progress update)it works fine, but when i'm trying to update an operation
progress, i get this exception...
my main idea : GUI with some user controls (main thread) then i execute another worker thread, while it works i'm listening to event it rise (each time when the upgrade point was reached), and when i'm catching this event in the main gui thread, i would like to update some label control...
I read about invoke etc... but it doesn't work with backgroundWorker.
any idea for solution will be appreciated
thanks
|
|
|
|
|
BackgroundWorker has progress reporting built in. Use this functionality and the ProgressChanged event will be fired on your UI thread for you.
Nick
|
|
|
|
|
This[^] should certainly help you out.
Me, I'm dishonest. And a dishonest man you can always trust to be dishonest. Honestly. It's the honest ones you want to watch out for...
|
|
|
|
|
|
thanks both of you, it really helped me !!!
one more question,
is there a method to implement a "pause" button for running thread ?
i guess when i use backgroundWorker.CancelAsync(); current working thread is die...
what i want is to make it pause, and on my request, to be continued from the place it was stopped..
|
|
|
|
|
BackgroundWorker does not have Thread.Suspend and Thread.Resume, so I suspect you will have to do it manually:
1) Set up a class level bool accessable to both the main and BackgroundWorker threads.
2) Set the bool true in the main thread when you want to pause the worker, and false when you want to resume it.
3) In the worker thread, monitor the bool on occasion, and while it it true, use the Sleep method for a suitable interval - half a second or so, depending on how fast you want it to start continuing.
Or better, replace the bool with a ManualResetEvent[^]
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace
C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
|
|
|
|
|
thanks for the answer,
I'll try it
|
|
|
|
|
I am retrieving a filepath that I need in my program using a folderBrowserDialog. Can anyone tell me how to change all the back slashes to forward slashes?
Thanks
Darrall
|
|
|
|
|
string withForwardSlashes = withBackwardSlashes.Replace('\\', '/');
or
string withForwardSlashes = withBackwardSlashes.Replace("\\", "/");
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace
C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
|
|
|
|
|
I tried it exactly as you have it there and I get "Too many characters in character literal" and "No overload for method
'Replace' takes '1' arguments."
Any thoughts?
Thanks for your time.
Darrall
|
|
|
|
|
Works fine for me - could you post the code fragment exactly as you have it?
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace
C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
|
|
|
|
|
Here it is
private void button2_Click(object sender, EventArgs e)
{
string OldPathName;
string NewPathName;
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
MessageBox.Show("MyFolder = " + folderBrowserDialog1.SelectedPath);
textBox1.Text = folderBrowserDialog1.SelectedPath;
OldPathName = NewPathName.Replace ('\','/');
}
|
|
|
|
|
Backslash is an escape character. The '\' you entered is interpreted as '' . You can change that to @'\' or '\\' and it should work.
Don't blame me. I voted for Chuck Norris.
|
|
|
|
|
Thanks Somewhere along the line I think C# should have left the backslash alone.
|
|
|
|
|
Using an other character would probably be worse, since so many* languages use the backslash as escape character..
* Perl, Python, Java, ECMAScript, C, C++, PostScript, RTF, awk, sh and many others
|
|
|
|
|
I take it from the other responses that that problem is fixed? Now change your code slightly to make it actually work!
private void button2_Click(object sender, EventArgs e)
{
string OldPathName;
string NewPathName;
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
MessageBox.Show("MyFolder = " + folderBrowserDialog1.SelectedPath);
NewPathName = folderBrowserDialog1.SelectedPath;
textBox1.Text = NewPathName;
OldPathName = NewPathName.Replace ('\\','/');
}
You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace
C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy
|
|
|
|
|
Darrall wrote: Any thoughts?
Yes, two:- Did you type carefully?
- Post your code if you want us to help you fix it.
/ravi
|
|
|
|
|
Darrall wrote: Can anyone tell me how to change all the back slashes to forward slashes?
For what purpose? In Windows, the standard path seperator is a backslash while the forward slash is used for command line switches.
|
|
|
|
|
I am using folderBrowserDialog to locate the folder where the user stored his program which also has the location of the data files which are needed for the program. The path is returned as it is in the computer with backslashes but can't be used that way by the program.
|
|
|
|
|
Darrall wrote: can't be used that way by the program
Fix the program.
|
|
|
|
|
hi, I defined groups for a listview and I want to add items to the groups but I can't. Could you please see below code and tell me what's wrong?
listview1.Groups[0].Items.Add("item");
|
|
|
|
|
Hi,
teknolog123 wrote: but I can't
that is not informative.
What you need can be achieved through the Group property. Here[^] is a simple example.
|
|
|
|
|
Hello,
I am writing a messenger like client server application for a school project.
My client and server are communicating trough tcp protocol via the the .net sockets.
It all worked fine untill I tried to transfer complex data from the server to the client,like a custom classes I made.
To transfer this data I have decided to use Serialization and turn the objects to bytes,than send the bytes to the client via the the sockets SendTo method.
I did manage to send the object,but when I try to deserialize the bytes back to an object on the server side , I get this exception
System.Runtime.Serialization.SerializationException was unhandled
Message="Unable to find assembly 'SmallTalkServer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'."
Source="mscorlib"
I googled around for a few hours and learned that the source of the prroblem is that since the object was serialized on the server side application that have X assembles,it can't be deserialized on the client server which other assembles.
I tried to use the proprtey
bf.AssemblyFormat = FormatterAssemblyStyle.Simple;
But it didn't help,also from my searching I learned that there is a bug with the proprtey (I am using 3.5 framework s1)
I tried to change to soap foramtter and I soon learned that soap foramatter can not serialize generic types and thus he is help less for me.
I am really not sure how to solve this problem,please help me,
Thanks in advance!
btw this is my Serializer class which I have the same on both of the app.
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters;
namespace SmallTalkServer
{
public static class Serializer
{
public static void SerializeObject(string filename, BuddyList buddyList)
{
Stream stream = File.Open(filename, FileMode.Create);
BinaryFormatter bFormatter = new BinaryFormatter();
bFormatter.AssemblyFormat = FormatterAssemblyStyle.Simple;
bFormatter.Serialize(stream, buddyList);
stream.Close();
}
public static BuddyList DeSerializeObject(string filename)
{
BuddyList buddylist;
Stream stream = File.Open(filename, FileMode.Open);
BinaryFormatter bFormatter = new BinaryFormatter();
bFormatter.AssemblyFormat = FormatterAssemblyStyle.Simple;
buddylist = (BuddyList)bFormatter.Deserialize(stream);
stream.Close();
return buddylist;
}
public static BuddyList DeSerializeObject(byte[] arrBytes)
{
MemoryStream memStream = new MemoryStream();
memStream.Write(arrBytes, 0, arrBytes.Length);
memStream.Seek(0, SeekOrigin.Begin);
BuddyList buddylist;
BinaryFormatter bFormatter = new BinaryFormatter();
bFormatter.AssemblyFormat = FormatterAssemblyStyle.Simple;
buddylist = (BuddyList)bFormatter.Deserialize(memStream);
memStream.Close();
return buddylist;
}
public static byte[] ObjectToByteArray(object obj)
{
if (obj == null)
return null;
BinaryFormatter bf = new BinaryFormatter();
bf.AssemblyFormat = FormatterAssemblyStyle.Simple;
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, obj);
return ms.ToArray();
}
public static object ByteArrayToObject(byte[] arrBytes)
{
MemoryStream memStream = new MemoryStream();
BinaryFormatter binForm = new BinaryFormatter();
binForm.AssemblyFormat = FormatterAssemblyStyle.Simple;
memStream.Write(arrBytes, 0, arrBytes.Length);
memStream.Seek(0, SeekOrigin.Begin);
object obj = (object)binForm.Deserialize(memStream);
return obj;
}
}
}
|
|
|
|
|
A class is identified by its name and the assembly in which it is declared.
I presume you have included your BuddyList class code in both your server and client assemblies. This creates 2 different types because they are in different assemblies. You cannot, by default, serialize one class and deserialize a different one, even if they have identical members.
One solution would be to put any shared classes in a separate library assembly and reference that from both your server and client applications.
Another option would be to use a SerializationBinder . This would also help you maintain backwards compatibility between versions, if you also implement ISerializable .
Nick
|
|
|
|
|
Thanks for the anwser,I was wondering if you have any good guide for how SerializationBinder is working,or more details on how to create a separate library assembly,since I am learning through creating the project I am not familiar with those things.
Thank you!
|
|
|
|
|
Ok, the easiest way is to create a library assembly:
1) Add a new project in your solution of type "Class Library"
2) Put your code for any classes that are shared ( like BuddyList ) into this project.
3) Right-click on the References node of both your server and client and add a reference to the new project.
Both your server and client will now be using the exact same classes and you will be able to serialize them in one and deserialize them in the other.
Nick
|
|
|
|