|
|
|
Hi all,
I have a little problem with one of my threads now that I switched over to the dotnet 2.0
I have a main form with a toolbar. One button on the toolbar opens a new form, an appointmentbook.
I have a Global boolean variable AppointmentBook to check if it is open or not, then I initialized the form I want to open with the thread during the load of my main form, named appointmentBook. It all worked in .net1.1 but now I get an error at the point where I say
...appointmentBook.WindowState = System.Windows.Forms.FormWindowState.Normal...
What this is suppose to do is if the form was just minimized, and the toolbar button was clicked, it will see that currently there is an open form and then just bring that form up again.
The error I receive is a cross threading error, and says that I can't access the form initialized in another thread.
So I have the following...
case "2":
//start thread to handel new appointment book
if(Brim.Library.GlobalVariables.AppointmentBook)
{
appointmentBook.WindowState = System.Windows.Forms.FormWindowState.Normal;
}
else
{
StartThread();
}
private void StartThread()
{
ThreadStart appointThread = new ThreadStart(InitializeAppointmentBook);
Thread workerThread = new Thread(appointThread);
workerThread.Start();
}
private void InitializeAppointmentBook()
{
Brim.Library.GlobalVariables.AppointmentBook = true;
appointmentBook.ShowDialog();
}
Any help in how to solve this will be appreciated.
Thanks in advance
Last modified: 32mins after originally posted --
He who laughs last is a bit on the slow side
|
|
|
|
|
What's the error ? Global Variables always cause grief.
In .NET 2.0 there's an exception called if you access UI elements across thread. You can either turn this off ( checkforcrossthreadclientsomethingorother is the property name ), or you can fix your code, to not access UI elements between threads ( which is fraught with danger )
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
Does <code>StartThread</code> create the <code>appointmentBook</code> object? If not, who creates it? My guess would be that it is created by a different thread and when the UI thread attempts to access the <code>WindowState</code> property, a cross thread exception occurs.
You can solve this problem by creating the form on the UI thread (or whatever thread accesses the <code>WindowState</code>) property. To make a method execute on the UI thread, you can use Control.Invoke[^] or Control.BeginInvoke[^]. This[^] article will explain you the basics behind those methods and why they are needed.
|
|
|
|
|
Thanks for the reply. The main form is on the main thread. The main form contains the toolbar. The appointmentBook is created on the main form...frmAppointmentBook appointmentBook = new frmAppointmentBook, but the appointmentBook.ShowDialog() is only called within the sub thread. Then the call to the WindowState of the appointmentBook is done from the main thread.
Hope that makes sense
He who laughs last is a bit on the slow side
|
|
|
|
|
NeroToxic wrote: ut the appointmentBook.ShowDialog() is only called within the sub thread. Then the call to the WindowState of the appointmentBook is done from the main thread.
There lies your problem. If you're going to show the form on a different thread, create the form on that thread. When trying to acess the <code>WindowState</code> property from the main thread, use Invoke or BeginInvoke like this
void SetWindowStateForAppointmentBook()
{
if (appointmentBook.InvokeRequired)
{
this.Invoke(new MethodInvoker(SetWindowStateForAppointmentBook), null);
}
else
{
appointmentBook.WindowState = System.Windows.Forms.FormWindowState.Normal;
}
} Hope this helps.
|
|
|
|
|
Thanks, works 100%
He who laughs last is a bit on the slow side
|
|
|
|
|
Hi
I have exported pubs authors table into flatfile. I am trying to import the same flat file data into dataset in C#. Could you any one help me please.
Data from flat file to Dataset(same format like authors table).
kesavan
|
|
|
|
|
The following works for an Excel worksheet:
this.dataSet = GetDataset(worksheet, "BookName");
private DataSet GetDataset(Excel.Worksheet worksheet, string startElement)
{
StringBuilder stringBuilder = new StringBuilder();
XmlTextWriter xmlTextWriter = new XmlTextWriter(new StringWriter(stringBuilder));
xmlTextWriter.WriteStartDocument();
xmlTextWriter.Formatting = Formatting.Indented;
xmlTextWriter.Indentation = 3;
int row = 2;
Array rowValues = (Array)worksheet.get_Range("A" + row.ToString(), "C" + row.ToString()).Cells.Value2;
string Col1 = GetString(rowValues.GetValue(1, 1));
string Col2 = GetString(rowValues.GetValue(1, 2));
string Col3 = GetString(rowValues.GetValue(1, 3));
DataSet ds = new DataSet("table");
xmlTextWriter.WriteStartElement(startElement);
if (Col2 == string.Empty)
{
DataTable dataTable = new DataTable("row");
dataTable.Columns.Add("Col1");
dataTable.Columns.Add("Col2");
dataTable.Columns.Add("Col3");
dataTable.Rows.Add(new object[] { null, null, null });
ds.Tables.Add(dataTable);
return ds;
}
while (Col2 != string.Empty)
{
xmlTextWriter.WriteStartElement("row");
xmlTextWriter.WriteElementString("Col1", Col1);
xmlTextWriter.WriteElementString("Col2", Col2);
xmlTextWriter.WriteElementString("Col3", Col3);
xmlTextWriter.WriteEndElement();
row++;
rowValues = (Array)worksheet.get_Range("A" + row.ToString(), "C" + row.ToString()).Cells.Value2;
Col1 = GetString(rowValues.GetValue(1, 1));
Col2 = GetString(rowValues.GetValue(1, 2));
Col3 = GetString(rowValues.GetValue(1, 3));
}
xmlTextWriter.WriteEndElement();
xmlTextWriter.Flush();
xmlTextWriter.Close();
XmlTextReader xmlReader = new XmlTextReader(stringBuilder.ToString(), XmlNodeType.Element, null);
ds.ReadXml(xmlReader);
return ds;
}
For a flat file get rowValues array as follows:
StreamReader inStream = new StreamReader(filePath);
line = inStream.ReadLine();
while(inStream.Peek() >= 0)
{
line = inStream.ReadLine();
string[] rowValues = line.Split(new Char [] {','});//assumes flat file is comma delimited
}
inStream.Close();
Col1 = rowValues[1];
Col2 = rowValues[2];
Col3 = rowValues[3];
|
|
|
|
|
I am trying to connect SQL Server from my computer (named TANKIMHUY.kimhuy.com.vn) to my colleague's computer (named PHANTAM.kimhuy.com.vn). Of course both computers are clients.
SQL Server instance is installed in two machines.
I open SqlConnection's wizard dialog, I see two names( (local) and PHANTAM) in Server Name comboBox.
When I choose PHANTAM as a server name and fill usename: sa password: sa, but I see an empty list of databases instead of a list of available databases such as Northwind, ... in SQL Server 2000
I am very confused what wrong I have. Someone help me show the available databases or how to connect SQL Server from client machine to client other in C#
Thank you very much.
-- modified at 1:57 Wednesday 17th January, 2007
|
|
|
|
|
Have you check the basics?
- Have you got the correct credential to conect to his database? (Try with both SQL credential and microsoft autentification)
- Has your colleague given you the correct privileges to access his database?
- have you try to create a OBDC connection to his PC (That will also validate the connection)
- Check with your IT that they have not restricted some connections...
Elephant
|
|
|
|
|
Hi All,
I have block of code and i want to execute that code only once when we run(execute) a program 4 first time in C#.net
pls give a sol ,I wiil be looking for u r reply,
regards,
VINAY,
pune
|
|
|
|
|
If you are using asp.net code behind c# means put the code in postback event.
kesavan
|
|
|
|
|
I'd say your best bet would be to create a registry entry, firstRun, 0-1.
Then check that registry value to check if it is your apps first run, if it is execute block of code, set value to 0 for false, next time you start up the program it will check the value and see firstRun is false and the skip the block of code.
Hope that helps.
He who laughs last is a bit on the slow side
|
|
|
|
|
hi Nero ,
Thamks 4 u r reply , But Iam not awareof registry entry concept,Could u pls tell me detailed how do i do it.
|
|
|
|
|
|
pls try nt 2 use smsspk in da 4rm. s'not a mob fone.
|
|
|
|
|
there is a image on a click on one part of an image i have to connect a page
and another part i have to do connect someother page
how to do this piece of work
thanking u
|
|
|
|
|
Split the image in two parts and put two image controls on the page and write your code accordingly.
Jayant D. Kulkarni
Brainbench Certified Software Engineer in C#, ASP.NET, .NET Framework and ADO.NET
|
|
|
|
|
I was wondering if it is possible to compare file attributes over the internet?
|
|
|
|
|
Hi Frnds,
i have a issue in C#.
when i run some code which i downloaded from internet i got the error like.
"visual studio can not start debugging because the debug target 'so so so so path .exe' is missing. please build the project and retry, or set the output path and assemblyname properties appropriately to point at the correct location for the target assembly."
i dont know how to resolve..i search on internet since 1 week still i m not able to get the solution...
i post in forum also asked some developers but still i am not getting the solutions.
i m not able to run project...because in Bin directory there is no (.EXE) it is not necessary that we must have (*.exe) in BIN directory.
in my some program if i delete the Bin Folder and again run then it will automatically created BIN folder and (.exe) file also in those project i didnt get error that you have no (.exe)
in some program if i click on the Rebuil System. then it delete (.exe) and the program will not run and get the same error like "visual studio can nt start debugging ......................"
i know this problem is small for all of you... please try to solve and give me the proper suggestion
Awaiting for your positive reply..
patelhiren20@rediffmail.com
Thanks & Regards
Hiren Patel
patelhiren20@rediffmail.com
hiren
|
|
|
|
|
If there's no exe, then your build has failed. That's what happens when you grab code at random and add it to your project.
What is the error with the actual code ?
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
Tools->Options->Projects and Solutions->Build and run:
set: "On Run, when projects are out of date:" to: "Always build"
set: "On Run, when build or deployment errors occur:" to "do not launch"
life is study!!!
|
|
|
|
|
Hi,
I am having a WCF service which exposes two functions. One returns a string and another returns an array of custom object Employee. Employee class is a DataContract and all it contains is simple premitive data types marked as DataMember. First method works fine but I am not able to get the second method GetEmployeeArray() working.
Any clues how to pass complex data types with premitive data members in WCF?
Jayant D. Kulkarni
Brainbench Certified Software Engineer in C#, ASP.NET, .NET Framework and ADO.NET
|
|
|
|